Solved

SET NOCOUNT ON clearing @@ROWCOUNT in trigger

Posted on 2004-08-24
3
3,584 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

739 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