mssql sqlserver 获取sql脚本历史执行记录的方法分享


摘要:
下文讲述获取sqlserver运行sql脚本历史记录的方法分享,如下所示:
实验环境:sql server 2008 R2


  SELECT TOP 1000  
QS.creation_time as [创建时间], 
SUBSTRING(ST.text,(QS.statement_start_offset/2)+1, 
((CASE QS.statement_end_offset WHEN -1 THEN DATALENGTH(st.text) 
ELSE QS.statement_end_offset END - QS.statement_start_offset)/2) + 1 
) AS [sql脚本],  
ST.text as [执行计划], 
QS.total_worker_time, 
QS.last_worker_time, 
QS.max_worker_time, 
QS.min_worker_time 
FROM 
sys.dm_exec_query_stats QS 
CROSS APPLY 
sys.dm_exec_sql_text(QS.sql_handle) ST 
ORDER BY 
QS.creation_time DESC