SET NOCOUNT OFF or RETURN @@ROWCOUNT?(SET NOCOUNT OFF 还是 RETURN @@ROWCOUNT?)
问题描述
我正在 Sql Server 2008 数据库中创建一个存储过程.我想返回受影响的行数.SET NOCOUNT OFF 或 RETURN @@ROWCOUNT 哪个更好?
I am creating a stored procedure in Sql Server 2008 database. I want to return the number of rows affected. Which is a better option SET NOCOUNT OFF or RETURN @@ROWCOUNT?
ALTER PROCEDURE [dbo].[MembersActivateAccount]
@MemberId uniqueidentifier
AS
BEGIN
-- Should I use this?
SET NOCOUNT OFF;
UPDATE [dbo].Members SET accountActive = 1 WHERE id = @MemberId;
--Or should I SET NOCOUNT ON and use the following line instead?
--return @@ROWCOUNT;
END
我知道两者都有效,但哪个更好,为什么?
I know that both work, but which is a better choice and why?
经过一些尝试,我得出的结论是,在存储过程中,默认情况下 SET NOCOUNT 为 OFF.是否可以在我的数据库中更改此行为?
After some trying I am coming to a conclusion that SET NOCOUNT is OFF by default inside stored procedures. Is it possible to change this behavior inside my database?
推荐答案
使用@@RowCount.它是明确和透明的,它完全由您的代码而不是内置行为控制.
Use @@RowCount. It's explicit and transparent, it is entirely controlled by your code rather than a built-in behaviour.
NOCOUNT 选项可以手动设置为默认为 ON (Optons>Query Execution>SQL Server>Advanced).如果您以这种方式设置它,但随后在您的存储过程中声明 SET NOCOUNT OFF,则该本地设置优先.
The NOCOUNT option can be manually set to default to ON (Optons>Query Execution>SQL Server>Advanced). If you set it this way but then declare SET NOCOUNT OFF in your stored procedure then that local setting takes precedence.
这篇关于SET NOCOUNT OFF 还是 RETURN @@ROWCOUNT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SET NOCOUNT OFF 还是 RETURN @@ROWCOUNT?
基础教程推荐
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
