在case语句sql server中的THEN部分设置另一个条件

时间:2018-04-24 10:03:16

标签: sql sql-server sql-server-2014

我遇到需要设置

的情况
CASE JDT1.TransType 
   WHEN 46 THEN  JDT1.Debit > 0
END

导致错误,如何克服此问题,我的完整查询是

create PROCEDURE [dbo].[Supplier_Enquiry]   
AS
BEGIN

SELECT        OCRD.CardCode, OCRD.CardName, OCRD.GroupCode, OCRG.GroupName, OCRD.CreditLine, ISNULL(JDT1.FCCurrency,'LKR') AS Currency, JDT1.RefDate, 
                         CAST(OJDT.BaseRef AS int) AS 'Document Number',
CASE  WHEN JDT1.TransType=18 THEN 'Invoice'
          WHEN JDT1.TransType=19 THEN 'Credit Memo'
          WHEN JDT1.TransType=204 THEN 'A/P Down Payment'
          WHEN JDT1.TransType=46 THEN 'Vendor Payment'
          WHEN JDT1.TransType=30 THEN 'Journal Entry'

          END AS 'Transaction', 
                         JDT1.DueDate, JDT1.Debit - JDT1.Credit AS 'Outstanding', JDT1.Debit AS [Amount Dr], JDT1.Credit AS [Amount Cr],ISNULL(VPM1.CheckNum, 0) AS CheckNum
FROM            VPM1 INNER JOIN
                         OVPM ON VPM1.DocNum = OVPM.DocEntry RIGHT OUTER JOIN
                         JDT1 INNER JOIN
                         OCRD ON JDT1.ShortName = OCRD.CardCode INNER JOIN
                         OCRG ON OCRD.GroupCode = OCRG.GroupCode INNER JOIN
                         OJDT ON JDT1.TransId = OJDT.TransId ON OVPM.TransId = OJDT.TransId
WHERE        (OCRD.CardType = 'S')  
AND
(

CASE JDT1.TransType 
    WHEN 46 THEN  JDT1.Debit > 0

END

) 
END

需要一些指导来纠正问题

2 个答案:

答案 0 :(得分:1)

在这种情况下你根本不需要案件:

WHERE OCRD.CardType = 'S' 
AND 
(
    JDT1.TransType <> 46
    OR JDT1.Debit > 0
)

答案 1 :(得分:1)

因此,仅当TransType = 46

时,才会检查借记&gt; 0
...
AND (JDT1.TransType <>46 OR (JDT1.TransType = 46 and JDT1.Debit > 0))
...