復制代碼 代碼如下:
DECLARE @filename VARCHAR(255)
DECLARE @date DATETIME
SELECT @date=GETDATE()
SELECT @filename = 'G:\存放位置\數據庫名稱-'+CAST(DATEPART(yyyy,@date) as varchar)+'-'+CAST(DATEPART(mm,@date) as varchar)+'-'+CAST(DATEPART(dd,@date) as varchar)+'.bak'
BACKUP DATABASE [數據庫名稱] TO DISK = @filename WITH INIT
GO
DECLARE @OLDDATE DATETIME
SELECT @OLDDATE=GETDATE()-5
EXECUTE master.dbo.xp_delete_file 0,N'G:\存放位置',N'bak',@olddate,1
上面是一個備份最近5天的數據庫作業腳本。