Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Detecting Detached Databases Expand / Collapse
Author
Message
Posted Wednesday, August 14, 2002 5:28 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Posted Wednesday, August 14, 2002 5:29 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Posted Wednesday, August 14, 2002 5:57 PM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Monday, September 15, 2014 9:16 AM
Points: 6,784, Visits: 1,895
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
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
Post #39630
Posted Wednesday, August 14, 2002 6:01 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Posted Wednesday, August 14, 2002 6:31 PM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: Moderators
Last Login: Monday, September 15, 2014 9:16 AM
Points: 6,784, Visits: 1,895
Thats correct - once you detach SQL doesnt know anything about it.

Andy
http://www.sqlservercentral.com/columnists/awarren/




Andy
SQLAndy - My Blog!
Connect with me on LinkedIn
Follow me on Twitter
Post #39632
Posted Wednesday, August 14, 2002 8:27 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Posted Monday, August 19, 2002 2:49 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Posted Tuesday, December 18, 2012 6:02 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 9:26 AM
Points: 15, Visits: 150
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
Posted Tuesday, December 18, 2012 6:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:40 PM
Points: 12,897, Visits: 32,105
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
Posted Tuesday, December 18, 2012 11:06 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 9:26 AM
Points: 15, Visits: 150
That old? Hope they haven't been waiting all this time for my bug fix...
:o)
Post #1397940
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse