SQL server 2008 - 子查询返回了多个值

时间:2014-12-30 18:35:20

标签: sql sql-server-2008

SQL server 2008 - 从查询结果集中读取数据时发生错误。子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

This came up as soon I added the case statement with CHDOCS. Everything else worked
 fine until that point. Can anyone help me out with this one please?


SELECT
CHDOCS.MAILING_ID
,CASE WHEN (SELECT CHDOCS.MAILING_DOCUMENT
       FROM CHDOCS 
       WHERE
               CHDOCS.MAILING_DOCUMENT LIKE N'%EX%')IS NOT NULL

               THEN (SELECT CHDOCS.MAILING_DOCUMENT
       FROM CHDOCS 
       WHERE
               CHDOCS.MAILING_DOCUMENT LIKE N'%EX%') ELSE '' END AS 'DOC'

     FROM CHDOCS

2 个答案:

答案 0 :(得分:0)

您不能对结果集使用CASE语句 - 它必须针对单个值运行。

我相信这是您要完成的任务,这是对MAILING_DOCUMENT表中每个CHDOCS字段的评估:

SELECT CHDOCS.MAILING_ID

    -- Check if the Mailing Document field matches the pattern *EX*
    ,CASE WHEN CHDOCS.MAILING_DOCUMENT LIKE N'%EX%' 
        THEN CHDOCS.MAILING_DOCUMENT  -- It does, return the field value.
        ELSE ''                       -- It doesn't, return empty.
        END AS DOC

FROM CHDOCS

答案 1 :(得分:0)

您的此查询

SELECT
    CHDOCS.MAILING_ID, ISNULL(CHDOCS.MAILING_DOCUMENT,'')
    FROM CHDOCS
WHERE CHDOCS.MAILING_DOCUMENT LIKE N'%EX%'