在SQL Server中使用游标变量的优点(declare @cn cursor) [英] Advantages on using cursor variable in SQL Server (declare @cn cursor)

查看:545
本文介绍了在SQL Server中使用游标变量的优点(declare @cn cursor)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在T-SQL中,可以用两种方式(我知道)来声明游标:

In T-SQL a cursor can be declared in two ways (that I know of):


  1. declare CursorName cursor for ...

  2. 声明@CursorName游标

  1. declare CursorName cursor for ...
  2. declare @CursorName cursor

我正在运行一些测试,我注意到,创建一个游标变量不会在 sp_cursor_list

I was running some tests and I notice that the creation of a cursor variable will not add an entry to the result of sp_cursor_list.

从性能,资源利用等角度来看,使用第二种方法有什么优点/缺点吗?

Is there any advantage/disadvantage on using the second approach from the point of view of performance, resource utilization, etc?

PS:我知道潜在的游标性能问题。我不是要求对游标vs集的比较。

推荐答案

p>从我读的游标变量的目的是能够使用它作为存储过程中的输出变量,从而使您能够将游标中的数据发送到另一个控制过程。我没有试过这个,所以我不知道它将如何工作,但这是我从阅读在线书。我会惊讶,如果有任何可衡量的性能差异,当然不是通过不使用光标在第一个地方可以获得的改善。如果你不打算使用它作为输出变量,我建议保持更常见的光标定义可能使代码更容易维护。

From what I read the purpose of the cursor variable is to be able to use it as an output variable in stored proc, thus enabling you to send the data in the cursor to another controlling proc. I have not tried this so I don't know exactly how it would work, but that is what I get from reading Books Online. I would be surprised if there is any measurable performance difference and certainly not the the improvement you could get by not using a cursor in the first place. If you aren't planning to use it as an output variable, I'd suggest that staying with the more common cursor definiton might make the code easier to maintain.

也就是说,实际上需要一个游标的情况非常少。

That said, there are very, very few cases where a cursor is actually needed.

这篇关于在SQL Server中使用游标变量的优点(declare @cn cursor)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆