Using stored procedures to rename a SQL Server database

Source: Internet
Author: User
Tags name database

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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.