当我运行第一个查询时,我得到5行结果。 row1的值是“Day1”,第2行是“Day2”等等......之后我想将它组合成一个字符串“Day1,Day2,Day3,Day4,Day5”。但是在运行存储过程之后,我得到了空的结果。下面是我的存储过程。我该如何解决?感谢
SET NOCOUNT ON
Declare @PeriodID nvarchar(50);
Declare @FinalString nvarchar(80);
DECLARE vendor_cursor CURSOR FOR
SELECT PeriodID FROM PeriodTable c WHERE c.Active=1
OPEN vendor_cursor
FETCH NEXT FROM vendor_cursor
INTO @PeriodID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @FinalString = @FinalString + @PeriodID + ','
FETCH NEXT FROM vendor_cursor
INTO @PeriodID
END
CLOSE vendor_cursor;
DEALLOCATE vendor_cursor;
print @FinalString
SET NOCOUNT OFF
答案 0 :(得分:2)
不需要游标。我认为您错过了首先将变量设置为空字符串,因此它为null,使每行为空。这有效:
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+', '+name
from ImagedApps
order by name
-- take off the first comma and space
select substring(@ret, 3, 8000)