SQL: Database Rename from CLI

SQL: Database Rename from CLI

Below is a snippet that I have used to rename a database and its logical file names

# Change Logical file names
invoke-sqlcmd -ServerInstance localhost -database TESTDB -Query "ALTER DATABASE TESTDB MODIFY FILE ( NAME = TESTDB, NEWNAME = NEWDB)"
invoke-sqlcmd -ServerInstance localhost -database TESTDB -Query "ALTER DATABASE TESTDB MODIFY FILE ( NAME = TESTDB_log, NEWNAME = NEWDB_log)"
# Change Database global name
invoke-sqlcmd -ServerInstance localhost -database TESTDB -Query "ALTER DATABASE TESTDB MODIFY NAME = NEWDB"
Advertisements