带有UNION Query的SQL STUFF()返回NULL

时间:2014-03-18 01:48:36

标签: sql sql-server tsql sql-server-2012 sqlxml

我有一个SQL Server 2012查询返回1行数据,BUT可以根据查询返回多行。

我想将以下数据转换为csv连接字符串。我的查询使用STUFF()UNION查询,该查询基于this Stack Overflow question

即:我的数据就是这个

MR
------
NS
------

并将成为:

MR, NS

当我选择没有STUFF()部分查询的数据时,它返回数据没问题。

完整查询(见下文)运行时没有错误但总是返回NULL,即使查询的SELECT UNION部分返回数据。

我的完整查询如下所示:

SELECT STUFF((Select * FROM (
SELECT ISNULL(scddisc.abbrev, '!!') AS medicalDisciplines
FROM personenc INNER JOIN contacts ON personenc.cntid = contacts.cntid INNER JOIN scddisc ON contacts.discipline = scddisc.disid
WHERE (personenc.perid = 15410) AND (scddisc.active = 1) AND (contacts.active = 1) AND (scddisc.medgroup = 1) --AND (personenc.sdate BETWEEN '2014-03-05' AND '2014-03-12')
UNION 
SELECT ISNULL(scddisc.abbrev, '!!') AS medicalDisciplines
FROM scddisc INNER JOIN contacts ON scddisc.disid = contacts.discipline INNER JOIN personenc ON contacts.cntid = personenc.ocntid
WHERE (personenc.perid = 15410) AND (scddisc.active = 1) AND (contacts.active = 1) AND (scddisc.medgroup = 1) -- AND (personenc.sdate BETWEEN '2014-03-05' AND '2014-03-12')
) AS k
FOR XML PATH(''),TYPE).value('text()[1]','nvarchar(max)'),1,2,'')

有谁能让我知道我做错了什么?

有效的查询是上述查询的SELECT UNION部分 即,当我运行下面的查询时,它返回有效数据。

SELECT ISNULL(scddisc.abbrev, '!!') AS medicalDisciplines
    FROM personenc INNER JOIN contacts ON personenc.cntid = contacts.cntid INNER JOIN scddisc ON contacts.discipline = scddisc.disid
    WHERE (personenc.perid = 15410) AND (scddisc.active = 1) AND (contacts.active = 1) AND (scddisc.medgroup = 1) --AND (personenc.sdate BETWEEN '2014-03-05' AND '2014-03-12')
    UNION 
    SELECT ISNULL(scddisc.abbrev, '!!') AS medicalDisciplines
    FROM scddisc INNER JOIN contacts ON scddisc.disid = contacts.discipline INNER JOIN personenc ON contacts.cntid = personenc.ocntid
    WHERE (personenc.perid = 15410) AND (scddisc.active = 1) AND (contacts.active = 1) AND (scddisc.medgroup = 1) -- AND (personenc.sdate BETWEEN '2014-03-05' AND '2014-03-12')

但是,当与FOR XMLSTUFF结合使用时,则完整语句会返回NULL

2 个答案:

答案 0 :(得分:2)

有趣的是,我发现了这个问题。它似乎是2倍......

  • 看来.value('text()[1]','nvarchar(max)'),1,2,'') STUFF查询的一部分是未显示数据的部分。

    当我将text()更改为..value('.[1]','nvarchar(max)'),1,2,'')查询返回为 预期

  • 其次,我忘记将N', ' +添加到ISNULL(scddisc.abbrev, '!!') AS medicalDisciplines,以便根据需要将其设为逗号分隔列表。

因此,整个新陈述成了:

SELECT
    STUFF (
        (
            SELECT
                *
            FROM
                (
                    SELECT
                        N', ' + ISNULL(scddisc.abbrev, '!!') AS medicalDisciplines
                    FROM
                        personenc
                    INNER JOIN contacts ON personenc.cntid = contacts.cntid
                    INNER JOIN scddisc ON contacts.discipline = scddisc.disid
                    WHERE
                        (personenc.perid = 15410)
                    AND (scddisc.active = 1)
                    AND (contacts.active = 1)
                    AND (scddisc.medgroup = 1) --
                    AND (
                        personenc.sdate BETWEEN '2014-03-05'
                        AND '2014-03-12'
                    )
                    UNION
                        SELECT
                            N', ' + ISNULL(scddisc.abbrev, '!!') AS medicalDisciplines
                        FROM
                            scddisc
                        INNER JOIN contacts ON scddisc.disid = contacts.discipline
                        INNER JOIN personenc ON contacts.cntid = personenc.ocntid
                        WHERE
                            (personenc.perid = 15410)
                        AND (scddisc.active = 1)
                        AND (contacts.active = 1)
                        AND (scddisc.medgroup = 1) -- AND (personenc.sdate BETWEEN '2014-03-05' AND '2014-03-12')
                ) AS k FOR XML PATH (''),
                TYPE ).
            VALUE
                ('.[1]', 'nvarchar(max)'),
                1,
                2,
                ''
        );

有谁能告诉我.value('.[1]','nvarchar(max)'),1,2,'').value('text()[1]','nvarchar(max)'),1,2,'')的含义/区别是什么?

答案 1 :(得分:0)

SELECT STUFF (
(SELECT ... FOR XML PATH ('')), 1, 2, '')