Management Studio 不显示光标的结果
好吧,这可能很简单,但我找不到解决方案。 我开始在 T-SQL 中使用游标并尝试它们。
但是,如果我通过执行按钮在 Management Studio 中执行结果,则不会返回结果。我得到的只是“命令执行成功”。
如果我调试它,我会得到结果,下次单击执行时,我也会得到结果......
是否有某种缓存?还是我做错了?
脚本看起来像这样:
declare @po varchar(20), @prod varchar(50), @qty integer, @type varchar(20)
declare db_cursor cursor for
select product, po, qty, space(1) as btype from header
for read only
open db_cursor
while @@FETCH_STATUS=0
begin
fetch db_cursor into @po, @prod, @qty, @type
if @qty<1000
set @type = 'small'
else
set @type = 'large'
print @type
end
close db_cursor
deallocate db_cursor
PS:当然我在打印之前使用了选择,同样的问题。
Ok, this might be an easy one, but I can't find a solution.
I'm beginning to work with cursors in T-SQL and am playing around with them.
However I don't get my results back if I execute them in Management Studio via the Execute-Button. All I get is "Command executed successfully".
If I debug it I get the results and the next time I click on execute I also get the results...
Is there some kind of cache? Or am I doing it wrong?
Script looks like this:
declare @po varchar(20), @prod varchar(50), @qty integer, @type varchar(20)
declare db_cursor cursor for
select product, po, qty, space(1) as btype from header
for read only
open db_cursor
while @@FETCH_STATUS=0
begin
fetch db_cursor into @po, @prod, @qty, @type
if @qty<1000
set @type = 'small'
else
set @type = 'large'
print @type
end
close db_cursor
deallocate db_cursor
PS: naturally I used select before print, same issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
啊我明白你的意思了。第一次运行下面的脚本会产生结果。在随后的情况下,什么也不会发生(除非您在新的 SSMS 窗口中尝试)。
问题在于您如何检查
@@FETCH_STATUS
值。这在新连接中从0
开始,但您的脚本将其保留在-1
处。您需要在循环之前
获取
第一行。请参阅此博文了解正常模式。Ah I see what you mean. On the first occasion the script below is run it produces results. On subsequent occasions nothing happens (unless you try in a new SSMS window).
The problem is how you are checking the
@@FETCH_STATUS
value. This starts off at0
in a new connection but your script leaves it at-1
.You need to
Fetch
the first row before the loop. See this blog post for the normal pattern.如果可以的话,尝试切换选项卡:
听起来您正在“消息”选项卡上
编辑
另外,在仔细查看之后,您是在选择光标数据之后还是选择了整个脚本?我问这个问题是因为看起来您只是设置变量而不对它们执行任何操作(当然除了
print
)。Try switching the tabs if you can:
It sounds like you're on the Messages tab
Edit
Also, after looking at it more, are you selecting your cursor data after that or is that your whole script? I ask because it looks like you're just setting variables and not doing anything with them (except the
print
of course).使用以文本形式查看结果选项
Use the View results as text option