I have a database called "ip_ent_site". And I wanna rename it to "ip_ent_site1" for example.
I have done right click and rename, it is keeping on failed. This is the error message:
Anyone can help?
That is because there are open transactions. If those transactions can be killed, then this can easily be done with this SQL
ALTER DATABASE ip_ent_site
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
sp_rename 'ip_ent_site', 'new_db_name' ,'DATABASE';
GO
ALTER DATABASE new_db_name
SET MULTI_USER
GO
Before renaming, set the database to single user mode MSDN
USE master;
GO
ALTER DATABASE ip_ent_site
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
Then rename it
USE master
GO
ALTER DATABASE ip_ent_site
Modify Name = ip_ent_site1
GO
And then put it back to multi user mode
ALTER DATABASE ip_ent_site1
SET MULTI_USER;
The reason is because the database has to prevent any other connection/transaction to the db while you are renaming it.
A simple script to get a lock on the db:
ALTER DATABASE [ip_ent_site] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [ip_ent_site] MODIFY NAME = [ip_ent_site_new]
GO
ALTER DATABASE [ip_ent_site_new] SET MULTI_USER;
GO
To enable the ability to
right click and rename
the DB:
This the simplest way to rename Database name. Just make sure you have closed all the query windows before running this command.
EXEC sp_renamedb 'old_Name', 'new_Name'
Execute below query
USE master;
GO
sp_renamedb @dbname = 'old_name' , @newname = 'new_name'
执行系统过程sp_who2
找出使用数据库的会话,然后关闭应用程序或使用kill命令sp_who2
会话。
Rename SQLDatabase name Using Query
use [master] go
Alter Database Old_database_name Modify name = New_database_name
or
sp_renameDB 'old_dbname' , 'new_dbname'
我们不应该也更改“逻辑文件名”吗?
ALTER DATABASE [testdb] MODIFY FILE (NAME=N'testdbold ', NEWNAME=N'testdb') GO ALTER DATABASE [testdb] MODIFY FILE (NAME=N'testdbold', NEWNAME=N'testdb_log') GO
Please try this,it will work.
ALTER DATABASE GWPRD_CMCC --old db name
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO ALTER DATABASE GWPRD_CMCC --old db name
MODIFY NAME = GWPRD_CMCC_1 --new db name
GO ALTER DATABASE GWPRD_CMCC_1 --new db name SET MULTI_USER GO
OFFLINE DATABASE
USE [master];
GO
--Disconnect all existing session.
ALTER DATABASE Datatbase_Name SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
--Change database in to OFFLINE mode. ALTER DATABASE Datatbase_Name SET OFFLINE To rename Physical Database Files, use Open SQL Server Management Studio Folder or use the script below
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
Rename MDF FILE dan Log
ALTER DATABASE [Datatbase_Name] MODIFY FILE (Name='Old_Mdf_file_Name', FILENAME='C:\Program Files\Microsoft SQL Server\MSSQL13.SQLMS2016\MSSQL\DATA\New_Mdf_file_Name.mdf')
ALTER DATABASE [Datatbase_Name] MODIFY FILE (Name='Old_log_file_Name', FILENAME='C:\Program Files\Microsoft SQL Server\MSSQL13.SQLMS2016\MSSQL\DATA\New_log_file_Name.ldf')
EXEC xp_cmdshell 'RENAME "C:\Program Files\Microsoft SQL Server\MSSQL13.SQLMS2016\MSSQL\DATA\Old_Mdf_file_Name.mdf", "New_Mdf_file_Name.mdf"'
GO
EXEC xp_cmdshell 'RENAME "C:\Program Files\Microsoft SQL Server\MSSQL13.SQLMS2016\MSSQL\DATA\Old_log_file_Name.ldf", "New_log_file_Name.ldf"'
GO
ONLINE DATABASE
ALTER DATABASE [Datatbase_Name] SET ONLINE
Go
ALTER DATABASE [Datatbase_Name] SET MULTI_USER
Go
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.