I have a requirement where a set of variables are already initialized to some default values and are being used for other purposes. Now I am trying to pass a few variables dynamically, usually will query the variables from another table and pass to a select statement.
I do not want to use all the variables now. The number of parameters may vary from time to time.
When I try to implement in the below manner I am getting variable not defined.
Below is the sample code snippet which resembles the logic.
DECLARE @i_int1 INTEGER = 1
,@i_int2 INTEGER = 2
,@i_int3 INTEGER = 3
,@i_int4 INTEGER = 4
,@i_int5 INTEGER = 5
,@i_int6 INTEGER = 6
,@sql NVARCHAR(max) = ''
,@vcOutput NVARCHAR(max) = ''
BEGIN
--set @vcOutput = CAST(@i_int1 AS nvarchar(MAX)) +','+ CAST(@i_int2 AS nvarchar(MAX))
SET @vcOutput = '@i_int1,@i_int2'
SET @sql = 'select ' + @vcOutput
EXECUTE sp_executesql @sql;
END