sql-server – 在SQLCMD中忽略SET NOCOUNT OFF

我想查看查询运行的每个DDL语句会影响多少行,因此我在运行的每个查询的开头设置SET NOCOUNT OFF.

示例查询:

SET NOCOUNT OFF;
GO
BEGIN TRY

    BEGIN TRANSACTION
    UPDATE dbo.tbProvClause SET ClauseTemplate = 'Clause1' where DocumentName = '\Templates\EndorsAccessPlainLanguageQCEng.CDS';
    UPDATE dbo.tbProvClause SET ClauseTemplate = 'Clause 2' where DocumentName = '\Templates\EndorsEnforcedRemovallLtdMktPublicPropertyQCEng.CDS';
    UPDATE dbo.tbProvClause SET ClauseTemplateFR = 'Malgré French Clause 1' where DocumentNameFR = '\Templates\EndorsAccessHOPPQcFr.CDS';
    UPDATE dbo.tbProvClause SET ClauseTemplateFR = 'Malgré les exceptions  Clause 2' where DocumentNameFR = '\Templates\EndorsEnlèvementFTNdomainepublicERLMPublicPropertyQcFr.CDS';  
   COMMIT TRAN
   PRINT 'Script Completed With Success - Changes committed on ' + CAST(current_timestamp AS varchar(25))
END TRY

BEGIN CATCH
   --
END CATCH

GO

然后它回来了

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)
Script Completed With Success - Changes committed on Nov 29 2017 12:10PM

这很好.但是当我在SQLCMD中运行相同的时候,我只得到1行.i.e.

sqlcmd -S testserver -dTestDB -i StackOverflowSQL.sql

(1 rows affected)
Script Completed With Success - Changes committed on Nov 29 2017 12:24PM

如何在SQLCMD中保留SET NOCOUNT OFF的功能?我问这个问题的原因是我有许多脚本需要使用SQLCMD进行批处理,我将保存他们的日志.在这种情况下,SET NOCOUNT OFF对于检查受影响的1行中有多少行将给出运行成功的反馈非常有用.

最佳答案 尝试这样的事情,看看它是否有效.

使用-v(小写字母v).

sqlcmd -v NOCOUNT=OFF -S testserver -dTestDB -i StackOverflowSQL.sql

要么

在同一命令提示符中,首先在调用sqlcmd之前运行SET NOCOUNT = OFF.

查看以下文档链接并搜索“变量优先级”.你会得到一些想法.

https://docs.microsoft.com/en-us/sql/tools/sqlcmd-utility

点赞