|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, April 12, 2013 5:50 AM
Points: 33,
Visits: 234
|
|
This is good stuff... exactly what I need to troubleshoot a strange unique key violation with a MERGE.
Question though- why is the IDENTITY column of the type an INT, while the @single_update_id param to the proc is a BIGINT? I am guess the two datatypes should match, depending on the size needed for the particular application.
Thanks!
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, August 07, 2012 11:11 AM
Points: 7,
Visits: 104
|
|
Matthew-399425 (11/4/2010)
Question though- why is the IDENTITY column of the type an INT, while the @single_update_id param to the proc is a BIGINT? I am guess the two datatypes should match, depending on the size needed for the particular application.
You're right, the datatypes should match and sizing depends on the application.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, August 07, 2012 11:11 AM
Points: 7,
Visits: 104
|
|
Something else I recently learned that I left out of the article for clarity is about duplicate keys in the source data.
MERGE gets broken horribly if there are duplicate alternate keys in the <table_source> i.e. rows with the same customer_id in the @CustomerUpdates UDDT.
A validation step to detect / remove any duplicates before the merge statement can help catch otherwise hard to detect errors.
HTH
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, May 03, 2013 12:27 PM
Points: 21,
Visits: 42
|
|
This is a great post and a great demo of recursion capabilities in T-SQL/SPs. My current issue is the duplicate key/ID in the source data. What I am trying to do is get the recursive call to just skip to the next highest ID, but the MERGE OUTPUT apparently does not contain anything at all if an error occurs so I can't get the MAX(ID) from the output. In addition, I would rather not have to keep creating UDDTs and passing TVPs for every MERGE I need to do.
*sigh* It seems like another bright idea that Microsoft didn't implement very well (see postings online about duplicate key handling and other features of other RDBMSes).
I need single row error handling and logging, so I am going back to trusty cursors that loop through the IDs and then do a MERGE on each record processed. FYI, cursors perform great when the cursor declare only processes indexed IDs. Then you do the work retrieving other values inside the cursor loop.
|
|
|
|