I am attempting to set variables whose names are dynamic in a stored procedure:
DECLARE @var01 varchar(50)
DECLARE @var02 varchar(50)
...
DECLARE @var30 varchar(50)
DECLARE @sql = varchar(max)
DECLARE @loopcnter INT
-- (Inside some loop where the loopcounter increments each iteration)
...
SET @sql = 'SET @var0'+CAST(@loopcntr AS Varchar)+'= '''+'somevalue'+''''
-- e.g.) SET @var01= 'somevale'
EXEC (@sql)
This doesn't work because the variables are declared in a different scope to that of the dynamic sql.
What is the correct way to dynamically set variables in this manner?
@@variables?