使用 SET ROWCOUNT 安全吗?
我正在使用 SET ROWCOUNT ,因为该值来自我的过程中的参数。
SET ROWCOUNT @take
SELECT * FROM Something
SET ROWCOUNT 0
另一个过程是否可以同时执行并获取行计数设置,或者在存储过程上使用它是否完全安全?
I am using SET ROWCOUNT
because the value comes from a parameter into my procedure.
SET ROWCOUNT @take
SELECT * FROM Something
SET ROWCOUNT 0
Is it possible to another procedure executes at the same time and get the rowcount setting, or is it perfectly safe to use it on a stored procedure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Rowcount 特定于您当前的范围,因此您在那里是安全的。然而,在线图书告诉我这一点(这可能会也可能不会影响您的需求):
TOP 也可以使用变量,现在可以在 INSERT、UPDATE 和 DELETE 语句中使用。 (嘿,我今天学到了一些新东西。)在在线书籍中查找如何将 TOP 与变量一起使用。
Rowcount is specific to your current scope, so you are safe there. However, Books Online tells me this (which may or may not affect your needs):
TOP can use variables too and now can be used in INSERT,UPDATE and DELETE statments. (Hey I learned something new today.) Look up how to use TOP with variables in Books online.
我添加这个答案是为了那些可能仍在搜索这个问题的人的利益。
使用 SET ROWCOUNT 不再安全,因为它将在下一版本的 SQL Server 中弃用:
TechNet:已弃用的数据库引擎功能
I am adding this answer for the benefit of people who may still search for this.
It is no longer safe to use SET ROWCOUNT as it will be deprecated in the next version of SQL Server:
TechNet: Deprecated Database Engine Features
我这样使用它仅用于选择,所以它还不会被弃用
作为监视器线程,我需要从这个表中选择所有待处理的行,但是,
作为进程线程我只想一次处理 10 行
但我不想在我的存储过程中出现意大利面条代码,所以这是我的解决方案:
参数(@top int = 0)
--正常选择
从表中选择 *
-- 获取所有行或获取虚拟“顶部 (@top)”
-- 示例:exec up_myProc @top=10 -- 获取前 10 个
-- exec up_myProc @top=0 -- 获取所有行
I am using it like this FOR SELECT ONLY SO ITS NOT going to be deprecated yet
as a monitor thread i need to select all pending rows from this one table but,
as the process thread i only want to process 10 rows at a time
but i don't want spaghetti code in my sproc so here is my solution:
parameters ( @top int = 0 )
-- select as normal
select * from aTable
-- either gets all rows or gets a virtual "top (@top)"
-- example: exec up_myProc @top=10 -- gets top 10
-- exec up_myProc @top=0 -- gets all rows
对 Necro 感到抱歉,刚刚在 SQLServer 2016 上偶然发现了这一点,并认为它可能很有趣:
如果您使用
SELECT; INTO tabY from tabX
,如果您设置了 SET ROWCOUNT <>,这也会受到影响。 0,即使此选择发生在您正在调用的过程中。所以要小心。Sorry for the Necro, just stumbled upon this on SQLServer 2016 and thought it might be interesting:
If you are using a
SELECT <x> INTO tabY from tabX
, this will also be affected if you have SET ROWCOUNT <> 0, even if this select happens in a procedure that you are calling. So be careful.