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.
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.
Reference: Pinal Dave (http://blog.SQLAuthority.com)












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
http://beyondrelational.com/blogs/madhivanan/archive/2010/03/17/bulk-insert-to-table-with-specific-columns.aspx
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:
http://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!
thanks Again.
[...] Find Location of Data File Using T-SQL [...]
This script works better
list database name, owner,filename and location
use master
Select
sys.databases.name as [database name],
suser_sname(owner_sid) as [owner name],
sys.master_files.name as [file name],
sys.master_files.physical_name as [Location]
from sys.databases
right join sys.master_files on sys.databases.database_id = sys.master_files.database_id
order by location,sys.database.name