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 |
|
wotrac
Yak Posting Veteran
98 Posts |
Posted - 2002-08-03 : 10:11:03
|
| I am trying to create a dynamic update statement, but am having a little problem.The standard statement would be.UPDATE SL_ACCOUNTS SET CUTURNOVR_L7 = CUTURNOVR_LT - 200 WHERE CUCODE = 'MYCODE'What I am trying to do is substitute L with @year and 7 with @periodAs these values will differ depending a number of select statements earler on in my script.This is what I tried, but the Syntax is not correctUPDATE SL_ACCOUNTS SET CUTURNOVR_ + @year + @period = (CUTURNOVR_ + @year + @period) - 200 WHERE CUCODE = 'MYCODE'@year and @period are set to VARCHAR(2)but the CUTURNOVR_L7 field etc are integer fieldsCan anyone point me in the right directionPaul |
|
|
Nazim
A custom title
1408 Posts |
Posted - 2002-08-03 : 11:04:45
|
you can achieve this by using Dynamic Sql . this should teach you how to do it http://www.sqlteam.com/item.asp?ItemID=4619 .Anywayz , for your current prob . this should help you.declare @sql varchar(500)@sql='UPDATE SL_ACCOUNTS SET CUTURNOVR_ '+ @year + @period +'= (CUTURNOVR_' +@year + @period +' ) - 200 WHERE CUCODE = ''MYCODE'' 'exec (@sql)HTHEDIT: Another week without a Title -------------------------What lies behind you and what lies ahead of you are small matters compared to what lies within you.-Ralph Waldo EmersonEdited by - Nazim on 08/03/2002 11:06:14 |
 |
|
|
wotrac
Yak Posting Veteran
98 Posts |
Posted - 2002-08-03 : 15:23:00
|
| Hi NazimIgnore my earlier message.I am still getting a syntax problem with the code you suggested.declare @sql varchar(500) @sql='UPDATE SL_ACCOUNTS SET CUTURNOVR_ '+ @year + @period +'= (CUTURNOVR_' +@year + @period +' ) - 200 WHERE CUCODE = '@oldAccnt'' exec (@sql) Syntax error near '='Paul |
 |
|
|
LarsG
Constraint Violating Yak Guru
284 Posts |
Posted - 2002-08-03 : 15:33:00
|
| declare @sql varchar(500) @sql='UPDATE SL_ACCOUNTS SET CUTURNOVR_ '+ @year + @period +'= (CUTURNOVR_' +@year + @period +' ) - 200 WHERE CUCODE = ''' + @oldAccnt + ''' exec (@sql) This assumes that @oldAccnt is declared as varchar |
 |
|
|
|
|
|