Solved

SET NOCOUNT ON clearing @@ROWCOUNT in trigger

Posted on 2004-08-24
3
3,560 Views
Last Modified: 2012-05-05
On Books online it says:
"The @@ROWCOUNT function is updated even when SET NOCOUNT is ON"

Thus, I created the following trigger:


CREATE TRIGGER triggerMaster ON dbo.CG_BalanceMovements
AFTER  INSERT
AS

set nocount on

if (@@rowcount  = 1)
      begin
            -- do interesting stuff --
      end
else
      raiserror   ('Tried to add %d records, but only one BalanceMovement can be registered at a time .' , 16, 1, @@rowcount)


Then if I insert 1 row on enterprise manager, I get an error saying that I'm trying to insert 0 rows ... but if I comment out, the set nocount on, it works perfectly!

Why is this?
0
Comment
Question by:joshpressman
3 Comments
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 63 total points
ID: 11888727
>>Why is this?<<
Not sure.  Though I expect what BOL is referring to is having SET NOCOUNT ON at the start of the Stored Procedure.  In other words, by doing SET NOCOUNT ON you are in fact resetting the @@ROWCOUNT value to 0

As a workaround consider doing SET NOCOUNT ON after you check @@ROWCOUNT as in:

CREATE TRIGGER triggerMaster ON dbo.CG_BalanceMovements
AFTER  INSERT
AS

Declare @RowCount integer

SET @RowCount = @@ROWCOUNT

set nocount on

if (@RowCount  = 1)
     begin
          -- do interesting stuff --
     end
else
     raiserror   ('Tried to add %d records, but only one BalanceMovement can be registered at a time .' , 16, 1, @RowCount)


Or not use @ROWCOUNT altogether, but instead do something like this:

CREATE TRIGGER triggerMaster ON dbo.CG_BalanceMovements
AFTER  INSERT
AS

Declare @RowCount integer
set nocount on

Select @RowCount = Count(*) From Inserted
if (@RowCount  = 1)
     begin
          -- do interesting stuff --
     end
else
     raiserror   ('Tried to add %d records, but only one BalanceMovement can be registered at a time .' , 16, 1, @RowCount)
0
 
LVL 11

Accepted Solution

by:
ram2098 earned 62 total points
ID: 11888851
@@ROWCOUNT always depends on the last SQL statement that you execute.

because of "SET NOCOUNT ON" (since this was the last statement before your if statement), it always returns you zero.

If you remove that it is actually taking the count from your actual insert (since it was the last statement executed).

But, remember, there is no relation to "SET NOCOUNT ON" and @@rowcount.
0
 
LVL 1

Author Comment

by:joshpressman
ID: 11888878
Points splitted for your prompt replies!

Thanks guys... Very naive from me :-)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question