Skip to content

Instantly share code, notes, and snippets.

@SteGriff
Last active June 10, 2021 13:53
Show Gist options
  • Save SteGriff/35caa1976f240850a74bed8ffedf7649 to your computer and use it in GitHub Desktop.
Save SteGriff/35caa1976f240850a74bed8ffedf7649 to your computer and use it in GitHub Desktop.
Running SQL queries
SELECT
d.name,
t.text,
r.total_elapsed_time/1000 as [Time],
r.blocking_session_id,
SUBSTRING(t.text,r.statement_start_offset/2 +1,
(CASE WHEN r.statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), t.text)) * 2
ELSE r.statement_end_offset END -
r.statement_start_offset)/2)
AS 'TSQL-Query'
FROM sys.dm_exec_requests r
JOIN sys.databases d on d.database_id = r.database_id
CROSS APPLY sys.dm_exec_sql_text(sql_handle) t
order by time desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment