|
----------------------------------------------------------------------------------
--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)
*/
|