Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 7,2000
»
Administration
»
Detecting Detached Databases
12 posts, Page 1 of 2
1
2
»»
Detecting Detached Databases
Rate Topic
Display Mode
Topic Options
Author
Message
rflewitt
rflewitt
Posted Wednesday, August 14, 2002 5:28 PM
Grasshopper
Group: General Forum Members
Last Login: Sunday, May 23, 2004 6:21 PM
Points: 14,
Visits: 1
I've many databases on the server I've inherited. Some are detached and rather than manually check each db to see which is attached/detached I'm trying to find a sql script method that will tell me. I've searched through the site but cannot find anything that may help. Is it possible? If so, how? Thanks for any help.
Post #6166
rflewitt
rflewitt
Posted Wednesday, August 14, 2002 5:29 PM
Grasshopper
Group: General Forum Members
Last Login: Sunday, May 23, 2004 6:21 PM
Points: 14,
Visits: 1
Sorry - should have said SQL2000
- Richard
Post #39629
Andy Warren
Andy Warren
Posted Wednesday, August 14, 2002 5:57 PM
SSCertifiable
Group: Moderators
Last Login: Thursday, May 09, 2013 12:38 PM
Points: 6,462,
Visits: 1,384
Nothing that I know of. One way would be to pull the list of mdf's, then query against (sysfiles? have to look) to see if its listed. Or you could just try to move or rename them, SQL wont let you if they are in use.
Andy
http://www.sqlservercentral.com/columnists/awarren/
Andy
SQLShare - Learn One New Thing Each Day
SQLAndy - My Professional Blog
Connect with me on LinkedIn
Follow me on Twitter
Post #39630
rflewitt
rflewitt
Posted Wednesday, August 14, 2002 6:01 PM
Grasshopper
Group: General Forum Members
Last Login: Sunday, May 23, 2004 6:21 PM
Points: 14,
Visits: 1
It appears that once detached the db is no longer know to SQL Server (or at least it looks that way to me). I'm taking the sledge hammer approach and searching for mdf's and then as you suggest comparing them against sysdatabases. I guess I could do this in a dos script but that's a bit beyond me at the moment. Maybe someone will come up with something better - thanks, Richard.
Post #39631
Andy Warren
Andy Warren
Posted Wednesday, August 14, 2002 6:31 PM
SSCertifiable
Group: Moderators
Last Login: Thursday, May 09, 2013 12:38 PM
Points: 6,462,
Visits: 1,384
Thats correct - once you detach SQL doesnt know anything about it.
Andy
http://www.sqlservercentral.com/columnists/awarren/
Andy
SQLShare - Learn One New Thing Each Day
SQLAndy - My Professional Blog
Connect with me on LinkedIn
Follow me on Twitter
Post #39632
rflewitt
rflewitt
Posted Wednesday, August 14, 2002 8:27 PM
Grasshopper
Group: General Forum Members
Last Login: Sunday, May 23, 2004 6:21 PM
Points: 14,
Visits: 1
I've knocked together this short script that I run from Master. Forgive any scripting stupidity - I'm not a DBA - but I think you get the general approach. It assumes you have some idea where the .mdf files are. I'm sure someone could improve on it but it does what I need at the moment.
------------------------------
if exists (select name from sysobjects where name ='temp_mdf_files') drop table temp_mdf_files
if exists (select name from sysobjects where name ='temp_mdf_dbnames') drop table temp_mdf_dbnames
create table temp_mdf_files (
full_filename varchar(200))
create table temp_mdf_dbnames (
pos_dbname varchar(200))
insert temp_mdf_files
exec xp_cmdshell 'dir e:\*.mdf /s/b'
insert temp_mdf_dbnames
select reverse(
substring( reverse(
substring(full_filename,1,datalength(full_filename)-4)) ,1,patindex('%\%', reverse(full_filename))-5)
)
from temp_mdf_files
order by 1
select * from temp_mdf_dbnames
where pos_dbname not in (select name from sysdatabases)
drop table temp_mdf_files
drop table temp_mdf_dbnames
Post #39633
rflewitt
rflewitt
Posted Monday, August 19, 2002 2:49 PM
Grasshopper
Group: General Forum Members
Last Login: Sunday, May 23, 2004 6:21 PM
Points: 14,
Visits: 1
This teaches me to test a lot more before going public. The above didn't really work but this version does (I think).
if exists (select name from sysobjects where name ='temp_mdf_files') drop table temp_mdf_files
create table temp_mdf_files (
full_filename varchar(200))
insert temp_mdf_files
exec xp_cmdshell 'dir e:\*.mdf /s/b'
insert temp_mdf_files
exec xp_cmdshell 'dir c:\*.mdf /s/b'
insert temp_mdf_files
exec xp_cmdshell 'dir d:\*.mdf /s/b'
select * from temp_mdf_files
where Upper(full_filename) not in (select Upper(filename) from sysdatabases)
order by 1
drop table temp_mdf_files
Post #39634
Jack 95169
Jack 95169
Posted Tuesday, December 18, 2012 6:02 AM
Grasshopper
Group: General Forum Members
Last Login: Sunday, May 19, 2013 11:48 PM
Points: 13,
Visits: 121
Nearly...
You need to enable XP_cmdshell beforehand
I've also done a bit of tidying up
---- enable these jobs
---- show advanced options
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
---- enable xp_cmdshell
sp_configure 'xp_cmdshell', 1;
GO
RECONFIGURE;
GO
---- hide advanced options
sp_configure 'show advanced options', 0;
GO
RECONFIGURE;
GO
-- based on http://www.sqlservercentral.com/Forums/Topic6166-5-1.aspx
-- create temporary table
create table #temp_mdf_files
(
full_filename varchar(200)
)
--populate the temp table with any MDF files found
insert #temp_mdf_files
exec xp_cmdshell 'dir c:\*.mdf /s/b'
insert #temp_mdf_files
exec xp_cmdshell 'dir d:\*.mdf /s/b'
insert #temp_mdf_files
exec xp_cmdshell 'dir e:\*.mdf /s/b'
--
select
-- exclude the subdirectory name
upper(reverse(substring(reverse(full_filename ), 1,charindex('\', reverse(full_filename ) )-1) )) As MDF_FileName,
full_filename
from #temp_mdf_files
where
--exclude rows which contain system messages or nulls
full_filename like '%\%'
--exclude system databases
and upper(reverse(substring(reverse(full_filename ), 1,charindex('\', reverse(full_filename ) )-1) ))
not in ('DISTMDL.MDF', 'MASTER.MDF', 'MODEL.MDF', 'MSDBDATA.MDF' , 'MSSQLSYSTEMRESOURCE.MDF', 'TEMPDB.MDF' )
-- MDF filename excluding the subdirectory name
and full_filename
not in (select Upper(FILEname) from sys.SYSdatabases)
order by MDF_FileName
-- Housekeeping
drop table #temp_mdf_files
-- disable these jobs
-- show advanced options
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
-- disable xp_cmdshell
sp_configure 'xp_cmdshell', 0;
GO
RECONFIGURE;
GO
-- hide advanced options
sp_configure 'show advanced options', 0;
GO
RECONFIGURE;
GO
Post #1397735
Lowell
Lowell
Posted Tuesday, December 18, 2012 6:06 AM
SSChampion
Group: General Forum Members
Last Login: Yesterday @ 8:39 PM
Points: 11,638,
Visits: 27,713
Jack 95169 (12/18/2012)
Nearly...
You need to enable XP_cmdshell beforehand
I've also done a bit of tidying up
wow Jack you replied to a ten year old thread!
Lowell
--
There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1397737
Jack 95169
Jack 95169
Posted Tuesday, December 18, 2012 11:06 AM
Grasshopper
Group: General Forum Members
Last Login: Sunday, May 19, 2013 11:48 PM
Points: 13,
Visits: 121
That old? Hope they haven't been waiting all this time for my bug fix...
:o)
Post #1397940
« Prev Topic
|
Next Topic »
12 posts, Page 1 of 2
1
2
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.