Created
March 28, 2013 02:32
-
-
Save ElliotWood/5260063 to your computer and use it in GitHub Desktop.
Rename-SQLDatabase powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Rename-SQLDatabase { | |
param ( | |
[string] $ServerName, | |
[string] $SourceDb, | |
[string] $DestDb | |
) | |
$connection = New-Object System.Data.SqlClient.SqlConnection | |
$command = New-Object System.Data.SqlClient.SqlCommand | |
$connection.ConnectionString = "Server=$ServerName;Integrated Security=True;" | |
$command.CommandText = "ALTER DATABASE [$SourceDb] SET OFFLINE WITH ROLLBACK IMMEDIATE;ALTER DATABASE [$SourceDb] SET ONLINE;EXEC sp_renamedb [$SourceDb], [$DestDb];" | |
$command.Connection = $connection | |
$command.Connection.Open(); | |
$command.ExecuteNonQuery(); | |
$command.Connection.Close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment