Skip to content

Commit 2405c69

Browse files
jasonhornerrwestMSFT
authored andcommitted
Update sys-dm-exec-requests-transact-sql.md
Provided a more real-world example of showing active requests as this is the most likely use case for this dmv.
1 parent 8725e6c commit 2405c69

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

docs/relational-databases/system-dynamic-management-views/sys-dm-exec-requests-transact-sql.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,31 @@ For [!INCLUDE [sssql22-md](../../includes/sssql22-md.md)] and later versions, `s
111111

112112
## Examples
113113

114-
### A. Find the query text for a running batch
114+
### A. Show Active Requests
115115

116-
The following example queries `sys.dm_exec_requests` to find the interesting query and copy its `sql_handle` from the output.
116+
This following example shows all currently running queries in your SQL Data Warehouse, excluding your own session (@@SPID). It uses CROSS APPLY with `sys.dm_exec_sql_text` to retrieve the full query text for each request, and joins with `sys.dm_exec_sessions` to include user and host info. The session_id <> @@SPID filter ensures you don’t see your own query in the results.
117117

118-
```sql
119-
SELECT * FROM sys.dm_exec_requests;
120-
GO
121118
```
122-
123-
Then, to obtain the statement text, use the copied `sql_handle` with system function `sys.dm_exec_sql_text(sql_handle)`.
124-
125-
```sql
126-
SELECT * FROM sys.dm_exec_sql_text(< copied sql_handle >);
127-
GO
119+
SELECT
120+
r.session_id,
121+
r.status,
122+
r.command,
123+
r.start_time,
124+
r.total_elapsed_time / 1000.00 AS elapsed_seconds,
125+
r.cpu_time / 1000.00 AS cpu_seconds,
126+
r.reads,
127+
r.writes,
128+
r.logical_reads,
129+
r.row_count,
130+
s.login_name,
131+
s.host_name,
132+
t.text AS query_text
133+
FROM sys.dm_exec_requests r
134+
JOIN sys.dm_exec_sessions s
135+
ON r.session_id = s.session_id
136+
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
137+
WHERE r.session_id <> @@SPID
138+
ORDER BY r.start_time DESC;
128139
```
129140

130141
### B. Find all locks that a running batch is holding

0 commit comments

Comments
 (0)