2

In my database, i have a trigger which insert the change log entries when a row in Table tblA is updated.

Now, in my code i have to update it through a plain Sql query like

int count = DBContext.ExecuteStoreCommand("<sql query to update records>");

This count variable contains the number of rows affected(no of rows updated + no of rows inserted) due to query.

So my question is, How do i can get only the number of updated rows?

Currently i'm using Entity framework 4. I have looked for solution through connected or disconnected model but couldn't help myself.

Wasim Bajwa
  • 395
  • 3
  • 14
  • duplicated question, found: [stackoverflow](https://stackoverflow.com/questions/1103260/return-number-of-rows-affected-by-update-statements) – Foo Bar May 30 '17 at 06:38
  • @FooBar both are different as this question is to get the number of rows get updated not number of rows affected. – Wasim Bajwa May 30 '17 at 06:42

1 Answers1

3

int count = DBContext.ExecuteStoreCommand("");

I think you hv to change this to return Select result set

then do this,

<sql query to update> 
Select @@RowCount rowcountAffected

Or suppose your update is

update table1 set col1='foo' where id=2
select count(*) rowcountAffected from table1 where id=2

The most efficient way to return row affected can be

i) Assuming you only update (don't refresh any record after that)

Put Set Nocount ON
Declare @Output parameter inside proc
KumarHarsh
  • 5,046
  • 1
  • 18
  • 22