使用移动进行SQL脚本还原 - 使用文件&同

时间:2016-02-03 14:59:41

标签: sql sql-server sql-server-2008

我正在尝试编写一个数据库的恢复脚本,我需要知道使用with文件和移动命令的正确语法

到目前为止,我有:

   RESTORE DATABASE Test
   FROM DISK = 'E:\Test_db_BASELINE.BAK'
   WITH FILE = 1
   WITH MOVE 'Test_dat' TO 'C:\MSSQL\v2_Data\Test_Data.MDF',
        MOVE 'Test_log' TO 'C:\MSSQL\v2_Data\Test_Log.LDF', REPLACE

我的语法错误有任何想法吗?

进入上下文我试图执行以下操作来自动执行恢复,其中bak文件具有带时间戳的命名约定:

      --==CHECK IF DB EXISTS IF IT DOES DROP IT

 USE [master]
 IF EXISTS(SELECT * FROM sys.databases where name='SlotLookup')
 DROP DATABASE [SlotLookup]


 --==START THE RESTORE PROCESS
 DECLARE @FileName varchar(255), @PathToBackup varchar(255), @RestoreFilePath varchar(1000)

 DECLARE @Files TABLE (subdirectory varchar(255), depth int, [file] int)

 SET NOCOUNT ON

--==SET THE FILEPATH
 SET @PathToBackup = 'path'

 --insert into our memory table using dirtree and a single file level
 INSERT INTO @Files
 EXEC master.dbo.xp_DirTree @PathToBackup,1,1

 SELECT TOP 1 
    @FileName = [subdirectory]
 FROM 
    @Files
 WHERE
    -- get where it is a file
    [file] = 1
 AND 
 --==FIND THE LOGICAL NAME OF THE BAK FILE FROM THE CHRONILOGICALLY ORDERED LIST   
    subdirectory LIKE '%.bak'
 ORDER BY
    -- order descending so newest file will be first by naming convention
    subdirectory DESC

IF LEFT(REVERSE(@PathToBackup), 1) != '\'
BEGIN
    SET @PathToBackup = @PathToBackup + '\'
END

SET @RestoreFilePath = @PathToBackup + @FileName

SELECT @RestoreFilePath

--===BEGIN THE RESTORE TO THE DESIGNATED SERVER
RESTORE DATABASE [SlotLookup] 
FROM DISK = @RestoreFilePath
FILE = 1
WITH MOVE 'SlotLookup' TO 'path\SlotLookup.mdf',
MOVE 'SlotLookup_log' TO 'path\SlotLookup_log.ldf'

获取语法错误 消息156,第15级,状态1,第48行 关键字“FILE”附近的语法不正确。 消息319,第15级,状态1,第49行

1 个答案:

答案 0 :(得分:0)

带有移动的恢复备份脚本的语法如下所示:

RESTORE DATABASE [AdventureWorksCopy] FROM DISK = 'c:\mssql\backup\[...]\Backup.bak' 
WITH CHECKSUM, 
MOVE 'AdventureWorks_Data' TO 'c:\mssql\data\[...]\AdventureWorksCopy_Data.mdf', 
MOVE 'AdventureWorks_Log' TO 'c:\mssql\log\[...]\AdventureWorksCopy_Log.ldf', 
RECOVERY, REPLACE, STATS = 10;

你得到什么语法错误? 请将其包含在问题