insert query via connection.execute(sql,records) returning a record count even though query doesn't insert records

bgrushin

New Member
Joined
Oct 26, 2011
Messages
10
Hi All,

I have a process that gathers data and inserts into 3 tables via 3 insert queries in a ms sql server database. Recently the user made a mistake and left in data that was already in the database, which is a primary key violation and will fail the query. All of the queries are wrapped in

Rich (BB code):
begin transaction [Tran1]
begin try
...sql here...
commit transaction [Tran1]
end try begin catch rollback transaction [Tran1] end catch

code to prevent some records updating without others.

In the execute command I have a records affected variable parameter which since the query fails I expect to be 0 but its returning the number of records that were attempted to be inserted but weren't.

When I try to run the full query in SQL server I just get "Command(s) completed successfully." returned however I confirmed by checking that the query did not work.

What modification do I need to make to make the recordsaffected 0 in such a case? I use that check to either stop the process and throw an error or continue, so the process finished without the user realizing that records didn't go in.

Thanks in advance,
Boris
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I should add that I'm trying to avoid having to run a select statement after the fact to double check unless I absolutely have to due to either a bug or software limitation.

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top