I am trying to audit the network, to find and clean out old databases that may have been tested and forgotten.
Right now, I know I can use SQLCMD -L
to see the servers and instances.
I can use exec sp_helpdb to get the Server name, name of the db, size, owner, created, and status if Management Studio is connected to the server(s).
Both of these on their own is great, but they require connecting to each server in MGMT Studio, then running the below query against a Registered Server Group.
I was hoping to create a loop of the following query to go through all the databases that SQLCMD -L
has found, so that I can send out audit results for cleanup, possibly through SQLCMS/OSQL, to avoid the manual connection process. I also may have to work out the appropriate log on account to them, so if there's option for that in this mess, that would be great as well (Sa, Win Auth, SQL Auth, etc.)
If anyone has some suggestion on solution to run it as an automated process, it would be greatly appreciated.
create table #temp
(
name sysname
,db_size nvarchar(13)
,owner sysname
,dbid smallint
,created smalldatetime--nvarchar(11)
,status nvarchar(600)
,compatibility_level tinyint
)
go
insert into #temp
exec sp_helpdb
ALTER TABLE #temp
DROP COLUMN dbid
ALTER TABLE #temp
DROP COLUMN status
ALTER TABLE #temp
DROP COLUMN compatibility_level
select * from #temp
where name not like'master%'
AND name not like 'model%'
AND name not like 'msdb%'
AND name not like 'ems_master%'
AND name not like 'tempdb%'
AND name not like 'ReportServer%'
AND name not like 'HR'
AND name not like 'EMS_Personnel%'
order by created
drop table #temp