Sunday, March 11, 2012

"ON DUPLICATE KEY UPDATE"?

Hi,

I am migrating from MySQL to SQL Express. In MySQL, I import data from Oracle using the "INSERT INTO .... ON DUPLICATE KEY UPDATE". With the "ON DUPLICATE KEY UPDATE", when there is a duplicate key in MySQL exists, the respective records will be updated (so to avoid inserting the data twice). I cannot remove the duplicated rows in MySQL as it contains extra fields with user-input value.

Is there any way I can do the same in SQL Express? Is it a must that I need to create a temp table to hold the downloaded data and then update the duplicated data in the original before an INSERT?

Thanks

Raymond

There is no equivalent functionality. You can however do the following:

1. You can create an unique index on the key column(s) with the IGNORE_DUP_KEY option set to on. This will result in the duplicate rows being ignored during the INSERT operation for instance. You will have to however update the rows by inserting into another table and then performing update

2. Use a worktable approach where you insert all the rows into the worktable and then use insert/update as into the main table.

|||

Thanks,

I think I will go for the second option, as there is only a few new records (inserts) every time, and most the other records involve changes (updates).

Raymond

No comments:

Post a Comment