SQL Concat行到变量

时间:2018-07-19 07:47:14

标签: sql sql-server concat

我需要构建一个函数,该函数返回包含表中值的字符串。因此,我声明了一个变量NVARCHAR,并使用 concat 使用以下代码将每一行添加到字符串中。

DECLARE @Comment AS NVARCHAR(max) = ''

SELECT @Comment =
    @Comment + 
    CONCAT (
        replace(space(100), N' ', N'-')
        ,CHAR(13)
        ,convert(NVARCHAR(100), T.DT, 103)
        ,N' '
        ,convert(NVARCHAR(5), T.DT, 114)
        ,N' - '
        ,isnull(URESP.N_UTIL + N' ' + URESP.PRE_UTIL, 'System')
        ,N' : '
        ,isnull(TA.L_TACTION, T.ACT)
        ,CHAR(13)
        ,isnull(T.TXT , N' ')
        ,CHAR(13)
        )
FROM (
    SELECT D_CREATION DT
        ,'Commentaire' ACT
        ,I_C_UTIL_CREA C_UTIL
        ,L_COMMENT TXT
    FROM ACTIONS
    WHERE NO_APPEL = 106984 and C_TACTION = 'I_CR_INT'
    ) T
LEFT JOIN UTILISATEUR URESP ON URESP.C_UTIL = T.C_UTIL
LEFT JOIN TACTION TA ON TA.C_TACTION = ACT
ORDER BY DT desc

PRINT @Comment

输出:

----------------------------------------------------------------------------------------------------
19/07/2018 08:46 - ROCH Charly : Commentaire
test

我不明白为什么字符串只包含第一行。而查询下

SELECT T.* FROM (
    SELECT D_CREATION DT
        ,'Commentaire' ACT
        ,I_C_UTIL_CREA C_UTIL
        ,L_COMMENT TXT
    FROM ACTIONS
    WHERE NO_APPEL = 106984 and C_TACTION = 'I_CR_INT'
    ) T
LEFT JOIN UTILISATEUR URESP ON URESP.C_UTIL = T.C_UTIL
LEFT JOIN TACTION TA ON TA.C_TACTION = ACT
ORDER BY DT desc

返回2行。

DT                      ACT         C_UTIL  TXT
2018-07-19 08:50:41.470 Commentaire 14254   test2
2018-07-19 08:46:51.240 Commentaire 14254   test

3 个答案:

答案 0 :(得分:2)

您确定所有表的内容都正确吗? 当我测试类似的查询时,它工作正常。 示例:

create table COM (uid integer, comment varchar(50));
insert into COM values(1, 'Com 1');
insert into COM values(2, 'Com 2');
insert into COM values(2, 'Com 3');

create table UID (uid integer, name varchar(50));
insert into UID values(1, 'User 1');
insert into UID values(2, 'User 2');
insert into UID values(3, 'User 3');

declare @test as nvarchar(500) = ''
select @test = concat(@test, ' ', COM.uid, ' ', COM.comment, ' ', UID.name, ' | ')
from COM, UID
where COM.uid = UID.uid and UID.uid = 2
select RTRIM(SUBSTRING(RTRIM(@test), 1, LEN(@test)-1))

这给了我结果:

2 Com 2 User 2 | 2 Com 3 User 2

答案 1 :(得分:1)

Concat函数不是聚合函数,它将您传递的尽可能多的字符串作为参数而不是行进行连接。

https://docs.microsoft.com/en-us/sql/t-sql/functions/concat-transact-sql?view=sql-server-2017

要连接行,必须使用string_agg函数。

https://docs.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-2017

但这仅自SQL Server 2017起可用。如果使用的是旧版本,则必须使用此处说明的技巧。

How to concatenate text from multiple rows into a single text string in SQL server?

答案 2 :(得分:0)

感谢您的回答,我也自己得到了一个答案。不确定这是否是最好的方法,但目前仍然有效。

DECLARE @Comment AS NVARCHAR(max) = ''

    SELECT @Comment =
    --  Ligne de séparation des actions 
    @Comment + T.TXT
FROM (
    SELECT CONCAT (
            replace(space(100), N' ', N'-')
            ,CHAR(13)
            -- Date et heure de création de l'action  
            ,convert(NVARCHAR(100), A.D_CREATION, 103)
            ,N' '
            ,convert(NVARCHAR(5), A.D_CREATION, 114)
            --Utilisateur qui crée l'action : nom de l'action (L_TACTION) et à défaut le code de l'action
            ,N' - '
            ,isnull(URESP.N_UTIL + N' ' + URESP.PRE_UTIL, 'System')
            ,N' : '
            ,'Commentaire'
            ,CHAR(13)
            ,isnull(A.L_COMMENT, N' ')
            ,CHAR(13)
            ) TXT
    FROM ACTIONS A
    LEFT JOIN UTILISATEUR URESP ON URESP.C_UTIL = A.C_UTIL
    LEFT JOIN TACTION TA ON TA.C_TACTION = A.C_TACTION
    WHERE NO_APPEL = 106984
        AND A.C_TACTION = 'I_CR_INT'
    ) T
    RETURN @Comment

我现在正在构建一个表,其中包含已经格式化为字符串的数据。所以我只需要将所有行放在一起就可以了。