2012-04-30 42 views
2

在SQL Server 2008 R2中,我通過右鍵單擊SQL Server Management Studio中的服務器並設置它們,將DataLog位置設置爲F:\moved\SQL Server 2008 slqcmd數據位置

但是,當我使用sqlcmd恢復數據庫時,它將數據庫恢復爲E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA,這似乎是默認的SQL Server位置。

我需要的數據庫總是被恢復到F:\moved\用於存儲的目的,但是當我用我的劇本下面我有時得到一個錯誤:

RESTORE DATABASE $(database) FROM DISK = '$(root)\$(database).bak' WITH RECOVERY, 
MOVE '$(database)' TO '$(datadir)$(database).mdf', 
MOVE '$(database)_log' TO '$(datadir)$(database).ldf' 
GO 

錯誤:

Changed database context to 'master'.
Msg 3234, Level 16, State 2, Server SQL1, Line 3
Logical file 'userdb1' is not part of database 'userdb1'. Use RESTORE FILELISTONLY to list the logical file names.
Msg 3013, Level 16, State 1, Server SQL1, Line 3
RESTORE DATABASE is terminating abnormally.

sqlcmd可以強制恢復爲F:\moved\默認情況下,而不必手動指定它,因爲我有上面,以避免錯誤?

回答

0

難道你不能只在sqlcmd腳本中定義datadir變量嗎?

事情是這樣的:

:setvar datadir F:\moved\ 

RESTORE DATABASE $(database) FROM DISK = '$(root)\$(database).bak' WITH RECOVERY, 
MOVE '$(database)' TO '$(datadir)$(database).mdf', 
MOVE '$(database)_log' TO '$(datadir)$(database).ldf' 
GO 

有了這一行,sqlcmd應該始終使用該目錄(而不是SQL Server默認)

或者你也可以設置該變量的值,以任何你要從命令行調用sqlcmd上:

C:\> sqlcmd -v datadir=F:\moved\ (script name) 
+0

啊,對不起,我已經做到這一點: SQLCMD -S%4 -i恢復。 sql -v database =「%1」-v root =「%2」-v dbserver =「%4」-v datadir =「%6」-U sa -P測試結果爲錯誤.. –

+0

原因I想要它默認是因爲當我通過SQL管理工作室手動恢復數據庫它恢復正常? –