尝试添加
FETCH NEXT FROM
之间
OPEN
和
WHILE
:
DECLARE Something CURSOR FOR
SELECT * FROM tblSomething
OPEN Something
FETCH NEXT FROM Something -- <== add this line
WHILE @@FETCH_STATUS = 0
BEGIN
--CALL ANOTHER PROCEDURE
FETCH NEXT FROM Something
END
CLOSE Something
DEALLOCATE Something
下面是一个简单的演示,其中有一个10行表和一个游标,它读取每一行并打印相应的id:
declare @tmp table (row_id int)
insert into @tmp values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)
DECLARE @myID as int;
DECLARE @wf_Cursor as CURSOR;
SET @wf_Cursor = CURSOR FOR SELECT row_id FROM @tmp
OPEN @wf_Cursor;
FETCH NEXT FROM @wf_Cursor INTO @myID;
WHILE @@FETCH_STATUS = 0
BEGIN
print('Reading row: ' + cast(@myID as nvarchar(10)));
FETCH NEXT FROM @wf_Cursor INTO @myID;
END
CLOSE @wf_Cursor;
DEALLOCATE @wf_Cursor;
输出: