Modify database name in MSSQL

Source: Internet
Author: User
In MSSQL, if you want to modify the name of a database, you can use the following methods. Method 1: Use SP_RENAMEDB to implement the stored procedure. Syntax: sp_renamedb [@ dbname] old_name, [@ newname] new_name example: I need to change the name of database Test_1 to Testusemast

In ms SQL, if you want to modify the name of a database, you can use the following methods. Method 1: Use SP_RENAMEDB to implement the stored procedure. Syntax: sp_renamedb [@ dbname =] 'old _ name', [@ newname =] 'new _ name' example: I need to change the database Test_1 name to Test use mast

In ms SQL, if you want to modify the name of a database and the Hong Kong server, you can use the following methods.

 

Method 1: Use SP_RENAMEDB to implement the stored procedure.

Syntax: sp_renamedb [@ dbname =] 'old _ name', [@ newname =] 'new _ name'

Example: I need to change the database Test_1 name to Test

Use mastergoexec sp_renamedb @ dbname = 'test _ 1', @ newname = 'test'; go

 

Sometimes the following error is reported when you execute this script:

Message 5030, level 16, status 2, no record filing space, 1st lines

The database cannot be locked with exclusive locks to perform this operation.

In this case, there are other programs or users connecting to the database. You must ensure that no user is using the database. You can set the database to the single-user mode. Choose database Properties> connection> restrict access, and select SIGLE_USER. Run the preceding statement to modify the database name.

Of course, you can also use scripts to solve this problem. Check which sessions are currently connected to the database. If there are no records, you can directly execute the above script.

Select spid from master. dbo. sysprocesses WWHERE dbid = DB_ID ('test1 ');

Otherwise, run the KILL command to KILL these session processes.

KILL SPID

The script below can better handle the above problems. You do not need to perform too many additional operations and directly use the script to complete the task.

 

Use masterexec sp_dboption @ dbname = 'olddbname', @ optname = 'single user', @ optvalue = 'true' exec sp_renamedb @ dbname = 'olddbname ', @ newname = 'newdbname' exec sp_dboption @ dbname = 'newdbname', @ optname = 'single user', @ optvalue = 'false' GO

 

 

Check whether the modification is successful.

SELECT name, database_idFROM sys. databasesWHERE name = N 'dbname ';

 

Note: SQL Server 2008 and later versions of Microsoft SQL Server will delete this function. Avoid using this function in new development work, and start to modify the application that is currently using this function.

Method 2: Use the alter database statement to modify the DATABASE name.

USE master; goalter database Test1 modify name = Test; GO

 

Method 3: Use Microsoft SQL Server Management Studio, select Test1, right-click, and select "RENAME "; NOTE: If other user sessions are connected to the database, an error is reported. You must first kill the session processes or make the database in single-user mode, and then perform the preceding operations. Otherwise, an error is reported.

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.