在Where子句

时间:2016-02-09 09:27:55

标签: sql sql-server

需要在IF子句中使用Where语句执行SQL查询。

SELECT 
    CASE 
        WHEN Isnull(CustGuarFlag ,'') = '' THEN DocName 
        ELSE DocName + ' - '+ 
             CASE 
                 WHEN CustGuarFlag= 'C' THEN 'Customer'
                 ELSE 'Guarantor' 
             END 
    END DocName, 
    ISNULL(A.DOCSTAGE, '') DOCSTAGE, 
    ISNULL(A.DOCREFNO,'') DOCREFNO, 
    ISNULL(CONVERT(VARCHAR, A.DOCREFDT, 103), '') DOCREFDT, 
    ISNULL(CONVERT(VARCHAR, A.RECDDT, 103), '') RECDDT , 
    ISNULL(A.DOCVALUE, 0) DOCVALUE , 
    ISNULL(CONVERT(VARCHAR, A.DOCVLDUPTO, 103), '') DOCVLDUPTO, 
    a.pk_id AS AppDocpk, 
    b.pk_id AS Docpk, 
    CASE 
       WHEN Isnull(a.DocVerifiedStatus, '') = 'null' 
          THEN '' 
          ELSE Isnull(a.DocVerifiedStatus,'') 
    END AS DocVerifiedStatus, 
    ISNULL(RejRemarks, '') AS RejRemarks, 
    ISNULL(KYCDocHdr_Fk, 0) AS KYCPk, 
    ISNULL(DocShrtDescr, '') AS DocShrtDescr 
FROM            
    loln_apprvldoc A WITH(NOLOCK) 
JOIN
    LGEN_DOCUMENTS B WITH(NOLOCK) ON A.Doc_FK = B.PK_ID 
LEFT OUTER JOIN 
    Lgen_KYCApprvlDoc c ON a.KYCDocHdr_Fk = c.PK_Id 
WHERE           
    A.PRPSLNO = 'KATHU1602080002' 
    AND ISNULL(A.DocStat, '') = 'R' 
    AND ISNULL(A.DocVerifiedStatus, '') IN ('Rejection', '', 'null') 
    AND CASE 
           WHEN Isnull(c.DocVerifiedStatus, '') = 'A' 
              THEN c.Pk_id IS NOT NULL
              ELSE c.Pk_id IS NULL
        END

1 个答案:

答案 0 :(得分:1)

CASE不属于WHERE子句。用

替换该CASE子句
(
  (Isnull(c.DocVerifiedStatus,'')='A' AND c.Pk_id IS NOT NULL)
OR
  (Isnull(c.DocVerifiedStatus,'')<>'A' AND c.Pk_id IS NULL)
)