I have an insert statement:
insert into parentTbl
select firstId, secondId, thirdId, dateTm
from importTbl
where codeId = @codeIdParam
I need to reliably find out if that insert inserted anything at all. Ideally, I would like to set a @insertedCount variable to the number of rows inserted, even if that is 0.
I am currently using:
set @insertedCount = @@ROWCOUNT
But that only seems to get the last number of inserted rows – the problem is that if the INSERT SELECT statement did not insert anything the @@ROWCOUNT does not return 0.
You could try using an
OUTPUTclause, which would return one row per inserted row; something like:This would give you a resultset with the
firstIds of every inserted row, which you could then run a count on.One way would be to
output intoa table-var, and then do aselect count(*) from @tableVarat the end to get your insert-count.