Girish Gupta
Girish Gupta

Reputation: 1293

How to get number of rows affected by ExecuteNonQuery and ignore rows of triggers?

I am using ExecuteNonQuery to run an insert proc, it returns 2, but in actual I am inserting only 1 record. I am getting 1 extra due to trigger. Is there anyway that I get only actual number of rows affected. I do not want the rows affected by trigger.

Upvotes: 8

Views: 3944

Answers (2)

Tanmay Nehete
Tanmay Nehete

Reputation: 2196

please refer below mention Image

enter image description here

However, the MSDN article on this function states: For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

See... enter link description here

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460238

If you don't have it already, disable counting rows in your trigger:

SET NOCOUNT ON   

For example:

CREATE TRIGGER [dbo].[triggerName] ON [dbo].[TableName]
AFTER INSERT
AS
SET NOCOUNT ON;
......

MSDN

Upvotes: 9

Related Questions