Management Studio 不显示光标的结果

发布于 2024-11-10 04:37:12 字数 710 浏览 5 评论 0原文

好吧,这可能很简单,但我找不到解决方案。 我开始在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

通知家属抬走 2024-11-17 04:37:13

啊我明白你的意思了。第一次运行下面的脚本会产生结果。在随后的情况下,什么也不会发生(除非您在新的 SSMS 窗口中尝试)。

    declare @po varchar(20), @prod varchar(50), @qty integer, @type varchar(20)

    declare db_cursor cursor for
    select name, name, number, space(1) as btype from master..spt_values
    for read only

    open db_cursor

    while @@FETCH_STATUS=0
    begin
    print 'y'
      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

问题在于您如何检查 @@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).

    declare @po varchar(20), @prod varchar(50), @qty integer, @type varchar(20)

    declare db_cursor cursor for
    select name, name, number, space(1) as btype from master..spt_values
    for read only

    open db_cursor

    while @@FETCH_STATUS=0
    begin
    print 'y'
      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

The problem is how you are checking the @@FETCH_STATUS value. This starts off at 0 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.

烟雨扶苏 2024-11-17 04:37:13

如果可以的话,尝试切换选项卡:
SSMS 选项卡示例

听起来您正在“消息”选项卡上

编辑
另外,在仔细查看之后,您是在选择光标数据之后还是选择了整个脚本?我问这个问题是因为看起来您只是设置变量而不对它们执行任何操作(当然除了 print )。

Try switching the tabs if you can:
SSMS Tab example

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).

故事未完 2024-11-17 04:37:13

使用以文本形式查看结果选项

Use the View results as text option

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文