Solved

SET NOCOUNT ON clearing @@ROWCOUNT in trigger

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

705 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