日期:2014-05-18  浏览次数:20597 次

数据库最近更新时间?
如何一次性查询每一个数据库的modify_date最大时间
比如:select max(modify_date) from Accounting.sys.objects
可以查询Accounting这个数据库的max(modify_date),现在有本地有一百多个数据库,怎么求所有的modify_date?

------解决方案--------------------
e.g.
SQL code

if object_id('tempdb..#') Is not null
    Drop Table #

Create table #(dbname sysname,LastModifyDate datetime)
Insert Into #
Exec sys.sp_MSforeachdb  'Select ''?'', max(modify_date) From ?.sys.objects'
Select * from # Order by LastModifyDate Desc

------解决方案--------------------
SQL code
declare @T table(DBName sysname,modify_date datetime)
insert @T exec sp_msforeachdb 'select ''?'',max(modify_date) from ?.sys.objects'

select * from @T

------解决方案--------------------
SQL code
exec sp_msforeachdb
'use [?]if ''?'' not in(''master'', ''model'', ''msdb'', ''tempdb'' )
select ''[?]'',max(modify_date) from sys.objects
'

------解决方案--------------------
SQL code

create table #tb(dbname varchar(50),date datetime)
insert into #tb
exec sp_msforeachdb 'select ''?'' as dbname,max(modify_date) as date from ?.sys.objects'
select * from #tb
drop table #tb