插入时的用例条件

时间:2013-06-24 02:47:26

标签: sql sql-server sql-server-2000

这是一些在内部工作正常的SQL和SP(SQL Server 2000)

Insert into #sysAccrual
Select 'R', Cast(S.ServicePurchaseLineID as varchar),
    - S.Amount, dateadd(mm, 1, S.Date), null, L.LicenceTypeID, null,
    L.LicenceID, L.LicenceNumber
from sysMYOB_SP S inner join Licence L ON S.LicenceID = L.LicenceID 
        and S.PaymentReasonID = 2 
        and S.Accrued = 0
        and S.Deducted = 1
        and datediff(yy, S.Date, @AccrualMonth) = 0
        and datediff(mm, S.Date, @AccrualMonth) = 0

我想在这个部分周围放置条件'dateadd(mm,1,S.Date)'应该能够做到这一点:

Insert into #sysAccrual
Select 'R', Cast(S.ServicePurchaseLineID as varchar), - S.Amount, 
       case when 
       dateDiff(d, S.Date , LCS.statusDateTo) > 31 THEN 
     dateadd(mm, 1, S.Date), 
   ELSE 
         S.Date, 
   END 
       null, L.LicenceTypeID, null,
       L.LicenceID, L.LicenceNumber
from sysMYOB_SP S inner join Licence L ON S.LicenceID = L.LicenceID 
              inner join licCurrentStatus LCS ON L.LicenceID = LCS.LicenceID 
        and S.PaymentReasonID = 2 
        and S.Accrued = 0
        and S.Deducted = 1
        and datediff(yy, S.Date, @AccrualMonth) = 0
        and datediff(mm, S.Date, @AccrualMonth) = 0

@ghost我的问题是我可以做第二个查询的这一部分:

 case when 
 dateDiff(d, S.Date , LCS.statusDateTo) > 31 THEN 
    dateadd(mm, 1, S.Date), 
 ELSE 
    S.Date, 
 END 

我不确定是否可以像我一样在插入内部使用案例。

1 个答案:

答案 0 :(得分:0)

CASE声明的逗号不正确

而不是

case when 
   dateDiff(d, S.Date , LCS.statusDateTo) > 31 THEN 
 dateadd(mm, 1, S.Date), 
ELSE 
     S.Date, 
END 
你应该

case when 
 dateDiff(d, S.Date , LCS.statusDateTo) > 31 THEN 
 dateadd(mm, 1, S.Date)
ELSE 
 S.Date
END,