While preparing for the training course of Microsoft SQL Server 2005/2008 Query Optimization and & Performance Tuning, I needed to find out where my database files are stored on my hard drive. It is when following script came in handy to find the location of the data file using T-SQL.
SELECT SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1) DataFileLocation FROM master.sys.master_files WHERE database_id = 1 AND FILE_ID = 1
Resultset:
DataFileLocation -------------------------------------------------------------------------- C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\ (1 row(s) affected)
Above script provided exact location of where my data files of master database is. In fact, you can use it to find any other path of database as needed. If you want, you can change the master. mdf to any other file and you can easily find the location of the other database as well.
Here are few other blog posts related to this topic which you may find interesting:
- What is Copy Only Backup in SQL Server? – Interview Question of the Week #128
- SQL SERVER – Maintenance Plan – Backup Files Not Getting Deleted
- How to Get Status of Running Backup and Restore in SQL Server? – Interview Question of the Week #113
- SQL SERVER – Fix Error – Cannot use the backup file because it was originally formatted with sector size 4096 and is now on a device with sector size 512
I personally feel this is a very handy trick to find location of data file using T-SQL. If you have any other ticks available, do post in the comment and I will publish the blog post with due credit.
Reference: Pinal Dave (https://blog.sqlauthority.com)
27 Comments. Leave new
Thanks for sharing this useful script, but I would like to ask one more thing that how could we know the name of the data file (.mdf file) of any database. let us know If there is any scrip for that.. Thanks.
Hi Abdul,
Use the sp_helpdb to get the name of database files.
Regards,
Pinal Dave
Hey Pinal,
Hope you are doing great!
is there any Chance to Get Access to Power point Presenatations or Any Videos.
Please guide me
Thanks
Have a great day ahead.
Thanks for the script.
Ramdas
Hi pinal,
really its very good script. I need to know more abt sp_help commands, can we have list of all those? It will be very useful ! Thanks
In SQL Server help file, goto index and type sp_
It will list out all procedures that start with sp_
Hi Pinal,
I use for same purpose: – sp_helpdb ‘master’ OR SELECT physical_name FROM master.sys.master_files WHERE name like ‘master%’ AND FILE_ID = 1– for DATA file.
Is it good?
Code which you provide need database_id = 1, So firdt need to find out it using ‘sp_helpdb’
SELECT SUBSTRING(physical_name, 1,
CHARINDEX(N’master.mdf’,
LOWER(physical_name)) – 1) DataFileLocation
FROM master.sys.master_files
WHERE database_id = 1 AND FILE_ID = 1
Thanks
Vasant Jagtap
Some of us are stuck with SS2K. How would you modify this script for SS2K?
Hello Tom,
Use the sysfiles table as beow:
SELECT [filename] FROM dbName.dbo.sysfiles
Or you can use sp_helpdb dbName
Regards,
Pinal Dave
hi pinal
i wana to store excel csv file data in sql server 2005 .
plz help me how this possible
hi pinal
i wana import csv sheets data in sql server 2005 without using bulk cpoy.if u hv diff way plz do somethink
regards
vicky
web deveolper
If you dont want to use bcp, use BULK INSERT method
Refer this for more informations
Hi Pinal:
How could I know how much free space my SQL database has right now? Also what is the maximum size of SQL 2005 database can be.
I have a server with more than 3 TB HD space. Already 8 databases are there and more are going to be in. Mots of them are 100 to 150 GB. I’m afraid about thinking SQL 2005’s space capacity.
Thanks in advance.
Maksuda…
use master
SELECT * FROM sys.database_files
Hi, how can I find the address of a database? I the website like and name, do I need anything else?
What do you mean by it? Do you want to know the name?
select db_name()
To selam – How does that give you the free space?
it should be Selvam :-)
THe answer to Maksuda lies here:
https://blog.sqlauthority.com/2010/02/08/sql-server-find-the-size-of-database-file-find-the-size-of-log-file/
that doesnt give you free space. sorry. i’ll butt out now
please remove that article form the site
Can any one tell that how should we query data in Dat Extenstion File in via Transact SQL??
i just need to know Select Query like its not working
Select * from ‘D:\Data\abc.dat’
Good script. I wonder how I can have the same result with a batch script
Thanks so very much!