I need to select into a variable using a column name that is also stored in a variable, but it just selects the value of the column name instead.
The code below loops through all of the columns for a table and finds the old and new value of each column (part of a TRIGGER). The problem is, instead of assigning the old and new value to the variables, it assigns the value of the @fieldname variable.
Set @getColumnName = Cursor For
Select Column_name From Information_Schema.Columns Where Table_Name = @tablename
Open @getColumnName
Fetch Next From @getColumnName Into @fieldname
While @@FETCH_STATUS = 0
Begin
Select @oldValue = @fieldname From deleted
Select @newValue = @fieldname From inserted
-- Do something with the values here
-- @oldValue and @newValue are now equal to @fieldname
-- Actually want them to be the result of the query
End
Close @getColumnName
COLUMNS_UPDATEDin triggers. msdn.microsoft.com/en-us/library/ms186329.aspx