DECLARE @startdt VARCHAR(10)
DECLARE @enddt VARCHAR(10)
– – Provide start and end date
SET @startdt = ‘2019-03-05’
SET @enddt = ‘2022-04-05’
select CONCAT(‘IDWWM’, ‘~~’,
sess.original_login_name,’~~’,
AppId,’~~’,
sess.client_interface_name,’~~’,
convert(VARCHAR,d.last_execution_time,120),’~~’,
(d.total_elapsed_time/d.execution_count)/1000000.0,’~~’,
(total_logical_reads+total_physical_reads+total_logical_writes)/d.execution_count,’~~’,
0,’~~’,
convert(VARCHAR,(d.last_execution_time + d.total_elapsed_time/86400000000),120),’~~’,
convert(VARCHAR,d.last_execution_time,120),’~~’,
0,’~~’,
row_number() over (ORDER BY total_worker_time/d.execution_count desc),’~~’,
(total_worker_time/execution_count)/1000000.0,’~~’,
(total_logical_reads+total_physical_reads+total_logical_writes),’~~’,
tb4.ampcputime,’~~’,
tb4.TotalIOCount,’~~’,
d.total_elapsed_time/1000000.0,’~~’,
OBJECT_SCHEMA_NAME(d.object_id,d.database_id), ‘~~’,
db.name,’~~’,
REPLACE(CAST(text as NVARCHAR(MAX)), CHAR(10), ‘ ‘),’~~’,
execution_count
)
FROM sys.dm_exec_procedure_stats AS d
CROSS APPLY sys.dm_exec_sql_text(d.sql_handle) st
INNER JOIN
(
SELECT DISTINCT
a.last_execution_time,
SUM(total_worker_time)/1000000.0 as AMPCPUTIME,
(SUM(total_logical_reads)+sum(total_physical_reads)+sum(total_logical_writes)) AS TOTALIOCOUNT
FROM
sys.dm_exec_procedure_stats AS a
WHERE
cast(a.last_execution_time as date) BETWEEN CAST(@startdt+’ 00:00:00:00′ as DATETIME) AND CAST(@enddt+’ 00:00:00:00′ AS DATETIME)
GROUP BY a.last_execution_time
)TB4 ON TB4.last_execution_time = d.last_execution_time
inner join sys.databases db
on ( db.database_id = d.database_id)
LEFT JOIN (
SELECT DISTINCT es.program_name as AppId, es.client_interface_name,es.original_login_name,sp.dbid
FROM sys.dm_exec_sessions es inner join sys.sysprocesses sp
on es.session_id = sp.spid
) AS Sess ON Sess.dbid = d.database_id;