MSSQL database logical file name modification and view

Source: Internet
Author: User
Tags microsoft sql server mssql mssqlserver

What is the logical file name?

The program you use is connected with the database name, and then you right-click the property in your database, click "File" in the upper left corner, you can see that the database name and logical file name is not the same, you can look at the path of your database file

example, SQL query and modify database logical filename, mobile database storage path Sample.

The code is as follows

----------------------------------------------------------------------------------

--author:htl258 (Tony)

--Date:2010-06-26 21:51:30

--Version:microsoft SQL Server 2008 (RTM)-10.0.1600.22 (Intel X86)

--April 9 2008 14:43:34

--Copyright (c) 1988-2008 Microsoft Corporation

--Developer Edition on Windows NT 5.1 <X86> (build 2600:service Pack 2)

--blog:http://blog.csdn.net/htl258 (reprint keep this information)

--Subject:sql Query and modify database logical file name, mobile database storage Path Example
----------------------------------------------------------------------------------

Use MyDB

Go

--1. Querying the logical file name of the current database
SELECT file_name (1) as ' file name 1 ', file_name (2) as ' file name 2 ';

--or query with the following statement:
--select name from Sys.database_files

/*

File name 1 file name 2

------------------- --------------------

MyDB Mydb_log

(1 rows affected)

*/

--2. To modify the logical name of a data file or log file
ALTER DATABASE [MyDB]

MODIFY FILE (NAME = mydb, NEWNAME = new_mydb)

ALTER DATABASE [MyDB]

MODIFY FILE (NAME = mydb_log, NEWNAME = New_mydb_log)

--the name after the query change:
SELECT file_name (1) as ' file name 1 ', file_name (2) as ' file name 2 ';

/*

File name 1 file name 2

------------------- --------------------

New_mydb New_mydb_log

(1 rows affected)

*/

--3. To move a data file or log file to a new location, first offline the file to the appropriate location, and then online

--detailed reference to the following steps
--3.1 query data file or log file current storage path:
SELECT Physical_name from Sys.database_files

/*

Physical_name

-------------------------------------------------------------------------------------------

D:/program Files/microsoft SQL Server/mssql10. Mssqlserver/mssql/data/mydb.mdf

D:/program Files/microsoft SQL Server/mssql10. Mssqlserver/mssql/data/mydb_log.ldf

(2 rows affected)

*/

--3.2 Move Path
Use [master]

Go

--3.2.1 Close All Processes
DECLARE @str VARCHAR (500);

SET @str = ';

SELECT @str = @str + ' KILL ' +rtrim (spid) + '; ' +char (+char) (10)

From master.dbo.sysprocesses

WHERE dbid=db_id (' mydb ');

EXEC (@str);

Go

--3.2.2 Settings Database offline
ALTER DATABASE [mydb] SET OFFLINE

Go

--3.2.3 mobile data files and log files (to ensure data security, we recommend that move to copy, copy successful and then delete the source file)

EXEC Master.. xp_cmdshell ' move ' d:/program files/microsoft SQL server/mssql10. Mssqlserver/mssql/data/mydb.mdf "" E:/dbtest ", no_output

EXEC Master.. xp_cmdshell ' move ' d:/program files/microsoft SQL server/mssql10. Mssqlserver/mssql/data/mydb_log. LDF "" E:/dbtest ", no_output

Go

--3.2.4 set a new storage path
ALTER DATABASE [MyDB] MODIFY FILE (NAME = New_mydb,filename = ' E:/dbtest/mydb.mdf ')

ALTER DATABASE [MyDB] MODIFY FILE (NAME = New_mydb_log,filename = ' E:/dbtest/mydb_log.ldf ')

Go

--3.2.5 Settings Database Online
ALTER DATABASE [mydb] SET ONLINE

Go

--3.2.6 Query for new paths
Use MyDB

Go

SELECT Physical_name from Sys.database_files

/*

Physical_name

------------------------------

E:/dbtest/mydb.mdf

E:/dbtest/mydb_log.ldf

(2 rows affected)

*/

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.