首页 > 解决方案 > 您如何找到最后一个访问数据库的用户以及该访问的时间戳?

问题描述

我在 Windows Server 2003 机器上有一个 SQL Server 2005 数据库(是的,我们正在尝试迁移到更新的机器)。我可以使用以下代码查看上次访问各种数据库的时间:

select 
    d.name, 
    x1 = (select X1 = max(bb.xx) 
          from 
              (select xx = max(last_user_seek) 
               where max(last_user_seek) is not null 
          union all 
          select xx = max(last_user_scan) 
          where max(last_user_scan) is not null 
          union all 
          select xx = max(last_user_lookup) 
          where max(last_user_lookup) is not null 
          union all 
          select xx = max(last_user_update) 
          where max(last_user_update) is not null) bb) 
from
    master.dbo.sysdatabases d 
left outer join 
    sys.dm_db_index_usage_stats s on d.dbid= s.database_id 
group by 
    d.name

这很好用,但我需要找出哪个用户执行了该访问。我该怎么做?

标签: sqlsql-server-2005

解决方案


您需要打开数据库审计。您应该能够以管理员的责任来执行此操作。


推荐阅读