我有一个用于备份数据库的批处理/ sql脚本对。我最近进入文件夹以获取数据库,我看到它无法正常工作。我查看了我的批处理脚本,并没有在一天或一个月内找到我,但是这一年显示为“开启”!我的SQL脚本工作正常,我想添加我的批处理脚本。
这是我的批处理脚本
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%dd%-%mm%-%yyyy%
rem day month year check
echo %dd%
echo %mm%
echo %yyyy%
echo %date%
mkdir %date%
sqlcmd -S .\SQLEXPRESS -i backupQuery.sql
这里是'backupQuery.sql'
DECLARE @BackupLocation AS NVARCHAR(255) = 'C:\SQL\backups\'
/* Set @CurrentDate to a timestamp in mmddyyyy form */
DECLARE @CurrentDate AS NVARCHAR(255)
SET @CurrentDate = REPLICATE('0', 2 - LEN(CAST(DAY(GETDATE()) AS NVARCHAR))) /*day number 1*/
+
CAST(DAY(GETDATE()) AS NVARCHAR) /*day number 2*/
+ '-' +
REPLICATE('0', 2 - LEN(CAST(MONTH(GETDATE()) AS NVARCHAR))) /*month number 1*/
+
CAST(MONTH(GETDATE()) AS NVARCHAR) /*month number 1*/
+ '-' +
CAST(YEAR(GETDATE()) AS NVARCHAR) /*year*/
DECLARE @DatabaseName AS NVARCHAR(255)
/* Get all databases except for the temporary database */
DECLARE Databases CURSOR FOR
SELECT name
FROM sys.databases
WHERE name
NOT IN
('tempdb',
'master',
'model',
'msdb')
OPEN Databases
FETCH NEXT FROM Databases
INTO @DatabaseName
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @Command AS NVARCHAR(MAX)
SET @Command = 'BACKUP DATABASE ' + QUOTENAME(@DatabaseName) + ' TO DISK = ''' +
@BackupLocation + @CurrentDate + '\' + @DatabaseName + '.BAK'''
EXEC(@Command)
FETCH NEXT FROM Databases
INTO @DatabaseName
END
CLOSE Databases
DEALLOCATE Databases
他们打算备份到以当前日期命名的目录,但它是以--on?来发布的!
答案 0 :(得分:0)
Myself和@aschipfl设法解决了它,它之所以破坏的原因我认为是因为我正在重新分配一个系统变量。我所要做的就是在我的SQL中使用DATE CONVERT格式6,在cmd中使用%date%变量。这解决了它,我现在的格式为dd-MMM-yy。