我有一个SQL查询,它应该提取一条记录并将每个连接成一个字符串,然后输出该字符串。查询的重要部分如下所示:
DECLARE @counter int;
SET @counter = 1;
DECLARE @tempID varchar(50);
SET @tempID = '';
DECLARE @tempCat varchar(255);
SET @tempCat = '';
DECLARE @tempCatString varchar(5000);
SET @tempCatString = '';
WHILE @counter <= @tempCount
BEGIN
SET @tempID = (
SELECT [Val]
FROM #vals
WHERE [ID] = @counter);
SET @tempCat = (SELECT [Description] FROM [Categories] WHERE [ID] = @tempID);
print @tempCat;
SET @tempCatString = @tempCatString + '<br/>' + @tempCat;
SET @counter = @counter + 1;
END
当脚本运行时,
@tempCatString
的输出为 null,而 @tempCat
总是能够正确输出。在 While 循环内部进行字符串拼接有什么原因导致无法正常工作吗?这似乎是不正确的,因为递增 @counter
可以完美地工作。那么我还有其他遗漏的地方吗?