从批处理文件中恢复.bak文件

时间:2015-01-19 15:21:15

标签: batch-file sqlcmd

我正在自动化测试环境设置过程,以下是所涉及的手动步骤的一部分:

  1. 将.bak文件从网络驱动器复制到我的D:驱动器
  2. 启动SQL Management Studio,使用“还原文件和文件组”选项还原.bak文件
  3. 对于第1点,我使用了:

    robocopy \\10.xx.xxx.x\dirA\Build\%myVar%\DB\ D:\ myDB_V%myVar%.bak
    

    它复制得很好!

    现在,对于第2点,我在命令提示符下使用sqlcmd尝试以下命令:

    sqlcmd -E -S localhost\myInstance -Q "RESTORE DATABASE myDB_V2.2.2.10 FROM DISK='D:\myDB_V2.2.2.10.bak' WITH FILE = 1, MOVE 'myDB_V2.2.2.10' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf', MOVE 'myDB_V2.2.2.10_log' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf';"
    

    它说

    Msg 102, Level 15, State 1, Server localhost\myInstance, Line 1
    Incorrect syntax near '.2'.
    Msg 319, Level 15, State 1, Server localhost\myInstance, Line 1
    Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
    

    我无法解释它的含义。

    有人可以帮我吗?

3 个答案:

答案 0 :(得分:2)

试试这个:

sqlcmd -E -S localhost\myInstance -Q "RESTORE DATABASE 'myDB_V2.2.2.10' FROM DISK='D:\myDB_V2.2.2.10.bak' WITH FILE = 1, MOVE 'myDB_V2.2.2.10' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf', MOVE 'myDB_V2.2.2.10_log' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf';"

答案 1 :(得分:2)

试试这个 sqlcmd -E -S localhost\myInstance -Q "RESTORE DATABASE [myDB_V2.2.2.10] FROM DISK='D:\myDB_V2.2.2.10.bak' WITH FILE = 1, MOVE 'myDB_V2.2.2.10' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf', MOVE 'myDB_V2.2.2.10_log' TO 'C:\Program Files\Microsoft SQLServer\MSSQL10.SQLEXPRESS\MSSQL\DATA\myDB_V2.2.2.10.mdf';"

答案 2 :(得分:1)

看起来错误与您的数据库名称有关," RESTORE DATABASE myDB_V2 **。2 **。2.10 [...]"。我建议不要在数据库名称中使用句点,因为句点用于分隔查询中的数据库对象。如果您已经在使用句点,请尝试将数据库名称包装在方括号[]。