A case that requires renaming a database in multiuser mode has recently been encountered, because the database may be used by other users, so direct modification may fail. In this case, we can wait until all users have finished using the database, or switch the database to single-user mode.
Sometimes, we need to take the production environment database to the development environment directly to test the development process. We can rename the database directly on the production environment. In this way, even if you have some SQL is not saved, you can directly from the original database crawl.
There are two important details we must pay attention to.
- The name and data file name (. mdf,.ldf) of the filegroup is not changed when the name database is duplicate.
- The user must switch to the master database context and have SA permissions before the name database can be duplicate.
Use master; go--set the database to single-user-mode exec sp_dboption AdventureWorks, ' one user ', truego--rename database exec sp_renamedb ' AdventureWorks ', ' Adventureworks_new ' go--is setting the database back to multi-user mode exec sp_dboption adventureworks_new, ' single User ', Falsego
Reference documents
- Http://stackoverflow.com/questions/11014343/how-to-rename-database-in-multi-user-mode
- http://blog.sqlauthority.com/2007/09/19/ sql-server-rename-database-to-new-name-using-stored-procedure-by-changing-to-single-user-mode/