how to iterate over an cursor
I have an select statement that returns a single row. After that I have written a cursor as for me @@fetch_status is -1 it does not go inside the cursor only now
open cur_mkt
print @init
While (@init = 0)
Begin
fetch next from cur_mkt into
@Desc,
@Divisions
print @@fetch_status
if (@@fetch_status =开发者_Go百科-1)
BREAK
Is there any way I can go inside the cursor, please help me out.
It doesn't sound like you need a cursor (which you should try to avoid anyway). If you're determining the presence of a result you could do:
SELECT @Desc = Desc, @Divisions = Divisions
FROM YourTable
WHERE ID = 1
IF ( @@ROWCOUNT > 0 )
BEGIN
-- Row was found
END
So I would recommend not using cursors.
To directly answer the question, the way you use cursors/iterate round the results is as follows:
DECLARE @A INTEGER
DECLARE cur_mkt CURSOR FOR
SELECT 1 AS A
UNION ALL
SELECT 2 AS A
OPEN cur_mkt
FETCH NEXT FROM cur_mkt INTO @A
WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT @A
FETCH NEXT FROM cur_mkt INTO @A
END
CLOSE cur_mkt
DEALLOCATE cur_mkt
精彩评论