所以我搜索了很多類似的信息,但沒有發現相當的問題。批處理文件SQLCMD恢復循環 - 文件消失
我在測試環境中工作,需要從同一個SQL數據庫備份(.BAK)恢復到SQL Server的許多命名實例。所有的sql實例都預先安裝並運行。
我玩過不同的想法,但使用sqlcmd的批處理文件似乎是最適合這項工作的。
所以我創建了一個請求的啓動和停止的實例號,然後應該從備份到每個SQL命名實例遞增沿途的實例號恢復一個批處理文件(.bat)。
當它運行sqlcmd似乎工作正常。在結束它打印出
RESTORE DATABASE successfully processed X pages in Y seconds
另外的文件(.mdf,.NDF,.LDF)在目錄中如預期,然後將其移動到下一個。
問題是,當它移到下一個時,剛恢復的文件就從目錄中消失。
如果有人有任何想法,它肯定會被讚賞。
這裏的批...
ECHO OFF
ECHO Enter starting instance number for restore db
SET /P _IntStart=
ECHO Enter number of last instance for restore db
SET /P _IntStop=
SET /a _IntStop=_IntStop+1
:RestoreDb
If %_IntStart% GEQ %_IntStop% goto:EOF
ECHO Display Instance Number... IntStart = %_IntStart%
sqlcmd -e -s localhost\instance%_IntStart% -d master -U user -P password -Q "Use [master]; RESTORE DATABASE DBName1 FROM DISK = 'C:\DBName1.bak'WITH REPLACE, MOVE 'DBName1' TO 'E:\Microsoft SQL Server\MSSQL10_50.INSTANCE%_IntStart%\MSSQL\DATA\DBName1.mdf', MOVE 'DBName1_log' TO 'E:\Microsoft SQL Server\MSSQL10_50.INSTANCE%_IntStart%\MSSQL\DATA\DBName1_log.LDF', MOVE 'ftrow_DBName1Catalog' TO 'E:\Microsoft SQL Server\MSSQL10_50.INSTANCE%_IntStart%\MSSQL\DATA\DBName1_1.ndf';"
SET /a _IntStart=_IntStart+1
GOTO:RestoreDb
PAUSE
EXIT
================================= ========
從SQL管理。工作室我也嘗試了下面。它可以工作,如果我註釋掉循環並每次手動衝突實例編號運行它。它將創建數據庫和文件的單獨副本。這裏的問題是SQLCMD似乎並不喜歡Mgmt中的串聯。 Studio,因此我無法在:CONNECT中增加實例編號。它最終試圖連接到localhost \ instance $(SCintnum)。
Declare @intnum int
Set @intnum = 1
Declare @intstr NVARCHAR(255)
Set @intstr = @intnum
Declare @PathName1 NVARCHAR(255)
Declare @PathName2 NVARCHAR(255)
Declare @PathName3 NVARCHAR(255)
Set @PathName1 = 'E:\Microsoft SQL Server\MSSQL10_50.INSTANCE' + @intstr + '\MSSQL\DATA\DBName1.mdf'
Set @PathName2 = 'E:\Microsoft SQL Server\MSSQL10_50.INSTANCE' + @intstr + '\MSSQL\DATA\DBName1_log.LDF'
Set @PathName3 = 'E:\Microsoft SQL Server\MSSQL10_50.INSTANCE' + @intstr + '\MSSQL\DATA\DBName1_1.ndf'
While @intnum < 51
:SETVAR SCintnum 1
:CONNECT localhost\instance$(SCintnum) -U user -P password
Use [master];
RESTORE DATABASE DBName1 FROM DISK = 'C:\DBName1.bak'
WITH REPLACE,
MOVE 'DBName1' TO @PathName1,
MOVE 'DBName1_log' TO @PathName2,
MOVE 'ftrow_DBName1Catalog' TO @PathName3;
:SETVAR SCintnum $(SCintum)+1
Set @intnum = @intnum+1
Set @intstr = @intnum
Set @PathName1 = 'E:\Microsoft SQL Server\MSSQL10_50.INSTANCE' + @intstr + '\MSSQL\DATA\DBName1.mdf'
Set @PathName2 = 'E:\Microsoft SQL Server\MSSQL10_50.INSTANCE' + @intstr + '\MSSQL\DATA\DBName1_log.LDF'
Set @PathName3 = 'E:\Microsoft SQL Server\MSSQL10_50.INSTANCE' + @intstr + '\MSSQL\DATA\DBName1_1.ndf'
=========================================== ========================
這是我結束了使用C#示例.....
///Set SQL Connection
SqlConnection myConnection = new SqlConnection("user id=sa;" +
"password="+ sapassword+";server="+servername+"\\instance"+currentinstancenum+";" +
"Trusted_Connection=yes;" +
"database=master; " +
"connection LifeTime=0; connection Timeout=30");
///Set SQL Command
string thesqlcommand = "USE [master]; RESTORE DATABASE " + dbname + " FROM DISK = '" + backuplocation + "' WITH REPLACE, MOVE '" + dbname + "' TO " + @PathName1 + ", MOVE '" + dbname + "_log' TO " + @PathName2 + ", MOVE 'ftrow_" + dbname + "Catalog' TO " + @PathName3 + ";";
SqlCommand myCommand = new SqlCommand(thesqlcommand, myConnection);
///Set SQL Command TimeOut, open connection, execute command, close command
myCommand.CommandTimeout = 180;
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
你的意思是他們從你移動到的目錄中失蹤了嗎? –