2016-11-17 4 views
0

이 코드는 Microsoft SQL Server Management Studio 2008에서 SQL 쿼리로 실행할 때 작동하지만 작업 단계로 설정된 경우 두 번째 조건 (월말이 아닐 때 업데이트) 만 작동합니다. 이 코드의 문제점은 무엇입니까?작업 단계 작업을 만드는 방법

--set next invoice date 
    declare @data nvarchar(10) --invoice date 
    set @data = CONVERT (date, GETDATE()); 

    update table 
    set invoice_date = case when day(DATEADD(day,1,@data)) = 1 then --is last day of month 
            (SELECT convert(date,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)))) -- set inovice date as last day of next month 
          else --is not last day of month 
            (select DATEADD(MM,1,@data)) --add one month to inovice date 
          end 
    where status = 'current' and invoice_date = @data -- only for current inovices 
+0

언제 SQL 작업을 실행합니까? –

+0

매일 23:00:00에 일자리가 생깁니다. – mahnamahna

+0

(날짜, DATEADD (s, -1, DATEADD (mm, DATEDIFF (m, 0, GETDATE()) + 1,0))))) -) 다음 날짜의 마지막 날짜로 inovice 날짜를 설정합니다. 이 코드에서 다음 달 마지막 날처럼 언급했습니다. 그러나 그것이 그 달의 마지막 날을 잡았다. –

답변

0

시도해보십시오. 첫 번째 조건은 매월 마지막 날에만 작동합니다.

--set next invoice date 
DECLARE @data NVARCHAR(10) --invoice date 
SET @data = CONVERT (DATE, GETDATE()); 

UPDATE table 
SET invoice_date = CASE WHEN DAY(DATEADD(DAY,1,@data)) = 1 
        THEN --is last day of month 
         CAST(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@data)+2,0)) AS DATE) -- set inovice date as last day of next month 
        ELSE --is not last day of month 
        (DATEADD(MM,1,@data)) --add one month to inovice date 
         END 
WHERE status = 'current' AND invoice_date = @data -- only for current inovices 
관련 문제