某某茶叶有限公司欢迎您!
澳门新葡亰1495app > 操作系统 > 存储过程中创建游标后要打开【澳门新葡亰1495app】

存储过程中创建游标后要打开【澳门新葡亰1495app】

时间:2020-01-01 06:46

存储过程中创建游标后要打开【澳门新葡亰1495app】。存储过程中创建游标后要打开【澳门新葡亰1495app】。存储过程中创建游标后要打开【澳门新葡亰1495app】。创建表:

澳门新葡亰1495app 1澳门新葡亰1495app 2

1 1 create table tb1(
2 2 
3 3 id int ,
4 4 
5 5 name nvarchar(20)
6 6 
7 7 )

澳门新葡亰1495app,存储过程中创建游标后要打开【澳门新葡亰1495app】。存储过程中创建游标后要打开【澳门新葡亰1495app】。View Code


创建返回游标的存储过程:

澳门新葡亰1495app 3澳门新葡亰1495app 4

 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


 使用存储过程返回的游标:

澳门新葡亰1495app 5澳门新葡亰1495app 6

 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

 注意:存储过程中创建游标后要打开