Search code examples
sqlsql-serverdynamic-columns

Mapping dynamic column names in sql server


DECLARE @step VARCHAR(25) = '15';
DECLARE @COL VARCHAR(50) = step_col_@step;
UPDATE table_tblName SET @COL=<some value> WHERE <condition>

I have the above query where @COL is supposed to be dynamic i.e. step_col_1,step_col_2,step_col_3......step_col_18. How can I achieve this without If-else condition in sql server?


Solution

  • Try the below query,

    DECLARE @COL VARCHAR(50) = 'step_col_'+@step