SET NOCOUNT OFF 还是 RETURN @@ROWCOUNT?

SET NOCOUNT OFF or RETURN @@ROWCOUNT?(SET NOCOUNT OFF 还是 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)