SET NOCOUNT ON usage

前端 未结 17 1927
抹茶落季
抹茶落季 2020-11-22 05:55

Inspired by this question where there are differing views on SET NOCOUNT...

Should we use SET NOCOUNT ON for SQL Server? If not, why not?

17条回答
  •  花落未央
    2020-11-22 06:12

    SET NOCOUNT ON even does allows to access to the affected rows like this:

    SET NOCOUNT ON
    
    DECLARE @test TABLE (ID int)
    
    INSERT INTO @test
    VALUES (1),(2),(3)
    
    DECLARE @affectedRows int = -99  
    
    DELETE top (1)
      FROM @test
    SET @affectedRows = @@rowcount
    
    SELECT @affectedRows as affectedRows
    

    Results

    affectedRows

    1

    Messages

    Commands completed successfully.

    Completion time: 2020-06-18T16:20:16.9686874+02:00

提交回复
热议问题