-1

Here is some SQL which is working fine inside and 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

I would like to place a condition around this part 'dateadd(mm, 1, S.Date)' Should e able to do this:

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 my questions is can I do this part of the second query:

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

I am unsure if a case ca be used inside an insert like I have done.

4

1 回答 1

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,
于 2013-06-24T02:57:47.177 回答