自动备份

declare @filedir varchar(200),@dir varchar(200)
declare @dbname varchar(50),@bakname varchar(50),@sql varchar(4000)
select @filedir=filename from sysfiles
set @dir=''
while charindex('/',@filedir)<>0
begin
  set @dir=@dir+left(@filedir,charindex('/',@filedir))
  set @filedir=substring(@filedir,charindex('/',@filedir)+1,len(@filedir)-charindex('/',@filedir))
end
set @bakname='bak'+cast(datepart(weekday,getdate())-1 as varchar(1))
-----指定数据库
set @dbname='dbname'
set @sql='BACKUP DATABASE ['+@dbname+'] TO  DISK = N'''+'e:/sqlbak/'+@bakname+''' WITH  INIT ,  NOUNLOAD ,  NOSKIP ,  STATS = 10,  NOFORMAT'
--备份文件到"e:/sqlbak/"目录下
exec(@sql)
 
--实质是:
backup database pubs to disk =  'e:/ddk' with
init ,nounload,noskip,stats=10,noformat

你可能感兴趣的:(sql,数据库,database,disk)