SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Dynamic sql
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pvccaz
Yak Posting Veteran

USA
87 Posts

Posted - 08/19/2013 :  19:34:08  Show Profile  Reply with Quote
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 Value
Table1 Column1_email Test@email.com
Table1 Column2_email Test@email.com
Table2 Column1_firstname SteveFName
Table2 Column2_LastName TurnerLName

I 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
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 08/19/2013 :  21:03:12  Show Profile  Reply with Quote
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 Value
Table1 Column1_email Test@email.com
Table1 Column2_email Test@email.com
Table2 Column1_firstname SteveFName
Table2 Column2_LastName TurnerLName

I 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.
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 08/20/2013 :  09:03:18  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
There should be single quotes around the values. So the slightly changed code is
SELECT
	'UPDATE ' + TABLE_NAME + ' SET ' +
	COLUMN_NAME + ' = ''' + VALUE + ''';'
FROM
	YourTable;


Madhivanan

Failing to plan is Planning to fail

Edited by - madhivanan on 08/20/2013 09:04:05
Go to Top of Page

pvccaz
Yak Posting Veteran

USA
87 Posts

Posted - 08/20/2013 :  14:26:57  Show Profile  Reply with Quote
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 @sql
GO

I am getting an error message :
Msg 512, Level 16, State 1, Line 2
Subquery 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
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 08/20/2013 :  14:32:01  Show Profile  Reply with Quote
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 @sql
GO

I am getting an error message :
Msg 512, Level 16, State 1, Line 2
Subquery 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 @sql
GO
Go to Top of Page

pvccaz
Yak Posting Veteran

USA
87 Posts

Posted - 08/20/2013 :  14:38:47  Show Profile  Reply with Quote
That's perfect !! Thank you very much James.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000