I'm writting a big procedure in T-SQL with dynamic elements. To make it short for you I only post the important codelines.
declare....
BEGIN
while @start <= @rowcount
begin
print @start
print @rowcount
set @sqlval = 'select @zwischen1 = @currentcolumn_val1
from z_skm
where id = @startid';
set @ParmDefinitionval = N'@currentcolumn_val1 nvarchar(500),
@startid int,
@zwischen1 nvarchar(50) OUTPUT';
exec sp_executesql
@sqlval,
@ParmDefinitionval,
@currentcolumn_val1 = @currentcolumn_val,
@startid = @start,
@zwischen1 = @currentval OUTPUT;
print @currentval
set @sqlkey ='select @zwischen2 = @currentcolumn_key1
from z_skm
where id = @startid';
set @ParmDefinitionkey = N'@Currentcolumn_key1 nvarchar(500),
@startid int,
@zwischen2 nvarchar(50) OUTPUT';
exec sp_executesql
@sqlkey,
@ParmDefinitionkey,
@Currentcolumn_key1 = @currentcolumn_key,
@startid = @start,
@zwischen2 = @currentkey OUTPUT;
print @currentkey
set @start = @start + 1
print'loop-end'
end
END
GO
My problem is, that it always prints the same values. And the values are the ones which are passed at @ParmDefinitionKey
and @parmdefinitionval
into @currentcolumn_key1
and @Currentcolumn_val1
.
I hope you guys can help out.
Edit:
Now it works. Here is the change in the code :
set @sqlval = 'select @zwischen1 = '+@currentcolumn_val+' from z_skm where id = @startid';
SET @ParmDefinitionval = N'@startid int,@zwischen1 nvarchar(50) OUTPUT' ;
exec sp_executesql @sqlval,@ParmDefinitionval,@startid=@start,@zwischen1=@currentval OUTPUT;
set @sqlkey ='select @zwischen2 = '+@currentcolumn_key+' from z_skm where id = @startid';
set @ParmDefinitionkey=N'@startid int, @zwischen2 nvarchar(50) OUTPUT';
exec sp_executesql @sqlkey,@ParmDefinitionkey,@startid=@start,@zwischen2=@currentkey OUTPUT;
I dont know why, but it looks like that it is not possible to pass the dynamic_column via argument into the execute, it needed to placed into the string before.. This made absolutely no sense for me.