子查询返回多个值的大小写

时间:2014-03-25 07:25:04

标签: sql-server etl

我写了一个像这样的查询

SELECT * FROM (
    SELECT CASE co.grdtxtCertificationOther 
        WHEN 'อย.' THEN  2 
        WHEN 'มผช.' THEN 3 
        WHEN 'มอก.' THEN 4 
        WHEN 'ฮาลาล' THEN 5
        WHEN 'Q' THEN 6 
        WHEN 'GMP' THEN 7 
        WHEN 'GAP' THEN 8
        WHEN 'HACCP' THEN 9 
        WHEN 'เกษตรอินทรีย์' THEN 10 
        ELSE 1 
        END As ID, 
        co.[grdtxtCertificationOther] AS STDName,
        co.[grdtxtCertificationNumberOther] AS STDNumber,
        CONVERT( VARCHAR(24),
           ( SELECT CASE 
             WHEN LEN(grddatIssueDateOther) >4 THEN grddatIssueDateOther
             END  
             FROM custom.[tblR_docProduct_grdCertificationOther] 
             WHERE dp.id = co.[_parent]
           ),109
        ) AS SentDate,  
        dp.libtxtUserID AS ParentID,
        1 AS Displayorder,
        0 AS isDisable
        FROM Custom.tblR_docProduct dp
        INNER JOIN  [Custom].[tblR_docProduct_grdCertificationOther] co
        ON dp.Id = co._Parent
    ) t

并显示错误

Subquery returned more than 1 value. 
This is not permitted when the subquery follows =, !=, <, <= , >, >= or 
when the subquery is used as an expression.

就此而言

(
 SELECT CASE 
 WHEN LEN(grddatIssueDateOther) >4 THEN grddatIssueDateOther
 END  
 FROM custom.[tblR_docProduct_grdCertificationOther] 
 WHERE dp.id = co.[_parent]),109
) As SentDate  

我不知道为什么会被卡在上面

1 个答案:

答案 0 :(得分:1)

我怀疑你只是想:

SELECT CASE co.grdtxtCertificationOther
            WHEN 'อย.' THEN  2
            WHEN 'มผช.' THEN 3
            WHEN 'มอก.' THEN 4
            WHEN 'ฮาลาล' THEN 5
            WHEN 'Q' THEN 6
            WHEN 'GMP' THEN 7
            WHEN 'GAP' THEN 8
            WHEN 'HACCP' THEN 9
            WHEN 'เกษตรอินทรีย์' THEN 10 ELSE 1 END As ID
        , co.[grdtxtCertificationOther] AS STDName
        , co.[grdtxtCertificationNumberOther] AS STDNumber
        , CONVERT(VARCHAR(24),
           CASE 
              WHEN LEN(co.grddatIssueDateOther) >4 THEN co.grddatIssueDateOther
                   END,109) As SentDate  
        , dp.libtxtUserID  As ParentID
        , 1 AS Displayorder
        , 0 AS isDisable
                   FROM Custom.tblR_docProduct dp
INNER JOIN  [Custom].[tblR_docProduct_grdCertificationOther] co
ON dp.Id = co._Parent

因为目前,您的子查询是一个不相关的子查询 - 它引入了对tblR_docProduct_grdCertificationOther表的 new 引用,但是WHERE子句只会对其进行断言。外部引用(使用别名co)和Custom.tblR_docProduct表(使用别名dp)。

如果你确实想要一个子查询,很难说它应该是什么,因为你的尝试中的WHERE子句似乎与JOIN之间的当前co条件相同。 dp

还不清楚为什么你有一个只在一个子查询上执行SELECT *的外部查询。