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?
我有上面的查询,其中@COL应该是动态的,即step_col_1,step_col_2,step_col_3 ...... step_col_18。如果没有SQL Server中的If-else条件,我该如何实现呢?
4 个解决方案
#1
1
Try the below query,
尝试以下查询,
DECLARE @COL VARCHAR(50) = 'step_col_'+@step
#2
0
Pass the variables like this to form a dynamic sql query
传递这样的变量以形成动态sql查询
DECLARE @step VARCHAR(25) = '15';
DECLARE @COL VARCHAR(50) = 'step_col_'+@step
exec ('UPDATE table_tblName SET '+@COL+'=<some value> WHERE <condition>')
#3
0
DECLARE @step VARCHAR(25) = '15';
DECLARE @COL VARCHAR(50) = 'step_col_'+@step;
DECLARE @String VARCHAR(500) = ''
SET @String = 'UPDATE table_tblName SET '''+@COL+''' = <some value> WHERE
<condition>'
EXEC (@String)
#4
0
DECLARE @step VARCHAR(25) = '15';
DECLARE @COL VARCHAR(50)
SET @COL = 'step_col'+@step;
UPDATE table_tblName SET @COL=<some value> WHERE <condition>
#1
1
Try the below query,
尝试以下查询,
DECLARE @COL VARCHAR(50) = 'step_col_'+@step
#2
0
Pass the variables like this to form a dynamic sql query
传递这样的变量以形成动态sql查询
DECLARE @step VARCHAR(25) = '15';
DECLARE @COL VARCHAR(50) = 'step_col_'+@step
exec ('UPDATE table_tblName SET '+@COL+'=<some value> WHERE <condition>')
#3
0
DECLARE @step VARCHAR(25) = '15';
DECLARE @COL VARCHAR(50) = 'step_col_'+@step;
DECLARE @String VARCHAR(500) = ''
SET @String = 'UPDATE table_tblName SET '''+@COL+''' = <some value> WHERE
<condition>'
EXEC (@String)
#4
0
DECLARE @step VARCHAR(25) = '15';
DECLARE @COL VARCHAR(50)
SET @COL = 'step_col'+@step;
UPDATE table_tblName SET @COL=<some value> WHERE <condition>