You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/relational-databases/wmi-provider-server-events/sample-creating-a-sql-server-agent-alert-with-the-wmi-provider.md
@@ -3,7 +3,8 @@ title: "Create a SQL Server Agent Alert with the WMI Provider"
3
3
description: Create a SQL Server Agent alert that responds to specific events. This simple alert saves XML deadlock graph events in a table for later analysis.
4
4
author: markingmyname
5
5
ms.author: maghan
6
-
ms.date: "03/14/2017"
6
+
ms.reviewer: randolphwest
7
+
ms.date: 10/30/2023
7
8
ms.service: sql
8
9
ms.subservice: wmi
9
10
ms.topic: "reference"
@@ -12,121 +13,132 @@ helpviewer_keywords:
12
13
- "WMI Provider for Server Events, samples"
13
14
- "sample applications [WMI]"
14
15
---
15
-
# Sample: Creating a SQL Server Agent Alert with the WMI Provider
16
+
# Sample: Create a SQL Server Agent Alert with the WMI Provider
One common way to use the WMI Event Provider is to create SQL Server Agent alerts that respond to specific events. The following sample presents a simple alert that saves XML deadlock graph events in a table for later analysis. SQL Server Agent submits a WQL request, receives WMI events, and runs a job in response to the event. Notice that, although several Service Broker objects are involved in processing the notification message, the WMI Event Provider handles the details of creating and managing these objects.
18
-
19
-
## Example
20
-
First, a table is created in the `AdventureWorks` database to hold the deadlock graph event. The table contains two columns: The `AlertTime` column holds the time that the alert runs, and the `DeadlockGraph` column holds the XML document that contains the deadlock graph.
21
-
22
-
Then, the alert is created. The script first creates the job that the alert will run, adds a job step to the job, and targets the job to the current instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. The script then creates the alert.
23
-
24
-
The job step retrieves the **TextData** property of the WMI event instance and inserts that value into the **DeadlockGraph** column of the **DeadlockEvents** table. Notice that [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] implicitly converts the string into XML format. Because the job step uses the [!INCLUDE[tsql](../../includes/tsql-md.md)] subsystem, the job step does not specify a proxy.
25
-
26
-
The alert runs the job whenever a deadlock graph trace event would be logged. For a WMI alert, SQL Server Agent creates a notification query using the namespace and WQL statement specified. For this alert, SQL Server Agent monitors the default instance on the local computer. The WQL statement requests any `DEADLOCK_GRAPH` event in the default instance. To change the instance that the alert monitors, substitute the instance name for `MSSQLSERVER` in the `@wmi_namespace` for the alert.
27
-
19
+
20
+
One common way to use the WMI Event Provider is to create SQL Server Agent alerts that respond to specific events. The following sample presents a simple alert that saves XML deadlock graph events in a table for later analysis. SQL Server Agent submits a WQL request, receives WMI events, and runs a job in response to the event. Notice that, although several Service Broker objects are involved in processing the notification message, the WMI Event Provider handles the details of creating and managing these objects.
21
+
22
+
## Example
23
+
24
+
First, a table is created in the `AdventureWorks2022` database to hold the deadlock graph event. The table contains two columns: The `AlertTime` column holds the time that the alert runs, and the `DeadlockGraph` column holds the XML document that contains the deadlock graph.
25
+
26
+
Then, the alert is created. The script first creates the job that the alert will run, adds a job step to the job, and targets the job to the current instance of [!INCLUDE [ssNoVersion](../../includes/ssnoversion-md.md)]. The script then creates the alert.
27
+
28
+
The job step retrieves the `TextData` property of the WMI event instance and inserts that value into the `DeadlockGraph` column of the `DeadlockEvents` table. [!INCLUDE [ssNoVersion](../../includes/ssnoversion-md.md)] implicitly converts the string into XML format. Because the job step uses the [!INCLUDE [tsql](../../includes/tsql-md.md)] subsystem, the job step doesn't specify a proxy.
29
+
30
+
The alert runs the job whenever a deadlock graph trace event would be logged. For a WMI alert, SQL Server Agent creates a notification query using the namespace and WQL statement specified. For this alert, SQL Server Agent monitors the default instance on the local computer. The WQL statement requests any `DEADLOCK_GRAPH` event in the default instance. To change the instance that the alert monitors, substitute the instance name for `MSSQLSERVER` in the `@wmi_namespace` for the alert.
31
+
28
32
> [!NOTE]
29
-
> For SQL Server Agent to receive WMI events, [!INCLUDE[ssSB](../../includes/sssb-md.md)] must be enabled in **msdb** and [!INCLUDE[ssSampleDBobject](../../includes/sssampledbobject-md.md)].
To see the job run, provoke a deadlock. In [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)], open two **SQL Query** tabs and connect both queries to the same instance. Run the following script in one of the query tabs. This script produces one result set and finishes.
85
-
86
-
```
87
-
USE AdventureWorks ;
88
-
GO
89
-
90
-
BEGIN TRANSACTION ;
91
-
GO
92
-
93
-
SELECT TOP(1) Name FROM Production.Product WITH (XLOCK) ;
94
-
GO
95
-
```
96
-
97
-
Run the following script in the second query tab. This script produces one result set and then blocks, waiting to acquire a lock on `Production.Product`.
98
-
99
-
```
100
-
USE AdventureWorks ;
101
-
GO
102
-
103
-
BEGIN TRANSACTION ;
104
-
GO
105
-
106
-
SELECT TOP(1) Name FROM Production.Location WITH (XLOCK) ;
107
-
GO
108
-
109
-
SELECT TOP(1) Name FROM Production.Product WITH (XLOCK) ;
110
-
GO
111
-
```
112
-
113
-
Run the following script in the first query tab. This script blocks, waiting to acquire a lock on `Production.Location`. After a short time-out, [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] will choose either this script or the script in the sample as the deadlock victim and end the transaction.
114
-
115
-
```
116
-
SELECT TOP(1) Name FROM Production.Location WITH (XLOCK) ;
117
-
GO
118
-
```
119
-
120
-
After provoking the deadlock, wait several moments for SQL Server Agent to activate the alert and run the job. Examine the contents of the `DeadlockEvents` table by running the following script:
121
-
122
-
```
123
-
SELECT * FROM DeadlockEvents ;
124
-
GO
125
-
```
126
-
127
-
The `DeadlockGraph` column should contain an XML document that shows all the properties of the deadlock graph event.
128
-
129
-
## See Also
130
-
[WMI Provider for Server Events Concepts](../../relational-databases/wmi-provider-server-events/wmi-provider-for-server-events-concepts.md)
131
-
132
-
33
+
> For SQL Server Agent to receive WMI events, [!INCLUDE [ssSB](../../includes/sssb-md.md)] must be enabled in `msdb` and [!INCLUDE [ssSampleDBobject](../../includes/sssampledbobject-md.md)].
Add an alert that responds to all `DEADLOCK_GRAPH` events for the default instance. To monitor deadlocks for a different instance, change `MSSQLSERVER` to the name of the instance.
83
+
84
+
```sql
85
+
EXEC msdb.dbo.sp_add_alert @name = N'Respond to DEADLOCK_GRAPH',
To see the job run, provoke a deadlock. In [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)], open two **SQL Query** tabs and connect both queries to the same instance. Run the following script in one of the query tabs. This script produces one result set and finishes.
95
+
96
+
```sql
97
+
USE AdventureWorks2022;
98
+
GO
99
+
100
+
BEGIN TRANSACTION;
101
+
GO
102
+
103
+
SELECT TOP (1) Name
104
+
FROMProduction.Product WITH (XLOCK);
105
+
GO
106
+
```
107
+
108
+
Run the following script in the second query tab. This script produces one result set and then blocks, waiting to acquire a lock on `Production.Product`.
109
+
110
+
```sql
111
+
USE AdventureWorks2022;
112
+
GO
113
+
114
+
BEGIN TRANSACTION;
115
+
GO
116
+
117
+
SELECT TOP (1) Name
118
+
FROMProduction.Location WITH (XLOCK);
119
+
GO
120
+
121
+
SELECT TOP (1) Name
122
+
FROMProduction.Product WITH (XLOCK);
123
+
GO
124
+
```
125
+
126
+
Run the following script in the first query tab. This script blocks, waiting to acquire a lock on `Production.Location`. After a short time-out, [!INCLUDE [ssNoVersion](../../includes/ssnoversion-md.md)] will choose either this script or the script in the sample as the deadlock victim and end the transaction.
127
+
128
+
```sql
129
+
SELECT TOP(1) Name FROMProduction.Location WITH (XLOCK);
130
+
GO
131
+
```
132
+
133
+
After provoking the deadlock, wait several moments for SQL Server Agent to activate the alert and run the job. Examine the contents of the `DeadlockEvents` table by running the following script:
134
+
135
+
```sql
136
+
SELECT*FROM DeadlockEvents;
137
+
GO
138
+
```
139
+
140
+
The `DeadlockGraph` column should contain an XML document that shows all the properties of the deadlock graph event.
141
+
142
+
## Related content
143
+
144
+
-[WMI Provider for Server Events concepts](wmi-provider-for-server-events-concepts.md)
Copy file name to clipboardExpand all lines: docs/relational-databases/wmi-provider-server-events/sample-using-the-wmi-event-provider-with-the-net-framework.md
+62-59Lines changed: 62 additions & 59 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,8 @@ title: "Sample: Use the WMI Event Provider in .NET"
3
3
description: "A sample C# application uses the WMI Event Provider to return event data for all data definition language events that occur on an instance of SQL Server."
4
4
author: markingmyname
5
5
ms.author: maghan
6
-
ms.date: "03/14/2017"
6
+
ms.reviewer: randolphwest
7
+
ms.date: 10/30/2023
7
8
ms.service: sql
8
9
ms.subservice: wmi
9
10
ms.topic: "reference"
@@ -12,62 +13,64 @@ helpviewer_keywords:
12
13
- "sample applications [WMI]"
13
14
- "managed code [WMI]"
14
15
---
15
-
# Sample: Using the WMI Event Provider with the .NET Framework
16
+
# Sample: Use the WMI Event Provider with the .NET Framework
The following sample creates an application in C# that uses the WMI Event Provider to return event data for all data definition language (DDL) events that occur on a default installation instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].
18
-
19
-
## Example
20
-
The example compiles by using the following command file:
21
-
22
-
```
23
-
set compiler_path=C:\WINNT\Microsoft.NET\Framework\v2.0.50110
24
-
25
-
%compiler_path%\csc %1
26
-
```
27
-
28
-
```
29
-
using System;
30
-
using System.Management;
31
-
32
-
class SQLWEPExample
33
-
{
34
-
public static void Main(string[] args)
35
-
{
36
-
string query = @"SELECT * FROM DDL_EVENTS " ;
37
-
// Default namespace for default instance of SQL Server
[WMI Provider for Server Events Concepts](../../relational-databases/wmi-provider-server-events/wmi-provider-for-server-events-concepts.md)
72
-
73
-
19
+
20
+
The following sample creates an application in C# that uses the WMI Event Provider to return event data for all data definition language (DDL) events that occur on a default installation instance of [!INCLUDE [ssNoVersion](../../includes/ssnoversion-md.md)].
21
+
22
+
## Examples
23
+
24
+
The example compiles by using the following command file:
25
+
26
+
```cmd
27
+
set compiler_path=C:\WINNT\Microsoft.NET\Framework\v2.0.50110
28
+
29
+
%compiler_path%\csc %1
30
+
```
31
+
32
+
```csharp
33
+
usingSystem;
34
+
usingSystem.Management;
35
+
36
+
classSQLWEPExample
37
+
{
38
+
publicstaticvoidMain(string[] args)
39
+
{
40
+
stringquery=@"SELECT * FROM DDL_EVENTS " ;
41
+
// Default namespace for default instance of SQL Server
0 commit comments