1

I am running an insert query from c# using:

string affectedRows = myCommand.ExecuteNonQuery().ToString();

However the query that I am running does two things. It first inserts stuff into a temp table, and then inserts stuff into the real-table.

Eg:

declare @t table (a int)
insert into @t 
values(1)

insert into MyTable
select * from @t

Since it does two inserts, my affectedrows will be 2. The question is how to i get either the last result of the insert, or perhaps an array/list of results?

Dividing by 2 won't help me, as there maybe multiple queries, etc and it won't always be 2. the above is just an example. Thanks

Isaac Pounder
  • 177
  • 1
  • 4
  • 14

1 Answers1

4

Just change the SET NOCOUNT statement to view the lines that really matter:

example:

set nocount off

declare @t table (a int)
insert into @t 
values(1)

set nocount on

declare @MyTable table (b int)

insert into @MyTable
select * from @t

See the Messages tab in SSMS.

Gustavo F
  • 2,071
  • 13
  • 23
  • 1
    +1, but the OP wants the affected records of the last statement, not the first. So first `nocount on` and latter `nocount off`. http://stackoverflow.com/questions/1483732/set-nocount-on-usage – Tim Schmelter Jan 19 '12 at 07:52