Click here to Skip to main content
11,578,954 members (64,398 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: SQL Server
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
Edited 18-May-12 7:08am
(no name)186.5K
losmac at 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 at 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
0 Peter Leow 135
1 Frankie-C 115
2 OriginalGriff 113
3 Richard MacCutchan 85
4 Afzaal Ahmad Zeeshan 85
0 OriginalGriff 933
1 Sergey Alexandrovich Kryukov 740
2 Abhinav S 573
3 F-ES Sitecore 420
4 Dave Kreskowiak 419

Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 18 May 2012
Copyright © CodeProject, 1999-2015
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