Using object explorer in SSMS, find the table in question and expand the node so you see the Columns, Keys, Constraints, Triggers, Indexes, and Statistics
Drag the Columns folder into the query window pane. This results in a column-space delimited list of the columns in a single line.
We're going to use a regular expression to transform the data into the desired format.
Things to know.
\n
is a newline/carriage return/enter
^
matches the beginning of a line
.
matches any thing one time
*
repeats the match as long as it can go
I will tag the word we match so that we can do something with it. This comes via the braces {}
. As things get tagged in braces, our reference to them by ordinal increases. Here, we'll only use one so /1
will suffice.
Regular expressions are greedy, that means it will match as much as possible which is not what we will want. Unfortunately, SSMS only supports greedy matches so we will have to make multiple passes
(note the trailing space the above)
- Theoretical Replace with:
\n, T1.\1 = T2.\1
Round 1
In this step, we are going to force all of our columns to exist on a single line
Important Note: One trailing space is included in the Find what
- Find what:
,
- Replace with:
\n
- Look in:
Current Document
- Use:
Regular expressions
Start
environment_id, environment_name, folder_id, description, created_by_sid, created_by_name, created_time
Result
environment_id
environment_name
folder_id
description
created_by_sid
created_by_name
created_time
We have now taken all of our columns and flattened them against the left margin.
Round 2
We are going to take all the words and wrap them with T1 = T2
I am going to make my output look similar to yours except I am a leading comma person.
- Find what:
^{.*}
- Replace with:
, T1.\1 = T2.\1
- Look in:
Current Document
- Use:
Regular expressions
Start
environment_id
environment_name
folder_id
description
created_by_sid
created_by_name
created_time
Result
, T1.environment_id = T2.environment_id
, T1.environment_name = T2.environment_name
, T1.folder_id = T2.folder_id
, T1.description = T2.description
, T1.created_by_sid = T2.created_by_sid
, T1.created_by_name = T2.created_by_name
, T1.created_time = T2.created_time
Now you have a nice, almost clean column translation list. Trim the first comma and slap that into your UPDATE statement