嗨,我想使用bat和sql文件备份数据库服务器的一个数据库。
sqlcmd -S HEIST-BERG-SL\SQLEXPRESS -E -Q "C:\SALTO\Automatic Backups\scripts\backupDB.sql"
PAUSE
当前,当我在上面运行代码时,我在服务器上备份了所有数据库,而这不是我想要的,因此我在这段代码中缺少某些东西来获取名为“ Salto_Test”的特定数据库。
SQL
declare @datstr as varchar(100)=''
declare @currdate as datetime=getdate()
set @datstr=cast(DATEPART(YYYY,@currDate) as varchar(5))+right('00'+cast(DATEPART(MM,@currDate)as varchar(5)),2)+right('00'+cast(DATEPART(DD,@currDate)as varchar(5)),2)+right('00'+cast(DATEPART(HH,@currDate)as varchar(5)),2)+right('00'+cast(DATEPART(MINUTE,@currDate) as varchar(5)),2)
declare @path as varchar(500)='C:\SALTO\Automatic Backups\Salto_Test_db_' + @datstr +'.BAK'
backup database Salto_Test to disk= @path
答案 0 :(得分:3)
如果知道数据库名称,那么为什么要进行while循环和查询字符串,您可以直接编写查询,请在下面替换您的SQL File代码。
declare @datstr as varchar(100)=''
declare @currdate as datetime=getdate()
set @datstr=cast(DATEPART(YYYY,@currDate) as varchar(5))+right('00'+cast(DATEPART(MM,@currDate)as varchar(5)),2)+right('00'+cast(DATEPART(DD,@currDate)as varchar(5)),2)+right('00'+cast(DATEPART(HH,@currDate)as varchar(5)),2)+right('00'+cast(DATEPART(MINUTE,@currDate) as varchar(5)),2)
declare @path as varchar(500)='C:\SALTO\Automatic Backups\Salto_Test_db_' + @datstr +'.BAK'
backup database Salto_Test to disk= @path