In practice, database files grows everyday. Sometimes it occupy the complete disk and we may end up in unsufficeint mamroy - leading to unexpected results.
In this blog, I'll explain how to transfer database files from one drive to another. To explain, I'll create a test database TestDB. Here is the command:
USE [master]
GO
IF DB_ID(N'TestDB') IS NULL
BEGIN
CREATE DATABASE [TestDB] ON PRIMARY
(
NAME = N'TestDB'
,FILENAME = N'C:\MSSQL\Data\TestDB.mdf'
,SIZE = 3MB
,MAXSIZE = 2048GB
,FILEGROWTH = 1024KB
)
LOG ON
(
NAME = N'TestDB_log'
,FILENAME = N'C:\MSSQL\Data\TestDB.ldf'
,SIZE = 3MB
,MAXSIZE = 2048GB
,FILEGROWTH = 10%
)
END
GO
Now check existing files location:
USE TestDB
GO
SELECT name, physical_name, state_desc
FROM TestDB.sys.master_files
WHERE database_id = DB_ID(N'TestDB')
Now the database is created, you can create some tables and enter some data into the database, if you wish, Otherwise proceed like this:
Step1: Make the database OFFLINE.
USE [master]
GO
ALTER DATABASE TestDB SET OFFLINE
Step2: Move file Physically
Now you need to move that file physically from existing folder to the new the location. Open the parent folder (Here 'C:\MSSQL\DATA') , You can see both mdf and ldf files', make sure that you cut the appropriate file, in this example it is the Log file. Cut that "TestDB_log.LDF" file and paste it on "D:\MSSQL\Data"
Step3: Update the system reference
Once you move the file physically , you need to update the system reference using the ALTER DATABASE .. MODIFY FILE Command as shown below:
ALTER DATABASE TestDB
MODIFY FILE (
NAME ='TestDB_log'
,FILENAME = 'D:\MSSQL\Data\TestDB.LDF'
)
Step5 : Make database ONLINE
Last step is to make database online as shown below:
ALTER DATABASE TestDB SET ONLINE
GO
We are done. Now check existing files location using query mentioned above.
In this blog, I'll explain how to transfer database files from one drive to another. To explain, I'll create a test database TestDB. Here is the command:
USE [master]
GO
IF DB_ID(N'TestDB') IS NULL
BEGIN
CREATE DATABASE [TestDB] ON PRIMARY
(
NAME = N'TestDB'
,FILENAME = N'C:\MSSQL\Data\TestDB.mdf'
,SIZE = 3MB
,MAXSIZE = 2048GB
,FILEGROWTH = 1024KB
)
LOG ON
(
NAME = N'TestDB_log'
,FILENAME = N'C:\MSSQL\Data\TestDB.ldf'
,SIZE = 3MB
,MAXSIZE = 2048GB
,FILEGROWTH = 10%
)
END
GO
Now check existing files location:
USE TestDB
GO
SELECT name, physical_name, state_desc
FROM TestDB.sys.master_files
WHERE database_id = DB_ID(N'TestDB')
Now the database is created, you can create some tables and enter some data into the database, if you wish, Otherwise proceed like this:
Step1: Make the database OFFLINE.
USE [master]
GO
ALTER DATABASE TestDB SET OFFLINE
Step2: Move file Physically
Now you need to move that file physically from existing folder to the new the location. Open the parent folder (Here 'C:\MSSQL\DATA') , You can see both mdf and ldf files', make sure that you cut the appropriate file, in this example it is the Log file. Cut that "TestDB_log.LDF" file and paste it on "D:\MSSQL\Data"
Step3: Update the system reference
Once you move the file physically , you need to update the system reference using the ALTER DATABASE .. MODIFY FILE Command as shown below:
ALTER DATABASE TestDB
MODIFY FILE (
NAME ='TestDB_log'
,FILENAME = 'D:\MSSQL\Data\TestDB.LDF'
)
Step5 : Make database ONLINE
Last step is to make database online as shown below:
ALTER DATABASE TestDB SET ONLINE
GO
We are done. Now check existing files location using query mentioned above.