Jump to content
Fórum Script Brasil
  • 0

Order Function Returned Table


shaq

Question

Hi, here is the problem

/* Order Function Returned Table *
Need to minimaly change a function(GetBigTableData), 
so that it returns the same results but ordered.
PS: Working in SQL Server 2005
*/
/* Assuming that: BigTable, @ReturnTable e @AuxTable have the same schema */
create function GetBigTableData (@All bit=0)
returns @ReturnTable table(col int)
as
begin
declare @AuxTable Table(col int)
/*-----UNCHANGABLE CODE--------------------------------------*/
    insert into @AuxTable
        select * from BigTable where BigTable.col < 5
    if @All=1
    begin
        insert into @AuxTable
            select * from BigTable where BigTable.col >= 5
    end
/*----------------------------------------------------------*/            
    insert into @ReturnTable
        select * from @AuxTable
            order by @AuxTable.col        --Error: Must declare the scalar variable "@AuxTable".
            --order by @ReturnTable.col    --Error: Must declare the scalar variable "@ReturnTable".
    return
end
go

Any Solutions?

Many Thanks,

Rui Miranda

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Dear Shap,

Remove the variable reference order by @AuxTable.col by order by col.

/* Order Function Returned Table *
Need to minimaly change a function(GetBigTableData),
so that it returns the same results but ordered.
PS: Working in SQL Server 2005
*/
/* Assuming that: BigTable, @ReturnTable e @AuxTable have the same schema */
create function GetBigTableData (@All bit=0)
returns @ReturnTable table(col int)
as
begin
declare @AuxTable Table(col int)
/*-----UNCHANGABLE CODE--------------------------------------*/
    insert into @AuxTable
        select * from BigTable where BigTable.col < 5
    if @All=1
    begin
        insert into @AuxTable
            select * from BigTable where BigTable.col >= 5
    end
/*----------------------------------------------------------*/            
    insert into @ReturnTable
        select * from @AuxTable
            order by col        --Error: Must declare the scalar variable "@AuxTable".
            --order by @ReturnTable.col    --Error: Must declare the scalar variable "@ReturnTable".
    return
end
go

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...