I have probably in excess of 100 databases on this one SQL Server (2005) instance. I'd like to list them in order of their create dates, or even better, in the order of the date of the latest modification to any table.
Is there a SELECT query I can write, and just as importantly, from what context do I write it and with what permissions do I need to execute it?
You can easily write this query against the sys.databases
catalog view
SELECT * FROM sys.databases
ORDER BY create_date
but unfortunately, there's no equivalent for the "last modification date" that I'm aware of ...
This should work from any database on that server - doesn't matter which database you're in, those sys
catalog views should be accessible from anywhere.
create table #db_name (db_name nvarchar(128), last_change datetime);
exec sp_MSForEachDB 'Use ?; insert into #db_name (db_name, last_change) select ''?'', max(modify_date) from sys.tables'
select * from #db_name order by last_change desc
this is not exactly one select but at least you got what you want. I'm db_owner on one of our databases and probably nothing impressive server-wide so it's not very demanding.
This should get you close to what you want.
SELECT name, crdate
FROM master..sysdatabases