In our code base I see a lot of SQL MERGE
statements that only perform an UPDATE
or only perform an INSERT
or only perform a DELETE
. I am having a hard time understanding why developers don't just write an insert or an update or a delete command.
What are the pros and cons of using merge statements that only execute one command?
From what I understand, merges are more problematic, if say a table's hash distribution column changes, or if a row gets updated more than once it will throw an exception (maybe a logic problem at that point, but will throw an exception nonetheless).
Example:
MERGE INTO dbo.my_dltd_recs AS dst
USING (SELECT *
FROM dbo.my_recs
WHERE dltd_ind = 'Y') src
ON (dst.my_skey = src.my_skey)
WHEN NOT MATCHED
-- only operation is INSERT, WHY USE MERGE
THEN INSERT (my_skey,
colA,
colB)
VALUES
( src.my_skey,
src.colA,
src.colB);