What database is an mdf attached to?

  • Whilst doing some spring cleaning on one of my SQL Servers I found some .mdf and .ldf which filenames I don't recognise as being attached to an SQL database. Is there a quick way to "search" within the SQL Server to establish whether the mdf/ldf files are attached to a database so I know whether they can be deleted? Or I guess another way of putting this is whether there's a way of listing out all of the mdf/ldf files which are currently attached to the existing databases?

  • i Guess You can use the below Query to verify what are the datbase that are there in the server and where the mdf and ldf files are there

    SELECT db_name(database_id) as DatabaseName,name,type_desc,physical_name,*

    FROM sys.master_files

  • Perfect...thanks. I was looking at using sp_helpdb but that meant running the procedure for every single database......and we have quite a few. Your SQL statement makes life a lot easier 🙂

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply