I'm trying to execute an update statement in a stored procedure that includes a variable for a column and a variable for a conditional constraint. For example I want to execute the following UPDATE, but using variables:
Update table set flagcode = 'A' where Field1 < 100
the field flagcode is a varchar and the field Field1 is an int
This basically how I have my code set up:
declare @flag as varchar(20)
declare @lowrange as varchar(20)
set @flag = 'A'
set @thefield = 'Field1'
exec('update table set Flagcode = ' + @flag + ' where '+@thefield+' < 100')
I get the error:
"Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'A'."