数据备份:
declare @a varchar(1000) set @a='backup database CDBST to disk = ''D:\DBbak\CDBST_'+convert(char(8),getdate(),112)+'''WITH COMPRESSION'
exec(@a)
go
删除3天的数据备份:
/*
CREATE TABLE COAFile(
id int IDENTITY, --编号
directory nvarchar(260), --路径
depth int, --深度,相对与@path
IsFile bit,
FileData varbinary(max)
)--0文件夹1文件名成
*/
DECLARE @Path nvarchar(260)
SET @Path = 'D:\DBbak'
IF OBJECT_ID('tempdb..#File') IS NOT NULL
DROP TABLE #File
CREATE TABLE #File
(
id int IDENTITY, --编号
directory nvarchar(260), --路径
depth int, --深度,相对与@path
IsFile bit --0文件夹1文件名成
)
INSERT #File EXEC master.dbo.xp_dirtree
@path = @path,
@depth = 0,
@file = 1
declare @日期 char(8)=convert(char(8),getdate()-2,112)
select @日期
select
日期=SUBSTRING(directory,7,8),
*
into #aa
from #File
--select * from #File where directory like 'ssd_20200919%' order by directory
--select * from #aa where directory like 'ssd_20200919%' order by directory
declare @sql varchar(max)=''
select top 1000 @sql=@sql+'exec xp_cmdshell ''del "'+@Path+'\'+directory+'"''
'
from #aa
where
日期<=@日期
order by directory
print @sql
exec(@sql)
drop table #File
drop table #aa