List all the databases on one SQL Server in the order they were created

前端 未结 3 1290
天涯浪人
天涯浪人 2021-02-07 08:53

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 dat

相关标签:
3条回答
  • 2021-02-07 09:15

    This should get you close to what you want.

    SELECT name, crdate 
    FROM master..sysdatabases
    
    0 讨论(0)
  • 2021-02-07 09:20
    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.

    0 讨论(0)
  • 2021-02-07 09:28

    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.

    0 讨论(0)
提交回复
热议问题