这个页面很旧,回复很旧。但是,最好的答案并没有被投票给顶部。我想这是因为没有提供足够的解释。
使用 NOCOUNT 设置。每个人都应该看看 NOCOUNT 设置。默认设置为关闭。
即使在新数据库上也普遍更改 this 的默认设置可能会导致某些编码人员\用户感到困惑。我建议使用在更改设置之前捕获设置,然后将其设置回来的方法。这在下面的示例脚本中显示,该脚本演示了 NOCOUNT 设置的使用。
这是一篇好文章。
https://www.sqlshack.com/set-nocount-on-statement-usage-and-performance-benefits-in-sql-server/
DROP TABLE IF EXISTS TestTable
GO
CREATE TABLE TestTable (ID INT, TestText VARCHAR (40))
GO
-- Get the Original NOCOUNT setting and save it to @OriginalNoCountSettingIsOn
DECLARE @options INT
SET @options = @@OPTIONS
DECLARE @OriginalNoCountSettingIsOn AS bit
SET @OriginalNoCountSettingIsOn = IIF(( (512 & @@OPTIONS) = 512 ),1,0)
-- Now set NOCOUNT ON to suppress result output returned from INSERTS
-- Note - this does not affect @@ROWCOUNT values from being set
SET NOCOUNT ON -- <---- Try running script with SET NOCOUNT ON and SET NOCOUNT OFF to See difference
INSERT INTO TestTable (ID, TestText)
VALUES (0, 'Test Row 1')
INSERT INTO TestTable (ID, TestText)
VALUES (0, 'Test Row 2'),
(0, 'Test Row 3'),
(0, 'Test Row 4')
INSERT INTO TestTable (ID, TestText)
VALUES (0, 'Test Row 5')
/*Now set NOCOUNT back to original setting*/
IF @OriginalNoCountSettingIsOn = 1
BEGIN
SET NOCOUNT ON
END
ELSE
BEGIN
SET NOCOUNT OFF
END
DROP TABLE IF EXISTS TestTable
GO