Hi All.
I have a SQL 2008 R2 isntance, and I am trying to have a system which alerts after a deadlock. The deadlock traceflags are already in place, but this writes to the SQL error log, the purpose of my plan is to alert right after a deadlock so I have decided the best way to do this is using Xevents.
At the moment I have the query below to extract the deadlock information. I would like to shred the XML and retrieve details and save onto a table such as table name, victim, spid, sql statements etc. Any help would be appreciated.
SELECT xed.value('@timestamp', 'datetime') as Creation_Date, xed.query('.') AS Extend_Event FROM ( SELECT CAST([target_data] AS XML) AS Target_Data FROM sys.dm_xe_session_targets AS xt INNER JOIN sys.dm_xe_sessions AS xs ON xs.address = xt.event_session_address WHERE xs.name = N'system_health' AND xt.target_name = N'ring_buffer' ) AS XML_Data
The XML looks like this
<event name="xml_deadlock_report" package="sqlserver" id="123" version="1" timestamp="2015-02-18T16:24:30.726Z"> <data name="xml_report"> <type name="unicode_string" package="package0" /> <value><deadlock> <victim-list> <victimProcess id="process5c1ddc8"/> </victim-list> <process-list> <process id="process5c1ddc8" taskpriority="0" logused="220" waitresource="KEY: 2:7926335401066364928 (c20918c6e169)" waittime="3800" ownerId="19070357" transactionname="user_transaction" lasttranstarted="2015-02-18T16:24:36.943" XDES="0x1469d9530" lockMode="X" schedulerid="9" kpid="5536" status="suspended" spid="61" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2015-02-18T16:24:50.497" lastbatchcompleted="2015-02-18T16:24:36.947" clientapp="Microsoft SQL Server Management Studio - Query" hostname="GBVA548376" hostpid="1208" loginname="INTL\A548376" isolationlevel="read committed (2)" xactid="19070357" currentdb="2" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200"> <executionStack> <frame procname="" line="1" stmtstart="30" sqlhandle="0x020000008407891bbd2cb7a0d4923843c79f4ce2254daada"> </frame> <frame procname="" line="1" sqlhandle="0x02000000dcc60f0386a6487bf053a44870ed1152ebeb6613"> </frame> </executionStack> <inputbuf> INSERT dbo.Tbl2 (id, col) VALUES (111, 555) </inputbuf> </process> <process id="process5c31048" taskpriority="0" logused="3548" waitresource="KEY: 2:7854277806987476992 (61a06abd401c)" waittime="11378" ownerId="19061287" transactionname="user_transaction" lasttranstarted="2015-02-18T16:23:24.980" XDES="0x8695463b0" lockMode="S" schedulerid="11" kpid="14736" status="suspended" spid="57" sbid="0" ecid="0" priority="0" trancount="3" lastbatchstarted="2015-02-18T16:24:42.903" lastbatchcompleted="2015-02-18T16:24:20.723" lastattention="2015-02-18T16:20:51.050" clientapp="Microsoft SQL Server Management Studio - Query" hostname="GBVA548376" hostpid="1208" loginname="INTL\A548376" isolationlevel="read committed (2)" xactid="19061287" currentdb="2" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200"> <executionStack> <frame procname="" line="2" stmtstart="30" sqlhandle="0x020000008407891bbd2cb7a0d4923843c79f4ce2254daada"> </frame> <frame procname="" line="2" stmtstart="24" sqlhandle="0x020000008d2ab9030650ebc0eab587b94eebe42bfed9ea40"> </frame> </executionStack> <inputbuf> BEGIN TRAN INSERT dbo.Tbl2 (id, col) VALUES (111, 2) </inputbuf> </process> </process-list> <resource-list> <keylock hobtid="7926335401066364928" dbid="2" objectname="" indexname="" id="lock67f7d80" mode="X" associatedObjectId="7926335401066364928"> <owner-list> <owner id="process5c31048" mode="X"/> </owner-list> <waiter-list> <waiter id="process5c1ddc8" mode="X" requestType="wait"/> </waiter-list> </keylock> <keylock hobtid="7854277806987476992" dbid="2" objectname="" indexname="" id="lock69b1100" mode="X" associatedObjectId="7854277806987476992"> <owner-list> <owner id="process5c1ddc8" mode="X"/> </owner-list> <waiter-list> <waiter id="process5c31048" mode="S" requestType="wait"/> </waiter-list> </keylock> </resource-list> </deadlock> </value> <text /> </data> </event>