Solved

SET NOCOUNT ON clearing @@ROWCOUNT in trigger

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Backup Job question 4 20
Concatenating multiple comments into one row 16 46
while loop in html mail format 5 33
sql query Help 12 34
Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
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 …
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now