on 10-14-2015 11:30 AM
When doing a merge statement we'd like to log the number of rows inserted and the number of rows updated.
At present we do this using 2 separate statements so we know from @@rowcount how many rows each one has operated on.
Is there anyway of getting the individual counts from a merge statement ?
Thanks
Interesting question. Here is one way I can think to do it:
declare @minserts int, @mupdates int
set @minserts = 0
set @mupdates = 0
merge into myhist as h
using mytrans as t
on h.pkey = t.pkey
when matched then update set h.description = t.description, @mupdates = @mupdates + 1
when not matched then insert (pkey,description) values (t.pkey,t.description)
select @minserts = @@rowcount - @mupdates
print "UPDATES: %1! INSERTS: %2!",@mupdates,@minserts
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
24 | |
12 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.