sqlserver 存储过程返回游标的处理

时间:2024-01-21 08:39:32

sqlserver 存储过程返回游标的处理

Posted on 2018-07-11 10:15 jhjs 阅读(...) 评论(...) 编辑 收藏

创建表:

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 @cur
View 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   end
View Code