监控数据库事物运行情况

SELECT DBT.DATABASE_ID,
       DB_NAME(DBT.DATABASE_ID) '数据库名',      
       DBT.TRANSACTION_ID,
       AT.NAME,
       AT.TRANSACTION_BEGIN_TIME,       
       CASE AT.TRANSACTION_TYPE    
           WHEN 1 THEN '读/写事务'  
           WHEN 2 THEN '只读事务'  
           WHEN 3 THEN '系统事务'  
           WHEN 4 THEN '分布式事务'  
       END 'TRANSACTION类型',           
       CASE AT.TRANSACTION_STATE  
           WHEN 0 THEN '事务尚未完全初始化'  
           WHEN 1 THEN '事务已初始化但尚未启动'  
           WHEN 2 THEN '事务处于活动状态'  
           WHEN 3 THEN '事务已结束。该状态用于只读事务'  
           WHEN 4 THEN '已对分布式事务启动提交进程'  
           WHEN 5 THEN '事务处于准备就绪状态且等待解析'  
           WHEN 6 THEN '事务已提交'  
           WHEN 7 THEN '事务正在被回滚'  
           WHEN 8 THEN '事务已回滚'  
       END  'TRANSACTION状态',       
       ST.SESSION_ID,  
       TT.TEXT AS '最近执行的语句',      
       ES.PROGRAM_NAME        
FROM SYS.DM_TRAN_DATABASE_TRANSACTIONS DBT
LEFT JOIN SYS.DM_TRAN_ACTIVE_TRANSACTIONS AT
       ON DBT.TRANSACTION_ID = AT.TRANSACTION_ID
LEFT JOIN SYS.DM_TRAN_SESSION_TRANSACTIONS ST
       ON AT.TRANSACTION_ID = ST.TRANSACTION_ID
LEFT JOIN SYS.DM_EXEC_SESSIONS ES
       ON ST.SESSION_ID = ES.SESSION_ID
LEFT JOIN SYS.DM_EXEC_CONNECTIONS EC
       ON ES.SESSION_ID = EC.SESSION_ID
OUTER APPLY SYS.DM_EXEC_SQL_TEXT(EC.MOST_RECENT_SQL_HANDLE) TT
WHERE ST.SESSION_ID >= 50 AND DATABASE_ID <> 32767

你可能感兴趣的:(事务,dba,进程,死锁,t-sql)