Skip to content
Advertisement

Create an UPDATE statement for every row in a table

I want to update a table on one server with data from a table on another server. The tables have the same columns and I just want to sync the data. I have earlier made a script that creates insert statements for these tables (put out a question to SO and answered it myself…) but the seemingly simpler task of creating UPDATE statements now eludes me. Maybe because I’m just back from holiday.

For reference, here’s my “INSERT question”: Creating “Not Exists”/Insert Into Statements – Stumped by nothing (NULL)

I’m trying to use my INSERT statements code but I’m either not getting multiple rows or just getting hard-to-debug error messages (Error close to NULL)

So, what I want to do is creating statements like this, one for each row in the table:

I could use some general pointers like "Since you're now creating UPDATE statements instead of INSERT you need to think about..."

This is a table used for integration of several systems. We “collect” messages we get from other systems and record them in ErrorDescription. If it’s too long or misleading we put a shorter explanation in Errortext to show for the user. (in this example I happened to use the same text, “error creating email”, but in the real system the error description we get is much longer)

One try is a code like this:


Trying to execute @SQL_KOD gives: “Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword ‘NULL’.”

From test run, with EXECUTE (@SQL_KOD) commented out:

Advertisement

Answer

This seems to fit the bill. Qutation marks in data are now correctly doubled and the end result is one UPDATE statement for each row in the table, as I wanted. As I mentioned, reading from information_schema.columns is unnecessary when I’m here just working with one known column, but maybe when I’m modyfying this script for another table it’s not so…

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement