
create table syshealth (thexml xml)
go

insert syshealth
select cast(xest.target_data as xml)
from sys.dm_xe_session_targets xest
join sys.dm_xe_sessions xes on xes.address = xest.event_session_address
where xest.target_name = 'ring_buffer' and xes.name = 'system_health'
go

SELECT
	Data2.Results.value ('(data/.)[1]', 'int') AS ErrorNumber
	-- ,Cast(Data3.Results.value ('.', 'nvarchar(MAX)') as XML) AS Stacks
from
(
select cast(xest.target_data as xml) as data
from sys.dm_xe_session_targets xest
join sys.dm_xe_sessions xes on xes.address = xest.event_session_address
where xest.target_name = 'ring_buffer' and xes.name = 'system_health'
) events
cross apply Data.nodes ('//RingBufferTarget/event') 
  AS Data2 (Results)
cross apply Data2.Results.nodes('(action[@name="tsql_stack"]/value)[1]')
  AS Data3 (Results)