declare @temp_temp uniqueidentifier--臨時變數 DECLARE aaa CURSOR for select Id from A -------------------打開游標 open aaa --先查詢一次再迴圈,防止有多個游標時@@FETCH_STATUS=-
declare @temp_temp uniqueidentifier--臨時變數 DECLARE aaa CURSOR for select Id from A -------------------打開游標 open aaa --先查詢一次再迴圈,防止有多個游標時@@FETCH_STATUS=-1不能進入下個游標迴圈的情況 fetch next from aaa into @temp_temp -------------------迴圈取數據 while @@FETCH_STATUS=0 begin print @temp_temp fetch next from aaa into @temp_temp end ----------------------------------- 關閉游標 Close aaa ----------------------------------- 刪除游標 Deallocate aaa
游標的嵌套
declare @temp_temp uniqueidentifier--臨時變數 DECLARE aaa CURSOR for select Id from A -------------------打開游標 open aaa --先查詢一次再迴圈,防止有多個游標時@@FETCH_STATUS=-1不能進入下個游標迴圈的情況 fetch next from aaa into @temp_temp -------------------迴圈取數據 while @@FETCH_STATUS=0 begin print @temp_temp --===========================游標嵌套 DECLARE bbb CURSOR for select Id from B -------------------打開游標 open bbb --先查詢一次再迴圈,防止有多個游標時@@FETCH_STATUS=-1不能進入下個游標迴圈的情況 fetch next from bbb into @temp_temp -------------------迴圈取數據 while @@FETCH_STATUS=0 begin print @temp_temp fetch next from bbb into @temp_temp end ----------------------------------- 關閉游標 Close bbb ----------------------------------- 刪除游標 Deallocate bbb --===========================游標嵌套 fetch next from aaa into @temp_temp end ----------------------------------- 關閉游標 Close aaa ----------------------------------- 刪除游標 Deallocate aaa