Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UPDATE SET COLUMN NAME

Status
Not open for further replies.

trenttc

Technical User
Feb 25, 2002
68
US
Is it possible to do an UPDATE SET @variable = something where the column name is a variable? I can't do a CREATE TABLE or ALTER TABLE for this particular problem.

SET @q = 'q'+ Quantity; @q becomes the column name q100
UPDATE table
SET @q = Price Update column q100 = Price

 
If your database allows it, you can use dynamic SQL. The following works in MS SQL Server. Other database systems may require different syntax. I recommend that you ask most questions in a specific database forum because of the varying implementations of the ANSI standard.

--Example:
Declare Qq varchar(12), @sql varchar(120)

--@q becomes the column name q100
SET @q = 'q'+ Quantity

--Create dynamic SQL
Select @sql='UPDATE table SET '+@q+'=Price'

--Execute statement
exec(@sql) Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
It works using Adaptive Server Anywhere. I just had to spell out execute immediate(@sql). Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top