SQL 服务器程序 - table 每天 - 每月保留一次历史记录
SQL Server procedure - table every day - keep history once a month
我在 SQL 服务器上有程序 运行ning。我想每天运行那个程序。哪个每天会产生输出table?
但是,我只想每月保留一次历史记录。我尝试了以下程序,但它每天都保留历史记录。
我怎样才能修改它;这个 table 应该每天创建,但我只能每月保留一次历史记录,例如在每个月的最后一天?
DECLARE @today AS DATE = CAST(GETDATE() AS DATE)
IF OBJECT_ID('[CI_Temp].[dbo].[Items_Scored_With_History]', 'U') IS NULL
CREATE TABLE [CI_Temp].[dbo].[Items_Scored_With_History]
(
id INT NOT NULL,
sub VARCHAR(255) NOT NULL,
actual VARCHAR(255) NOT NULL,
preds VARCHAR(255) NOT NULL,
prob VARCHAR(255) NOT NULL,
pred_eval VARCHAR(255) NOT NULL,
score_date DATETIME NOT NULL,
)
ELSE
DELETE FROM [CI_Temp].[dbo].[Items_Scored_With_History]
WHERE score_date = @today;
INSERT INTO [CI_Temp].[dbo].[Items_Scored_With_History]
SELECT id,
sub,
actual,
preds,
prob,
pred_eval,
@today AS score_date,
FROM [CI_Temp].[dbo].[Items_Scored_With_History] WITH (NOLOCK)
如有任何帮助,我们将不胜感激 - 提前致谢!
如果您使用 SQL Server 2012 或更新版本 SQL 服务器。
你可以确定今天是不是月末。
DECLARE @today AS DATE = CAST(GETDATE() AS DATE)
IF EOMONTH(@today, 0)=convert(date, GETDATE()) BEGIN
PRINT 'Today is end of months'
--your script will be here.
END
ELSE
PRINT 'Today is not end of the month'
我在 SQL 服务器上有程序 运行ning。我想每天运行那个程序。哪个每天会产生输出table?
但是,我只想每月保留一次历史记录。我尝试了以下程序,但它每天都保留历史记录。
我怎样才能修改它;这个 table 应该每天创建,但我只能每月保留一次历史记录,例如在每个月的最后一天?
DECLARE @today AS DATE = CAST(GETDATE() AS DATE)
IF OBJECT_ID('[CI_Temp].[dbo].[Items_Scored_With_History]', 'U') IS NULL
CREATE TABLE [CI_Temp].[dbo].[Items_Scored_With_History]
(
id INT NOT NULL,
sub VARCHAR(255) NOT NULL,
actual VARCHAR(255) NOT NULL,
preds VARCHAR(255) NOT NULL,
prob VARCHAR(255) NOT NULL,
pred_eval VARCHAR(255) NOT NULL,
score_date DATETIME NOT NULL,
)
ELSE
DELETE FROM [CI_Temp].[dbo].[Items_Scored_With_History]
WHERE score_date = @today;
INSERT INTO [CI_Temp].[dbo].[Items_Scored_With_History]
SELECT id,
sub,
actual,
preds,
prob,
pred_eval,
@today AS score_date,
FROM [CI_Temp].[dbo].[Items_Scored_With_History] WITH (NOLOCK)
如有任何帮助,我们将不胜感激 - 提前致谢!
如果您使用 SQL Server 2012 或更新版本 SQL 服务器。
你可以确定今天是不是月末。
DECLARE @today AS DATE = CAST(GETDATE() AS DATE)
IF EOMONTH(@today, 0)=convert(date, GETDATE()) BEGIN
PRINT 'Today is end of months'
--your script will be here.
END
ELSE
PRINT 'Today is not end of the month'