你的位置:首页 > 软件开发 > 数据库 > 如何rename sqlserver database

如何rename sqlserver database

发布时间:2016-06-08 16:00:13
ProblemSometimes there is a need to change the name of your database whether this is because the original name was based on some other proje ...

如何rename sqlserver database

Problem

Sometimes there is a need to change the name of your database whether this is because the original name was based on some other project that is no longer relevant to the data stored in the database or maybe it was because you restored a database and at the time just gave it a temporary name, but long term it ended up being a database that needed to remain on your system. 

Regardless of the reason there may come a time when you want to or need to rename a database.  In this tip we look at a couple different options.

Solution

There are a few ways of doing this.

Option 1 - Rename SQL Server Database using T-SQL

This command works for SQL Server 2005, 2008, 2008R2 and 2012:

ALTER DATABASE oldName MODIFY NAME = newName

Option 3 - Rename SQL database using detach and attach

Use the detach and attach feature of SQL Server to detach the database first and when you reattach the database you give the database a different name. This can be done by using the SSMS or you can do this by using the following commands:

EXEC sp_detach_db 'oldName', 'true'

Here we are reattaching database "Test", but before we finish we change the name of the database to "Test2".

如何rename sqlserver database

原标题:如何rename sqlserver database

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。