創建表: 1 1 create table tb1( 2 2 3 3 id int , 4 4 5 5 name nvarchar(20) 6 6 7 7 ) 創建返回游標的存儲過程: 1 create proc tb1_proc ( 2 3 @cur cursor varying output 4 ...
創建表:
1 1 create table tb1( 2 2 3 3 id int , 4 4 5 5 name nvarchar(20) 6 6 7 7 )View Code
-------------------------------------------------
創建返回游標的存儲過程:
1 create proc tb1_proc ( 2 3 @cur cursor varying output 4 5 ) 6 7 as 8 9 begin 10 11 set @cur=cursor for 12 13 select * from tb1 14 15 end 16 17 open @curView Code
-------------------------------------------------
使用存儲過程返回的游標:
1 declare @my_cur cursor 2 3 declare @id int, @name nvarchar(20) 4 5 exec tb1_proc @my_cur output 6 7 --open @cursor -- @cursor already opened 8 9 fetch next from @my_cur into @id, @name 10 11 while(@@fetch_status=0) 12 13 begin 14 15 print '編號:' + convert(nvarchar,@id) 16 17 print '姓名:' + @name 18 19 print '......................' 20 21 fetch next from @my_cur into @id, @name 22 23 endView Code
註意:存儲過程中創建游標後要打開