T-SQL While 循环和串联

发布于 2024-07-12 03:50:44 字数 797 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我还不会笑 2024-07-19 03:50:44

看起来它应该可以工作,但出于某种原因,它似乎认为 @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.

自演自醉 2024-07-19 03:50:44

这会更有效......

select @tempCatString = @tempCatString + Coalesce(Description,'') + '<br/>' from Categories...

select @fn

还要看看 concat_null_yields_null 作为解决串联问题的选项,尽管我会避免这条路线

this would be more efficient....

select @tempCatString = @tempCatString + Coalesce(Description,'') + '<br/>' from Categories...

select @fn

also look at concat_null_yields_null as an option to fix your concatenation issue, although I would avoid that route

舞袖。长 2024-07-19 03:50:44

我同意 keithwarren 的观点,但我始终确保在查询中添加 ORDER BY 子句。 然后,您可以确定这些值的连接顺序。

此外,用 '' 替换 NULL 值的 COALESCE 将有效地产生空白行。 我不知道你是否想要它们,但如果不只是在 WHERE 子句中进行过滤...

最后,你似乎有一个临时表,其中包含你感兴趣的 ID。该表可以包含在用于过滤源表的 JOIN...

DELCARE @output VARCHAR(8000)
SET @output = ''

SELECT
    @output = @output + [Categories].Description + '<br/>'
FROM
    Categories
INNER JOIN
    #vals
        ON #vals.val = [Categories].ID
WHERE
   [Categories].Description IS NOT NULL
ORDER BY
   [Categories].Description

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...

DELCARE @output VARCHAR(8000)
SET @output = ''

SELECT
    @output = @output + [Categories].Description + '<br/>'
FROM
    Categories
INNER JOIN
    #vals
        ON #vals.val = [Categories].ID
WHERE
   [Categories].Description IS NOT NULL
ORDER BY
   [Categories].Description
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文