{"id":890,"date":"2005-11-16T08:50:00","date_gmt":"2005-11-16T08:50:00","guid":{"rendered":"\/blogs\/bobb\/post\/For-all-you-XML-phobes-that-lke-DDL-triggers-and-Event-Notifications.aspx"},"modified":"2013-11-14T08:53:59","modified_gmt":"2013-11-14T16:53:59","slug":"for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/","title":{"rendered":"For all you XML-phobes that lke DDL triggers and Event Notifications"},"content":{"rendered":"<p>After speaking yesterday and recanting my story (for about the 100th time) about EventData being XML as a \u201cnefarious plot\u201d to require DBAs to learn XML, I wrote this fairly simple XQuery (actually dead simple XPath) function to change EventData into a rowset. It works based on the fact that you&#8217;ve deposited EventData into a table that looks like this:<\/p>\n<p>CREATE TABLE ddl_log (<br \/>\nid int primary key identity,<br \/>\ndata XML<br \/>\n);<\/p>\n<p>CREATE TRIGGER mytrig<br \/>\nON DATABASE<br \/>\nFOR CREATE_TABLE<br \/>\nAS<br \/>\nINSERT ddl_log VALUES(EventData());<\/p>\n<p>This makes your table into a rowset. You could also use a variation of it in your event notification handler, DDL trigger itself. Just leave out the cross apply. I thought I&#8217;d seen this before, but can never seem to ever have found it. Now that I&#8217;ve done this, &#8230;.learn XML why don&#8217;t &#8216;ya&#8230; There can only be more of it in future. Cheers.<\/p>\n<p>SELECT id,<br \/>\nTab.Col.value(&#8216;.\/EventType[1]&#8217;,&#8217;nvarchar(50)&#8217;) AS &#8216;EventType&#8217;,<br \/>\nTab.Col.value(&#8216;.\/PostTime[1]&#8217;,&#8217;datetime&#8217;) AS\u00a0 &#8216;PostTime&#8217;,<br \/>\nTab.Col.value(&#8216;.\/SPID[1]&#8217;,&#8217;nvarchar(50)&#8217;) AS\u00a0 &#8216;SPID&#8217;,<br \/>\nTab.Col.value(&#8216;.\/ServerName[1]&#8217;,&#8217;nvarchar(50)&#8217;) AS\u00a0 &#8216;ServerName&#8217;,<br \/>\nTab.Col.value(&#8216;.\/LoginName[1]&#8217;,&#8217;nvarchar(50)&#8217;) AS &#8216;LoginName&#8217;,<br \/>\nTab.Col.value(&#8216;.\/UserName[1]&#8217;,&#8217;nvarchar(50)&#8217;) AS &#8216;UserName&#8217;,<br \/>\nTab.Col.value(&#8216;.\/DatabaseName[1]&#8217;,&#8217;nvarchar(128)&#8217;) AS &#8216;DatabaseName&#8217;,<br \/>\nTab.Col.value(&#8216;.\/SchemaName[1]&#8217;,&#8217;nvarchar(128)&#8217;) AS &#8216;SchemaName&#8217;,<br \/>\nTab.Col.value(&#8216;.\/ObjectName[1]&#8217;,&#8217;nvarchar(128)&#8217;) AS &#8216;ObjectName&#8217;,<br \/>\nTab.Col.value(&#8216;.\/ObjectType[1]&#8217;,&#8217;nvarchar(50)&#8217;) AS &#8216;ObjectType&#8217;,<br \/>\nTab.Col.value(&#8216;.\/TSQLCommand[1]\/CommandText[1]&#8217;,&#8217;nvarchar(4000)&#8217;) AS &#8216;CommandText&#8217;,<br \/>\nTab.Col.value(&#8216;.\/TSQLCommand[1]\/SetOptions[1]\/@ANSI_NULLS&#8217;,&#8217;nvarchar(3)&#8217;) AS &#8216;ANSI_NULLS_OPTION&#8217;,<br \/>\nTab.Col.value(&#8216;.\/TSQLCommand[1]\/SetOptions[1]\/@ANSI_NULL_DEFAULT&#8217;,&#8217;nvarchar(3)&#8217;) AS &#8216;ANSI_NULL_DEFAULT_OPTION&#8217;,<br \/>\nTab.Col.value(&#8216;.\/TSQLCommand[1]\/SetOptions[1]\/@ANSI_PADDING&#8217;,&#8217;nvarchar(3)&#8217;) AS &#8216;ANSI_PADDING_OPTION&#8217;,<br \/>\nTab.Col.value(&#8216;.\/TSQLCommand[1]\/SetOptions[1]\/@QUOTED_IDENTIFIER&#8217;,&#8217;nvarchar(3)&#8217;) AS &#8216;QUOTED_IDENTIFIER_OPTION&#8217;,<br \/>\nTab.Col.value(&#8216;.\/TSQLCommand[1]\/SetOptions[1]\/@ENCRYPTED_OPTION&#8217;,&#8217;nvarchar(4)&#8217;) AS &#8216;ENCRYPTED_OPTION&#8217;<br \/>\nFROM ddl_log<br \/>\nCROSS APPLY<br \/>\ndata.nodes(&#8216;\/EVENT_INSTANCE&#8217;) AS Tab(Col)<br \/>\nGO<\/p>\n<p>&#8212; standalone trigger<\/p>\n<p>CREATE TRIGGER mytrig<br \/>\nON DATABASE<br \/>\nFOR CREATE_TABLE<br \/>\nAS<br \/>\nDECLARE @x XML<br \/>\nSET @x = Eventdata()<br \/>\nSELECT<br \/>\nTab.Col.value(&#8216;.\/EventType[1]&#8217;,&#8217;nvarchar(50)&#8217;) AS &#8216;EventType&#8217;,<br \/>\nTab.Col.value(&#8216;.\/PostTime[1]&#8217;,&#8217;datetime&#8217;) AS\u00a0 &#8216;PostTime&#8217;,<br \/>\n&#8212; rest of columns deleted for brevity<br \/>\nFROM @x.nodes(&#8216;\/EVENT_INSTANCE&#8217;) AS Tab(Col)<br \/>\nGO<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After speaking yesterday and recanting my story (for about the 100th time) about EventData being XML as a \u201cnefarious plot\u201d to require DBAs to learn XML, I wrote this fairly simple XQuery (actually dead simple XPath) function to change EventData into a rowset. It works based on the fact that you&#8217;ve deposited EventData into a [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,28,37],"tags":[],"class_list":["post-890","post","type-post","status-publish","format-standard","hentry","category-event-notifications","category-sql-server-2005","category-sql-server-xml"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>For all you XML-phobes that lke DDL triggers and Event Notifications - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"For all you XML-phobes that lke DDL triggers and Event Notifications - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"After speaking yesterday and recanting my story (for about the 100th time) about EventData being XML as a \u201cnefarious plot\u201d to require DBAs to learn XML, I wrote this fairly simple XQuery (actually dead simple XPath) function to change EventData into a rowset. It works based on the fact that you&#8217;ve deposited EventData into a [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2005-11-16T08:50:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-11-14T16:53:59+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/\",\"name\":\"For all you XML-phobes that lke DDL triggers and Event Notifications - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2005-11-16T08:50:00+00:00\",\"dateModified\":\"2013-11-14T16:53:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Event Notifications\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/event-notifications\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"For all you XML-phobes that lke DDL triggers and Event Notifications\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"For all you XML-phobes that lke DDL triggers and Event Notifications - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/","og_locale":"en_US","og_type":"article","og_title":"For all you XML-phobes that lke DDL triggers and Event Notifications - Bob Beauchemin","og_description":"After speaking yesterday and recanting my story (for about the 100th time) about EventData being XML as a \u201cnefarious plot\u201d to require DBAs to learn XML, I wrote this fairly simple XQuery (actually dead simple XPath) function to change EventData into a rowset. It works based on the fact that you&#8217;ve deposited EventData into a [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/","og_site_name":"Bob Beauchemin","article_published_time":"2005-11-16T08:50:00+00:00","article_modified_time":"2013-11-14T16:53:59+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/","name":"For all you XML-phobes that lke DDL triggers and Event Notifications - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2005-11-16T08:50:00+00:00","dateModified":"2013-11-14T16:53:59+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/for-all-you-xml-phobes-that-lke-ddl-triggers-and-event-notifications\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Event Notifications","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/event-notifications\/"},{"@type":"ListItem","position":3,"name":"For all you XML-phobes that lke DDL triggers and Event Notifications"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=890"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/890\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}