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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Using Variable Phrases in UPDATE Statements

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-05-20 : 23:02:49
Michael writes "On Windows NT 4.0 SP5 and Windows 2000, using SQL Server 2000, I'm trying to extract the rules for updating records from Transact-SQL and put them into tables so that they can be more easily changed by non-programmers. The table has a Condition field and a Result field, and I am trying to use the fields as variables in an update statement, but it gives a syntax error because it will not translate the variables into what is inside them before processing the update. Update is trying to change the variables themselves.

For example (in the first row in the table of 20 rows), say the Condition field contains: (ProdCode = '111') OR (ProdCode = '222') and the Result field contains: RegionCode = 'AA'. I would first read the fields into local variables (with a cursor), and then use these variables in the update statement. For example:

DECLARE @Condition char(100), @Results char(100)
DECLARE c CURSOR FOR
SELECT Condition, Result
FROM ConversionTable

OPEN c
FETCH c INTO @Condition, @Result

UPDATE NewTable
SET @Result
WHERE @Condition

CLOSE c
DEALLOCATE c

I would like it to translate into

UPDATE NewTable
SET RegionCode = 'AA'
WHERE (ProdCode = '111') OR (ProdCode = '222')

but instead, it gives a syntax error because it is trying to set the variable @Result to something else (and the WHERE @Condition would have the same problem).

Does anyone know how to get around this?"
   

- Advertisement -