Solved

SET NOCOUNT ON clearing @@ROWCOUNT in trigger

Posted on 2004-08-24
3
3,548 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

770 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