Given following snippet (MS SQL):
DECLARE UpdateList CURSOR FOR
SELECT MyColumn FROM MyTable
OPEN UpdateList
Nothing fancy so far. Now I would like to declare two variables where I can write the column's and table's name into. Following, of course, wouldn't work. How can I achieve this?
DECLARE @TableName nchar(20) = 'MyTable'
DECLARE @ColumnName nchar(20) = 'MyColumn'
DECLARE UpdateList CURSOR FOR
SELECT @ColumnName FROM @TableName
OPEN UpdateList
Thx for any tipps sl3dg3
This code is a very good sample for dynamic column with cursor, since you can not use '+' in @statement.
You'll have to use Dynamic SQL - you can't use parameters as table or column names. So something like:
Please keep in mind the security and performance issues associated with dynamic SQL - I don't know how you'll be populating the variables, here, and there can be some definite danger in doing this.
EDIT: Added full code.