2013-05-30 102 views
0

我正在創建一個可以重命名數據庫的PowerShell函數。使用PowerShell重命名SQL數據庫不會重命名物理文件名

這是工作得很好:

$sqlServer = "MySqlServer" 
    $currentDbName = "MyDB" 
    $sqlConnectionString = "Server='$sqlserver';Database='$currentDbName';Integrated Security=True" 
    $sqlQueries = ` 
     "ALTER DATABASE [$currentDbName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE",` 
     "EXEC sp_renamedb '$currentDbName', '$NewName'",` 
     "ALTER DATABASE [$NewName] SET MULTI_USER", ` 
     "ALTER DATABASE [$NewName] MODIFY FILE (NAME = $currentDbName, NEWNAME = $NewName)", ` 
     "ALTER DATABASE [$NewName] MODIFY FILE (NAME = $($currentDbName)_Log, NEWNAME = $($NewName)_Log)" 
    $conn=new-object System.Data.SqlClient.SQLConnection($sqlConnectionString) 
    try { 
     $conn.Open() 
     $sqlQueries | % { 
      $cmd = $conn.CreateCommand() 
      $cmd.CommandText = $_ 
      $cmd.ExecuteScalar() 
      Write-Host " Executed: $_" -ForegroundColor Gray 
     } 
    } 
    catch{ 
     $actualError = $error[0] 

     throw $actualError 
    } 
    finally{ 
     if($conn) { 
      $conn.Close() 
      $conn.Dispose() 
     } 
    } 

該腳本重命名數據庫和數據文件(假設只有一個數據文件)。

但是,此腳本不會重命名文件的物理名稱,也不支持重命名多個數據文件。

如何還可以重命名數據庫的物理文件?

請注意,數據庫架構不在我的控制之下,我無法在數據庫中添加存儲過程。

[編輯]腳本遠程運行,不在SQL Server框中。

回答

0

根據MSDN你需要包括文件名子句

MODIFY FILE (NAME = logical_file_name, FILENAME = ' new_path/os_file_name ') 

我相信,流量爲:

  1. 重命名數據庫和數據文件名稱
  2. 使數據庫脫機
  3. 重命名物理文件
  4. 更新數據庫文件n埃姆斯在新的物理文件指向
  5. 使數據庫重新聯機

我曾在更新你的腳本如何,我認爲它應該讀去,我沒有地方來測試這一點,所以確保你在一個「安全」的地方進行測試。

$sqlServer = "MySqlServer" 
$currentDbName = "MyDB" 
$sqlConnectionString = "Server='$sqlserver';Database='$currentDbName';Integrated Security=True" 

$PhysicalLocal = "C:\Data\" 
$PhysicalUNC = "\\$sqlserver\C$\Data\" 

$sqlQueries = ` 
    "ALTER DATABASE [$currentDbName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE",` 
    "EXEC sp_renamedb '$currentDbName', '$NewName'",` 
    "ALTER DATABASE [$NewName] SET MULTI_USER", ` 
    "ALTER DATABASE [$NewName] MODIFY FILE (NAME = $currentDbName, NEWNAME = $NewName)", ` 
    "ALTER DATABASE [$NewName] MODIFY FILE (NAME = $($currentDbName)_Log, NEWNAME = $($NewName)_Log)", ` 
    "ALTER DATABASE [$currentDbName] SET offline" 

$sqlQueriesRename = ` 
    "ALTER DATABASE [$NewName] MODIFY FILE (NAME = $NewName, FILENAME = $($PhysicalLocal\$NewName).mdf", ` 
    "ALTER DATABASE [$NewName] MODIFY FILE (NAME = $($NewName)_Log, FILENAME = $($PhysicalLocal\$NewName)_Log.ldf)", ` 
    "ALTER DATABASE [$currentDbName] SET online" 

$conn=new-object System.Data.SqlClient.SQLConnection($sqlConnectionString) 
try { 
    $conn.Open() 

    # Rename the database, leave in offline state 
    $sqlQueries | % { 
     $cmd = $conn.CreateCommand() 
     $cmd.CommandText = $_ 
     $cmd.ExecuteScalar() 
     Write-Host " Executed: $_" -ForegroundColor Gray 
    } 

    # Rename the physical files 
    Rename-Item $(Join-Path -Path $PhysicalUNC -ChildPath $($currentDbName).mdf) $($NewName).mdf 
    Rename-Item $(Join-Path -Path $PhysicalUNC -ChildPath $($currentDbName)_Log.ldf) $($NewName)_Log.ldf 

    # Rename the database files to match the physical ones 
    $sqlQueriesRename | % { 
     $cmd = $conn.CreateCommand() 
     $cmd.CommandText = $_ 
     $cmd.ExecuteScalar() 
     Write-Host " Executed: $_" -ForegroundColor Gray 
    } 
} 
catch{ 
    $actualError = $error[0] 
    throw $actualError 
} 
finally{ 
    if($conn) { 
     $conn.Close() 
     $conn.Dispose() 
    } 
} 
+0

這隻適用於如果我直接在服務器上工作,因爲我必須更新sql文件系統。我沒有精確(我承認),但這個腳本遠程工作。但這可能是答案的第一部分。 –

+0

您可以使用UNC進行遠程物理重命名嗎? –

+0

我想我可能會成功通過遠程方式更改文件系統,無論是按照您的建議使用UNC路徑,還是通過遠程調用powershell命令。雖然不容易,因爲必須打開安全門。 –