如何使工作步骤工作
How to make a job step work
此代码在 运行 作为 SQL Microsoft SQL Server Management Studio 2008 中的查询但设置为作业步骤时仅适用于第二个条件(更新时不是最后一个月中的某天)。这段代码有什么问题?
--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
试试这个。第一个条件仅在每个月的最后一天有效。
--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
此代码在 运行 作为 SQL Microsoft SQL Server Management Studio 2008 中的查询但设置为作业步骤时仅适用于第二个条件(更新时不是最后一个月中的某天)。这段代码有什么问题?
--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
试试这个。第一个条件仅在每个月的最后一天有效。
--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