Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
pvccaz
Yak Posting Veteran
87 Posts |
Posted - 2013-08-19 : 19:34:08
|
Hi,I have a table called ListTable that has a list of table names, column names and Values.Example of the ListTable:TABLE_NAME COLUMN_NAME ValueTable1 Column1_email Test@email.comTable1 Column2_email Test@email.comTable2 Column1_firstname SteveFNameTable2 Column2_LastName TurnerLNameI would like to generate a sql statement that reads the ListTable and generates an update statement using the columns TABLE_NAME, COLUMN_NAME and Value also executes it.Basically it should generate the below statements and execute it.UPDATE Table1 SET Column1_email = 'Test@email.com'UPDATE Table1 SET Column2_email = 'Test@email.com'UPDATE Table2 SET Column1_firstname = 'SteveFName'UPDATE Table2 SET Column2_lastname = 'TurnerLName'
Is there any dynamic sql method that could be used to acomplish this ?Any help would be much appreicated. |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-08-19 : 21:03:12
|
quote: Originally posted by pvccaz Hi,I have a table called ListTable that has a list of table names, column names and Values.Example of the ListTable:TABLE_NAME COLUMN_NAME ValueTable1 Column1_email Test@email.comTable1 Column2_email Test@email.comTable2 Column1_firstname SteveFNameTable2 Column2_LastName TurnerLNameI would like to generate a sql statement that reads the ListTable and generates an update statement using the columns TABLE_NAME, COLUMN_NAME and Value also executes it.Basically it should generate the below statements and execute it.UPDATE Table1 SET Column1_email = 'Test@email.com'UPDATE Table1 SET Column2_email = 'Test@email.com'UPDATE Table2 SET Column1_firstname = 'SteveFName'UPDATE Table2 SET Column2_lastname = 'TurnerLName'
Is there any dynamic sql method that could be used to acomplish this ?Any help would be much appreicated.
You could generate the dynamic SQL like this:SELECT 'UPDATE ' + TABLE_NAME + ' SET ' + COLUMN_NAME + ' = ' + VALUE + ';'FROM YourTable; You can copy the query above, run it, copy the output to an SSMS window and then run that.You can also put it into dynamic SQL string and execute that string. But, dynamic SQL is very vulnerable to SQL injection, so be very careful if you do use it. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2013-08-20 : 09:03:18
|
There should be single quotes around the values. So the slightly changed code isSELECT 'UPDATE ' + TABLE_NAME + ' SET ' + COLUMN_NAME + ' = ''' + VALUE + ''';'FROM YourTable; MadhivananFailing to plan is Planning to fail |
 |
|
pvccaz
Yak Posting Veteran
87 Posts |
Posted - 2013-08-20 : 14:26:57
|
Thank you James and Madhivanan !I would like to be able to run dynamically the query without copying to SSMS. So, I modified the query as follows:DECLARE @sql nvarchar(MAX)SET @sql = (SELECT 'UPDATE ' + TABLE_NAME + ' SET ' + COLUMN_NAME + ' = ''' + VALUE + ''';'FROM ListTable)EXEC sp_executesql @sqlGOI am getting an error message :Msg 512, Level 16, State 1, Line 2Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.There are multiple records in the ouput - but they need to be executed. I am not sure how to fix this. Thanks |
 |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-08-20 : 14:32:01
|
quote: Originally posted by pvccaz Thank you James and Madhivanan !I would like to be able to run dynamically the query without copying to SSMS. So, I modified the query as follows:DECLARE @sql nvarchar(MAX)SET @sql = (SELECT 'UPDATE ' + TABLE_NAME + ' SET ' + COLUMN_NAME + ' = ''' + VALUE + ''';'FROM ListTable)EXEC sp_executesql @sqlGOI am getting an error message :Msg 512, Level 16, State 1, Line 2Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.There are multiple records in the ouput - but they need to be executed. I am not sure how to fix this. Thanks
Concatenate the rows into one string like this:DECLARE @sql nvarchar(MAX)SET @sql = (SELECT 'UPDATE ' + TABLE_NAME + ' SET ' + COLUMN_NAME + ' = ''' + VALUE + ''';'FROM ListTable FOR XML PATH(''))EXEC sp_executesql @sqlGO |
 |
|
pvccaz
Yak Posting Veteran
87 Posts |
Posted - 2013-08-20 : 14:38:47
|
That's perfect !! Thank you very much James. |
 |
|
|
|
|
|
|