如何使用查询查找所有数据库的最后交易

时间:2019-02-25 13:13:43

标签: sql sql-server

SELECT distinct
(sson.login_name) AS USER__NAME
, sson.host_name AS PC__NAME
, sqltxt.text AS CODE__
, conn.session_id AS SID__
, sson.login_time AS LOGIN__TIME
, sson.last_request_start_time AS TRANSACTION_START_TIME
, REQ.total_elapsed_time AS ELAPSED__TIME
, sson.status AS STATUS_
, sson.database_id AS DB__ID
, dbases.name AS DB__NAME
FROM sys.dm_exec_connections conn 
INNER JOIN sys.dm_exec_sessions sson 
ON conn.session_id = sson.session_id  
INNER JOIN sys.databases dbases
ON sson.database_id=dbases.database_id
INNER JOIN sys.dm_exec_requests REQ
ON REQ.session_id=sson.session_id
CROSS APPLY sys.dm_exec_sql_text(most_recent_sql_handle) AS sqltxt

我使用了此查询。它在2012年及以后开始工作。但这在sql 2008中不起作用。

1 个答案:

答案 0 :(得分:1)

您对版本> sql server 2008 r2的查询,可以通过以下方式尝试使用

SELECT TOP 1

rsh.destination_database_name AS [Database],

rsh.user_name AS [Restored By],

CASE WHEN rsh.restore_type = 'D' THEN 'Database'

WHEN rsh.restore_type = 'F' THEN 'File'

WHEN rsh.restore_type = 'G' THEN 'Filegroup'

WHEN rsh.restore_type = 'I' THEN 'Differential'

WHEN rsh.restore_type = 'L' THEN 'Log'

WHEN rsh.restore_type = 'V' THEN 'Verifyonly'

WHEN rsh.restore_type = 'R' THEN 'Revert'

ELSE rsh.restore_type

END AS [Restore Type],

rsh.restore_date AS [Restore Started],

bmf.physical_device_name AS [Restored From],

rf.destination_phys_name AS [Restored To]

FROM msdb.dbo.restorehistory rsh

INNER JOIN msdb.dbo.backupset bs ON rsh.backup_set_id = bs.backup_set_id

INNER JOIN msdb.dbo.restorefile rf ON rsh.restore_history_id = rf.restore_history_id

INNER JOIN msdb.dbo.backupmediafamily bmf ON bmf.media_set_id = bs.media_set_id

where destination_database_name = 'YourDBName'

and restore_type = 'L'

ORDER BY rsh.restore_date DESC

link