T-SQL While 循环和串联
我有一个 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
效果很好。 那么我还缺少其他东西吗?
I have a SQL query that is supposed to pull out a record and concat each to a string, then output that string. The important part of the query is below.
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
When the script runs, @tempCatString
outputs as null while @tempCat
always outputs correctly. Is there some reason that concatenation won't work inside a While loop? That seems wrong, since incrementing @counter
works perfectly. So is there something else I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来它应该可以工作,但出于某种原因,它似乎认为 @tempCatString 为 null,这就是为什么您总是得到一个 null 值,因为 null 连接到其他任何内容仍然为 null。 建议您尝试对每个变量使用
COALESCE()
,如果它们为空,则将它们设置为“”。Looks like it should work but for somereason it seems to think @tempCatString is null which is why you are always getting a null value as nullconcatenated to anything else is still null. Suggest you try with
COALESCE()
on each of the variables to set them to " " if they are null.这会更有效......
还要看看 concat_null_yields_null 作为解决串联问题的选项,尽管我会避免这条路线
this would be more efficient....
also look at concat_null_yields_null as an option to fix your concatenation issue, although I would avoid that route
我同意 keithwarren 的观点,但我始终确保在查询中添加 ORDER BY 子句。 然后,您可以确定这些值的连接顺序。
此外,用 '' 替换 NULL 值的 COALESCE 将有效地产生空白行。 我不知道你是否想要它们,但如果不只是在 WHERE 子句中进行过滤...
最后,你似乎有一个临时表,其中包含你感兴趣的 ID。该表可以包含在用于过滤源表的 JOIN...
I agree with keithwarren, but I would always be sure to add an ORDER BY clause to the query. You can then be sure as to exactly what order the values are being concatenated in.
Also, the COALESCE to replace the NULL value with '' will effectively yield blank rows. I don't know if you want them or not, but if not just filter in the WHERE clause instead...
Finally, you appear to have a temp table including the IDs you're interested in. This table can just be included in a JOIN to filter the source table...