下麵的見解是在使用游標的過程中做的日記。我也是第一次使用,如果有什麼不對的地方請批評指正,大家一起努力。 1. 消息 16951,級別 16,狀態 1,過程 usp_proc,第 16 行 變數 '@myref' 不能用作參數,因為在執行該過程前,不得為 CURSOR OUTPUT 參數分配游標。 ...
下麵的見解是在使用游標的過程中做的日記。我也是第一次使用,如果有什麼不對的地方請批評指正,大家一起努力。
1.
消息 16951,級別 16,狀態 1,過程 usp_proc,第 16 行
變數 '@myref' 不能用作參數,因為在執行該過程前,不得為 CURSOR OUTPUT 參數分配游標。
這個問題是我在調用一個遞歸的、輸出cursor output 的存儲過程
create proc usp_proc( @level int @myref cursor varying output ) as begin if @level=3 begin set @myref=cursor local static for select * from table open @myref end if @level<3 begin declare @cur cursor exec usp_proc 2 @cur output --遞歸 -- --對輸出游標@cur做一些操作 -- --使用完游標 close @cur --關閉游標 deallocate @cur --刪除游標 end end
如果沒有對輸出的游標做close、deallocate處理就會出現上面錯誤。
2.
沒有為@cur,分配游標
這個問題是我在使用存儲過程返回的游標 cursor output 產生的
create proc myproc( @mycur cursor varying output ) as begin set @mycur=cursor local static for select * from table open @mycur --打開游標 end --調用myproc declare @cur cursor exec myproc @cur output fetch next from @cur while @@fetch_status=0 begin --使用游標 fetch next from @cur end
出現上述錯的原因就是定義游標後需要打開 open @mycur