mssql sqlserver 获取sql脚本及存储过程的执行记录


摘要:
下文通过系统存储过程获取sqlserver自开机启动运行过的sql脚本和存储过程历史记录的方法分享,如下所示:
实验环境:sql server 2008 R2


SELECT TOP 188   
main.creation_time as [创建时间], 
detail.text as [脚本信息], 
main.total_worker_time as [共作业时长], 
main.last_worker_time as [最后一次作业时长], 
main.max_worker_time as [最长作业时长],  
main.min_worker_time as [最小作业时长] 
FROM 
sys.dm_exec_query_stats main  ---主记录表
CROSS APPLY 
sys.dm_exec_sql_text(main.sql_handle) detail 
ORDER BY 
main.creation_time DESC