Skip to content

Commit ffa60c9

Browse files
authored
Merge pull request #32081 from rwestMSFT/rw-1015-fix-9906
Refresh sys.dm_broker_queue_monitors article (PR 9906)
2 parents f97e41c + 621ba96 commit ffa60c9

1 file changed

Lines changed: 50 additions & 54 deletions

File tree

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
title: "sys.dm_broker_queue_monitors (Transact-SQL)"
3-
description: sys.dm_broker_queue_monitors (Transact-SQL)
3+
description: sys.dm_broker_queue_monitors returns a row for each queue monitor in the instance.
44
author: rwestMSFT
55
ms.author: randolphwest
6-
ms.date: "02/24/2023"
6+
ms.date: 10/15/2024
77
ms.service: sql
88
ms.subservice: system-objects
99
ms.topic: "reference"
@@ -18,61 +18,57 @@ dev_langs:
1818
- "TSQL"
1919
---
2020
# sys.dm_broker_queue_monitors (Transact-SQL)
21+
2122
[!INCLUDE [SQL Server](../../includes/applies-to-version/sqlserver.md)]
2223

23-
Returns a row for each queue monitor in the instance. A queue monitor manages activation for a queue.
24-
24+
Returns a row for each queue monitor in the instance. A queue monitor manages activation for a queue.
25+
26+
| Column name | Data type | Description |
27+
| --- | --- | --- |
28+
| `database_id` | **int** | Object identifier for the database that contains the queue that the monitor watches. Nullable. |
29+
| `queue_id` | **int** | Object identifier for the queue that the monitor watches. Nullable. |
30+
| `state` | **nvarchar(32)** | State of the monitor. Nullable. This value is one of the following options:<br /><br />`INACTIVE`<br />`NOTIFIED`<br />`RECEIVES_OCCURRING` |
31+
| `last_empty_rowset_time` | **datetime** | Last time that a `RECEIVE` from the queue returned an empty result. Nullable. |
32+
| `last_activated_time` | **datetime** | Last time that this queue monitor activated a stored procedure. Nullable. |
33+
| `tasks_waiting` | **int** | Number of sessions that are currently waiting within a `RECEIVE` statement for this queue. Nullable.<br /><br />**Note:** This number includes any session executing a receive statement, regardless of whether the queue monitor started the session. This is for when you use `WAITFOR` together with `RECEIVE`. In other words, these tasks are waiting for messages to arrive on the queue. |
34+
35+
## Permissions
36+
37+
[!INCLUDE [sssql19-md](../../includes/sssql19-md.md)] and earlier versions require `VIEW SERVER STATE` permission on the server.
38+
39+
[!INCLUDE [sssql22-md](../../includes/sssql22-md.md)] and later versions require `VIEW SERVER PERFORMANCE STATE` permission on the server.
40+
41+
## Examples
42+
43+
### A. Current status queue monitor
2544

26-
|Column name|Data type|Description|
27-
|-----------------|---------------|-----------------|
28-
|**database_id**|**int**|Object identifier for the database that contains the queue that the monitor watches. NULLABLE.|
29-
|**queue_id**|**int**|Object identifier for the queue that the monitor watches. NULLABLE.|
30-
|**state**|**nvarchar(32)**|State of the monitor. NULLABLE. This is one of the following:<br /><br /> **INACTIVE**<br /><br /> **NOTIFIED**<br /><br /> **RECEIVES_OCCURRING**|
31-
|**last_empty_rowset_time**|**datetime**|Last time that a RECEIVE from the queue returned an empty result. NULLABLE.|
32-
|**last_activated_time**|**datetime**|Last time that this queue monitor activated a stored procedure. NULLABLE.|
33-
|**tasks_waiting**|**int**|Number of sessions that are currently waiting within a RECEIVE statement for this queue. NULLABLE.<br /><br /> Note: This number includes any session executing a receive statement, regardless of whether the queue monitor started the session. This is if you use WAITFOR together with RECEIVE. Basically, these tasks are waiting for messages to arrive on the queue.|
34-
35-
## Permissions
36-
Requires VIEW SERVER STATE permission on the server.
37-
38-
### Permissions for SQL Server 2022 and later
45+
This scenario provides the current status of all message queues.
3946

40-
Requires VIEW SERVER PERFORMANCE STATE permission on the server.
47+
```sql
48+
SELECT DB_NAME() AS [Database_Name],
49+
s.[name] AS [Service_Name],
50+
sch.[name] AS [Schema_Name],
51+
q.[name] AS [Queue_Name],
52+
ISNULL(m.[state], N'Not available') AS [Queue_State],
53+
m.tasks_waiting,
54+
m.last_activated_time,
55+
m.last_empty_rowset_time,
56+
(SELECT COUNT(1)
57+
FROM sys.transmission_queue AS t6
58+
WHERE t6.from_service_name = s.[name]) AS Tran_Message_Count
59+
FROM sys.services AS s
60+
INNER JOIN sys.databases AS d
61+
ON d.database_id = DB_ID()
62+
INNER JOIN sys.service_queues AS q
63+
ON s.service_queue_id = q.[object_id]
64+
INNER JOIN sys.schemas AS sch
65+
ON q.[schema_id] = sch.[schema_id]
66+
LEFT OUTER JOIN sys.dm_broker_queue_monitors AS m
67+
ON q.[object_id] = m.queue_id
68+
AND m.database_id = d.database_id;
69+
```
4170

42-
## Examples
43-
44-
### A. Current status queue monitor
45-
This scenario provides the current status of all message queues.
46-
47-
```
48-
SELECT t1.name AS [Service_Name], t3.name AS [Schema_Name], t2.name AS [Queue_Name],
49-
CASE WHEN t4.state IS NULL THEN 'Not available'
50-
ELSE t4.state
51-
END AS [Queue_State],
52-
CASE WHEN t4.tasks_waiting IS NULL THEN '--'
53-
ELSE CONVERT(VARCHAR, t4.tasks_waiting)
54-
END AS tasks_waiting,
55-
CASE WHEN t4.last_activated_time IS NULL THEN '--'
56-
ELSE CONVERT(varchar, t4.last_activated_time)
57-
END AS last_activated_time ,
58-
CASE WHEN t4.last_empty_rowset_time IS NULL THEN '--'
59-
ELSE CONVERT(varchar,t4.last_empty_rowset_time)
60-
END AS last_empty_rowset_time,
61-
(
62-
SELECT COUNT(*)
63-
FROM sys.transmission_queue t6
64-
WHERE (t6.from_service_name = t1.name) ) AS [Tran_Message_Count]
65-
FROM sys.services t1 INNER JOIN sys.service_queues t2
66-
ON ( t1.service_queue_id = t2.object_id )
67-
INNER JOIN sys.schemas t3 ON ( t2.schema_id = t3.schema_id )
68-
LEFT OUTER JOIN sys.dm_broker_queue_monitors t4
69-
ON ( t2.object_id = t4.queue_id AND t4.database_id = DB_ID() )
70-
INNER JOIN sys.databases t5 ON ( t5.database_id = DB_ID() );
71-
```
72-
73-
## See Also
74-
[Dynamic Management Views and Functions &#40;Transact-SQL&#41;](~/relational-databases/system-dynamic-management-views/system-dynamic-management-views.md)
75-
[Service Broker Related Dynamic Management Views &#40;Transact-SQL&#41;](../../relational-databases/system-dynamic-management-views/service-broker-related-dynamic-management-views-transact-sql.md)
76-
77-
71+
## Related content
7872

73+
- [System dynamic management views](system-dynamic-management-views.md)
74+
- [Service Broker Related Dynamic Management Views (Transact-SQL)](service-broker-related-dynamic-management-views-transact-sql.md)

0 commit comments

Comments
 (0)