如何将循环 sp_executesql 输出连接到变量中

how can I concatenate looped sp_executesql output into a variable

我使用下面的过程,试图将返回的 itemstr 连接到变量 strlist,值之间用逗号分隔。我在外观的每次迭代中都获得了单独的 itemstr 值,但 strlist 始终为 null。

   alter procedure my_test

   as 

   declare @counter int
   declare @columnstr varchar(100)
   declare @itemstr varchar(100)
   declare @strlist varchar(1000)

   declare @sqlstr nvarchar(1000)

   set @counter = 1


   while @counter < 15
   begin
     set @columnstr = 'txt_item' + ltrim(cast(@counter as varchar(2)))
     --select @columnstr as columnstr

     set @sqlstr = 'select @outstr = ' + @columnstr + ' from group1_ where id = ''298'''
     --select @sqlstr

   execute sp_executesql @sqlstr,N'@outstr varchar(100) OUTPUT',   @outstr=@itemstr OUTPUT;
   select @itemstr as itemstr

    set @strlist = @strlist + @itemstr + ','
    select @strlist as strlist  -- this is always null

    set @counter = @counter + 1
   end
   select @strlist     -- this is always null
   go

尝试确保字符串在创建时不为空

或者

 declare @strlist varchar(1000) = ''

set @strlist = ISNULL(@strlist,'') + @itemstr + ','