Back with server side traces, i would build a view for each trace that would extract the details out for me, and made it extremely queryable I want to do the same with Extended Events, but because each item is saved as XML, it makes it harder.it is important to note, i [b]do not want[/b] to hardcode xml...i want to discover the events, and dynamically build the right script for any events defined in a EE's actions.here's a solid setup: i'm capturing any errors greater than Level 14.[code]CREATE EVENT SESSION [ApplicationErrors] ON SERVER ADD EVENT sqlserver.error_reported( ACTION(package0.event_sequence, package0.last_error, sqlserver.client_app_name, sqlserver.client_hostname, sqlserver.database_id, sqlserver.database_name, sqlserver.nt_username, sqlserver.plan_handle, sqlserver.query_hash, sqlserver.query_plan_hash, sqlserver.session_nt_username, sqlserver.sql_text, sqlserver.username) WHERE ([package0].[greater_than_equal_int64]([severity],(14)) --AND [sqlserver].[not_equal_i_sql_unicode_string]([sqlserver].[client_hostname],N'SolarWinds') ) ) ADD TARGET package0.event_file(SET filename=N'ApplicationErrors.xel')WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=OFF)GOALTER EVENT SESSION [ApplicationErrors] ON SERVER STATE = START--ALTER EVENT SESSION [ApplicationErrors] ON SERVER STATE = STOP[/code]so after a while, i'll get a few errors in the extended event, which i can see in raw form like this:[code]--detailsSELECT * FROM sys.fn_xe_file_target_read_file(N'ApplicationErrors*.xel',null, null, null) fn--details as xmlSELECT CAST(event_data AS XML) AS event_dataFROM sys.fn_xe_file_target_read_file(N'ApplicationErrors*.xel',null, null, null) fn[/code]An article from Jonathan Kehayias has an example, that I trivially adapted to match my specific Extended Event, and for the columns he defined, i get values:[code]--copied and adapted from http://sqlblog.com/blogs/jonathan_kehayias/archive/2010/12/06/an-xevent-a-day-6-of-31-targets-week-asynchronous-file-target.aspxSELECT n.value('(@name)[1]', 'varchar(50)') AS event_name, n.value('(@package)[1]', 'varchar(50)') AS package_name, n.value('(@id)[1]', 'int') AS id, n.value('(@version)[1]', 'int') AS version, DATEADD(hh, DATEDIFF(hh, GETUTCDATE(), CURRENT_TIMESTAMP), n.value('(@timestamp)[1]', 'datetime2')) AS [timestamp], n.value('(data[@name="error"]/value)[1]', 'int') as error, n.value('(data[@name="severity"]/value)[1]', 'int') as severity, n.value('(data[@name="duration"]/value)[1]', 'int') as state, n.value('(data[@name="user_defined"]/value)[1]', 'varchar(5)') as user_defined, n.value('(data[@name="message"]/value)[1]', 'varchar(max)') as messageFROM (SELECT CAST(event_data AS XML) AS event_dataFROM sys.fn_xe_file_target_read_file(N'ApplicationErrors*.xel',null, null, null) ) as tabCROSS APPLY event_data.nodes('event') as q(n)[/code]so Jonathans Script gets values that exist in my EE, but I have additional ones defined, and I want to add them.For the additional the columns i havedefined, i can see in the EE are clearly the ones i defined above; so where in the metadata is that collection of columns, so i can dynamically build a string to pull the columns like host_name, sql_handle, etc?[img]http://www.sqlservercentral.com/Forums/Attachment19233.aspx[/img]
↧