Link to home
Start Free TrialLog in
Avatar of griswald65
griswald65

asked on

Creating a cursor to select in a particular order

I need to cursor a table in a particular order because i am creating a new sort number that is to update the table.  Any ideas on how to do this.  The cursor is not liking the order by in the select statement for the cursor.  Here is what I have:

Declare curNum cursor

For (Select Number, subNumber From ##Table order by sortorder)

Declare @Num varchar (25)
Declare @subnum varchar (25)

Open curNum

Fetch Next from curNum Into @Num, @subnum
While (@@Fetch_Status = 0)

Begin
Then Statement
Fetch Next from curNum into @Num, @subnum

End
Close curNum
Deallocate curNum
Avatar of derekkromm
derekkromm
Flag of United States of America image

You can't use an ORDER BY in the for section of a cursor. Just put this right before the cursor declaration:

select * into ##Table2 from ##Table order by sortorder

and then reference ##Table2 instead of ##Table
ASKER CERTIFIED SOLUTION
Avatar of dportas
dportas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial