Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 cursor

Author  Topic 

jgrant
Yak Posting Veteran

69 Posts

Posted - 2007-03-21 : 19:25:17
If I declare a cursor and want to use it twice in one batch, I just open it again for the second use and it will be back at the start correct?

The Yak Village Idiot

krishnarajeesh
Yak Posting Veteran

67 Posts

Posted - 2007-03-22 : 01:49:04
Hi Yak,

You can open the same cusor for a second use. For that you have to decalre your cusror as SCROLL.
The default option is FORWARD_ONLY. Also after your first batch close the while loop used for cursor and do the following ==> fetch FIRST from Cursurname into @declaredvariable. This command will fetch the first value from the cursor. See the exmaple for more information.


declare @declaredvariable varchar(20)

declare Cursurname Cursor SCROLL for
SELECT name from tablename

OPEN Cursurname
FETCH NEXT FROM Cursurname
INTO @declaredvariable
WHILE @@FETCH_STATUS = 0
BEGIN
print @declaredvariable
FETCH NEXT FROM Cursurname
INTO @declaredvariable
end

fetch first from Cursurname
into @declaredvariable

print @declaredvariable

CLOSE Cursurname
DEALLOCATE Cursurname


Thanks & Best Regards,
Krishna
Go to Top of Page
   

- Advertisement -