Question

I write a query like this

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

and it shows the error

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

on this

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

I don't know why and got stuck on it

Était-ce utile?

La solution

I suspect that you just want:

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

Because at the moment, your subquery is an uncorrelated one - it introduces a new reference to the tblR_docProduct_grdCertificationOther table, but then the WHERE clause only makes assertions about the outer reference (with the alias co) and the Custom.tblR_docProduct table (with the alias dp).

If you really did want a subquery, it's difficult to tell what it should be since the WHERE clause inside your attempt seems to be identical to the current JOIN condition between co and dp.

It's also unclear why you had an outer query that just performed a SELECT * on a single subquery.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top