Click here to Skip to main content
13,007,028 members (59,669 online)
Rate this:
Please Sign up or sign in to vote.
See more:
How can we get the attached file name (Attach As) from mdf or ldf file in SQL Server
Keep in mind, that we want get this name before attaching a file name to SQL Server Instance

[moved from answer]

now when you attach this database
EXEC sp_attach_db @dbname = N'PUBLISHER',
@filename1 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs.mdf',
@filename2 = N'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs_log.ldf'

here PUBLISHER will be name that will display in the SQL Server Databases List

Now, at the time, I Detached the Database.
and when i Attach the Database, I don't want the Name, SQL Server must automatically picks the Last Name 'PUBLISHER' from file,
as when we attach the database by using SQL Server Management Studio Express,
as we right click on Databases --> Attach
A Dialog Appears
Here i browse the mdf file, SQL SErver automatilly gets the name PUBLISHER in attach as field, How I get this using or By using sp_attach_db Options

[/moved from answer]
Posted 16-May-12 21:07pm
Updated 18-May-12 7:08am
Maciej Los254.6K
losmac 18-May-12 7:03am
Not clear...
Do you want to know the name of each file attached to sql server or do you want to search for it?
losmac 18-May-12 13:09pm
Always use "Improve question" button to update your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170628.1 | Last Updated 18 May 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100