Sunday, November 22, 2015

Move SQL Server transaction log file

Move SQL Server transaction logs to new location.  Use the following MSDN article, https://msdn.microsoft.com/en-us/library/ms345483(v=sql.110).aspx.


-- I used the following script to move my database files.  However, initially I ran into an access denied error as the Windows 2012 server modified the permissions on the file as I moved it.  I had to reset the file to read/write.

USE master;
GO

-- Return the logical file name
SELECT name, 
  physical_name AS CurrentLocation,
  state_desc
  FROM sys.master_files
 WHERE database_id = DB_ID(N'tf_prod')
   --AND type_desc = N'LOG';

GO
-- name CurrentLocation state_desc
-- lms_data.mdf E:\Program Files\Microsoft SQL Server\MSSQL11.LAPSQLSERVER\MSSQL\DATA\tf_prod_data.mdf ONLINE
-- lms_log.mdf E:\Program Files\Microsoft SQL Server\MSSQL11.LAPSQLSERVER\MSSQL\DATA\tf_prod_log.ldf         ONLINE

ALTER DATABASE tf_prod SET OFFLINE;
GO

-- Physically more the file to a new location
-- In the following statement, modify the path specified in FILENAME to the new location of the file on your
-- server.  Notice that the NAME is the logical file name found in the NAME field of the query above.
ALTER DATABASE tf_prod
  MODIFY FILE ( NAME = 'lms_log.mdf',
FILENAME = 'F:\MSSQL\Log\tf_prod_log.ldf' );
GO

ALTER DATABASE tf_prod SET ONLINE;
GO

-- Verify the new location
SELECT name, 
  physical_name AS CurrentLocation,
  state_desc
  FROM sys.master_files
 WHERE database_id = DB_ID(N'tf_prod')
   --AND type_desc = N'LOG';

-- name CurrentLocation state_desc
-- lms_data.mdf E:\Program Files\Microsoft SQL Server\MSSQL11.LAPSQLSERVER\MSSQL\DATA\tf_prod_data.mdf ONLINE
-- lms_log.mdf F:\MSSQL\Log\tf_prod_log.ldf ONLINE

No comments:

Post a Comment