SQL Server:带有函数调用的案例

时间:2018-07-19 17:49:50

标签: sql sql-server case

表是:

+----+------+
| Id | Name |
+----+------+    
| 1  | aaa  |
| 1  | bbb  |
| 1  | ccc  |
| 1  | ddd  |
| 1  | eee  |
+----+------+

必填输出:

+----+---------------------+
| Id |        abc          |
+----+---------------------+ 
|  1 | aaa,bbb,ccc,ddd,eee |
+----+---------------------+

查询:

SELECT 
    ID, 
    abc = STUFF((SELECT ',' + name 
                 FROM temp1 
                 FOR XML PATH ('')), 1, 1, '') 
FROM 
    temp1 
GROUP BY 
    id

此查询正常运行。我现在想做的是在select中添加一个case语句。如果XML为NULL,则显示“未找到名称”,但是当我尝试执行此操作时,SQL Server会引发错误。

像这样使用时,它要么抛出此错误(见下文)

SELECT
    ID, 
    abc = CASE 
             STUFF((SELECT ',' + name 
                    FROM temp1 
                    FOR XML PATH ('')), 1, 1, '') IS NOT NULL 
             THEN abc
             ELSE 'No names found'
FROM 
    temp1 
GROUP BY 
    id

错误:

  

在需要条件的上下文中指定的非布尔类型的表达式

或当这样使用

SELECT 
    ID, 
    CASE abc = STUFF((SELECT ',' + name 
                      FROM temp1 
                      FOR XML PATH ('')), 1, 1, '') IS NOT NULL 
       THEN abc 
       ELSE 'No names found'
FROM 
    temp1 
GROUP BY 
    id

错误:

  

无效的列名称abc

如何为case语句添加函数调用?

6 个答案:

答案 0 :(得分:2)

您可以:

SELECT ID, COALESCE(STUFF( (SELECT ',' + t1.name 
                            FROM temp1 t1
                            WHERE t1.id = t.id -- perhaps you need this
                            FOR XML PATH ('')
                           ), 1, 1, ''
                          ), 'No names found'
                   ) AS abc
FROM temp1 t
GROUP BY id;

但是,我不知道如何通过相同表的先前查询获得null

答案 1 :(得分:2)

模板:

CASE WHEN test expression THEN resultant ELSE default value END

第一遍

SELECT ID, 
    abc = CASE WHEN STUFF((SELECT ',' + name FROM temp1 FOR XML PATH ('')), 1, 1, '') IS NOT NULL
        THEN CASE WHEN STUFF((SELECT ',' + name FROM temp1 FOR XML PATH ('')), 1, 1, '')
    ELSE 'No names found' END
FROM temp1 
GROUP BY id;

使用CTE不必两次大张旗鼓

WITH cte as (SELECT ID, 
        abc = ((SELECT ',' + name FROM temp1 FOR XML PATH ('')), 1, 1, '')
    FROM temp1
    GROUP BY id)
SELECT ID,
    CASE WHEN abc IS NOT NULL THEN abc ELSE 'No names found' AS abc2
FROM cte;

在这种情况下,请使用COALESCE

SELECT ID, 
    abc = COALESCE(STUFF((SELECT ',' + name FROM temp1 FOR XML PATH ('')), 1, 1, '')
         , 'No names found')
FROM temp1 
GROUP BY id;

答案 2 :(得分:0)

您可以像这样使用COALESCE函数

 SELECT ID, 
   abc =  COALESCE( STUFF( (
             (SELECT ',' + name FROM temp1 FOR XML PATH ('')), 1, 1, ''
           ) , 'No names found')
FROM temp1 GROUP BY id

答案 3 :(得分:0)

查询中缺少关键字时。

当表达式然后是...否则为...结束时的情况

答案 4 :(得分:0)

选择     ID,     abc =大小写              STUFF((SELECT','+名称                     从temp1                     FOR XML PATH('')),1,1,'')不为空              THEN STUFF((SELECT','+名称                     从temp1                     FOR XML路径('')),1,1,'')              ELSE'未找到名字' 从     临时1 通过...分组     ID

答案 5 :(得分:0)

您可以说

SELECT 
    ID, 
    ISNULL(STUFF((SELECT ',' + name 
                      FROM temp1 
                      FOR XML PATH ('')), 1, 1, ''),'No names found') AS 'abc'
FROM 
    temp1 
GROUP BY 
    id

您不需要案例说明