您的位置:首页 > 数据库

sql 游标只读一次的原因

2016-05-06 19:16 573 查看
今天在用sql的游标的时候,在正确运行的情况下,想再运行一次装一下的时候,发现只能提示 命令已成功生成,并没有得到第一次运行时候的结果。查阅资料以后才知道要在while前面加上一行 fetch。真是被自己蠢哭了,下面看一个简单实例
declare test_cursor4 cursor scroll for
select StudyID
from [PACS].[dbo].MEDICAL_LOGON
open test_cursor4
DECLARE @examid int
WHILE @@fetch_status = 0
begin
print @examid
fetch next from test_cursor4 into @examid
end

close test_cursor4--关闭游标
deallocate test_cursor4--释放游标
这是原来的代码,测试后会出现上面描述的问题
<pre name="code" class="sql">declare test_cursor4 cursor scroll for
select StudyID
from [PACS].[dbo].MEDICAL_LOGON
open test_cursor4
DECLARE @examid int
fetch next from test_cursor4 into @examid
WHILE @@fetch_status = 0
begin
print @examid
fetch next from test_cursor4 into @examid
end

close test_cursor4--关闭游标
deallocate test_cursor4--释放游标
这是修改后的代码,就是在while之前加上一个 fetch 语句。希望对菜鸟有帮助,因为我也是菜鸟

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: