×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

change name of file of a database
2

change name of file of a database

change name of file of a database

(OP)
hello
by ALTER DATABASE Modify Name, we can change name of a database. but the name of
phisical file don't be renamed. how could it be do?
thanks.


RE: change name of file of a database

You can't

Borislav Borissov
VFP9 SP2, SQL Server

RE: change name of file of a database

You can't do this easily, but it can be done.

This will be a multi-step process, and could possibly take a long time to run depending on the size of your database.

1. Backup your database.
2. Drop your database.
3. Restore the database using the "With Move" option.

You can skip step #2 if you want to change the database name (as you refer to it in tSQL).

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

RE: change name of file of a database

yes you can do it - and easy enough to do.

requires following steps
  1. set database offline
  2. alter database modify filename to new name
  3. physically rename file (through xp_cmdshell or manually outside tsql)
  4. set database online
demo example below

CODE

EXEC sp_configure 'show advanced options', 1;  
GO  
RECONFIGURE;  
GO  
EXEC sp_configure 'xp_cmdshell', 1;  
go
RECONFIGURE;  
GO  

CREATE DATABASE [demo_rename]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'demo_rename', FILENAME = N'C:\sql_server_data\MSSQL12.SQL2014\MSSQL\DATA\demo_rename.mdf' , SIZE = 5120KB , FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'demo_rename_log', FILENAME = N'C:\sql_server_data\MSSQL12.SQL2014\MSSQL\DATA\demo_rename_log.ldf' , SIZE = 2048KB , FILEGROWTH = 10%)
GO
ALTER DATABASE [demo_rename] SET  MULTI_USER 
GO
select name, mf.physical_name
from sys.master_files mf
where db_name(mf.database_id) = 'demo_rename'

alter database demo_rename set single_user with rollback immediate
alter database demo_rename set offline

alter database demo_rename modify file (name=N'demo_rename', filename=N'C:\sql_server_data\MSSQL12.SQL2014\MSSQL\DATA\demo_newname.mdf')
exec sys.xp_cmdshell 'move C:\sql_server_data\MSSQL12.SQL2014\MSSQL\DATA\demo_rename.mdf C:\sql_server_data\MSSQL12.SQL2014\MSSQL\DATA\demo_newname.mdf'

alter database demo_rename set online


select name, mf.physical_name
from sys.master_files mf
where db_name(mf.database_id) = 'demo_rename'

EXEC sp_configure 'xp_cmdshell', 0;  
go
RECONFIGURE;  
GO  
drop database demo_rename 

the physical renaming of the datafiles can be done either outside or through a xp_cmdshell command to rename them.

Regards

Frederico Fonseca
SysSoft Integrated Ltd
www.syssoft-int.com

FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close