<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>triggers &#8211; SQLpowered.com</title>
	<atom:link href="https://sqlpowered.com/tag/triggers/feed/" rel="self" type="application/rss+xml" />
	<link>https://sqlpowered.com</link>
	<description>SQL Server + BI</description>
	<lastBuildDate>Fri, 24 Jul 2020 08:57:22 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://sqlpowered.com/wp-content/uploads/2020/07/FavIcon-e1594067873682-99x100.png</url>
	<title>triggers &#8211; SQLpowered.com</title>
	<link>https://sqlpowered.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Logging schema changes using DDL trigger</title>
		<link>https://sqlpowered.com/logging-schema-changes-using-ddl-trigger/</link>
					<comments>https://sqlpowered.com/logging-schema-changes-using-ddl-trigger/#respond</comments>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Thu, 02 Oct 2014 18:40:32 +0000</pubDate>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[triggers]]></category>
		<guid isPermaLink="false">https://sqlpowered.com/?p=392</guid>

					<description><![CDATA[Logging schema changes (DDL) in the database is a common requirement in many organizations or development teams. We can choose from many tools offering complex auditing or write our solution. Let&#8217;s build our one and keep it as simple as possible. Please remember that the event log can be manipulated...]]></description>
										<content:encoded><![CDATA[<p>Logging schema changes (DDL) in the database is a common requirement in many organizations or development teams. We can choose from many tools offering complex auditing or write our solution. Let&#8217;s build our one and keep it as simple as possible. Please remember that the event log can be manipulated by everyone with access to the database and this solution isn&#8217;t suitable for security compliant auditing.</p>
<p>Our solution is based on a DDL trigger. You can read more about it in <a href="https://docs.microsoft.com/en-us/sql/relational-databases/triggers/ddl-triggers?view=sql-server-ver15">BOL</a>.</p>
<p>Create table [dbo].[DDLEventLog] where logged events will be stored. Create trigger [trg_DatabaseDDLeventer] which will fire on all database level DDL events ( DDL_DATABASE_LEVEL_EVENTS) and store event detail in XML format to [dbo].[DDLEventLog] table.</p>
<pre class="lang:tsql decode:true">-- Create events table
CREATE TABLE [dbo].[DDLEventLog]
(
    [Id] INT IDENTITY(1,1) PRIMARY KEY,
    [EventDate] DATETIME,
    [Originator] NVARCHAR(256),
    [EventXML] XML
)
GO

-- Create trigger
SET ANSI_NULLS, QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [trg_DatabaseDDLeventer] ON DATABASE
FOR DDL_DATABASE_LEVEL_EVENTS AS
BEGIN
    
	INSERT INTO [dbo].[DDLEventLog] 
        ([EventDate], [Originator], [EventXML])
    VALUES
        (GETDATE(), USER_NAME(), EVENTDATA())

END
GO</pre>
<p>We will run the DDL statement CREATE TABLE to test how it works.</p>
<pre class="lang:tsql decode:true">-- Create sample DDL event to test trigger
CREATE TABLE [dbo].[TestTable] ([Id] int)
GO

-- Get results
SELECT * FROM [DDLEventLog]
GO</pre>
<p><img decoding="async" class="alignnone wp-image-4507" src="https://sqlpowered.com/wp-content/uploads/2014/10/Logging-schema-changes-with-DDL-trigger.png" alt="" width="638" height="34" srcset="https://sqlpowered.com/wp-content/uploads/2014/10/Logging-schema-changes-with-DDL-trigger.png 1257w, https://sqlpowered.com/wp-content/uploads/2014/10/Logging-schema-changes-with-DDL-trigger-300x16.png 300w, https://sqlpowered.com/wp-content/uploads/2014/10/Logging-schema-changes-with-DDL-trigger-1024x55.png 1024w, https://sqlpowered.com/wp-content/uploads/2014/10/Logging-schema-changes-with-DDL-trigger-150x8.png 150w, https://sqlpowered.com/wp-content/uploads/2014/10/Logging-schema-changes-with-DDL-trigger-768x41.png 768w, https://sqlpowered.com/wp-content/uploads/2014/10/Logging-schema-changes-with-DDL-trigger-360x19.png 360w, https://sqlpowered.com/wp-content/uploads/2014/10/Logging-schema-changes-with-DDL-trigger-160x9.png 160w, https://sqlpowered.com/wp-content/uploads/2014/10/Logging-schema-changes-with-DDL-trigger-320x17.png 320w, https://sqlpowered.com/wp-content/uploads/2014/10/Logging-schema-changes-with-DDL-trigger-520x28.png 520w, https://sqlpowered.com/wp-content/uploads/2014/10/Logging-schema-changes-with-DDL-trigger-720x38.png 720w, https://sqlpowered.com/wp-content/uploads/2014/10/Logging-schema-changes-with-DDL-trigger-980x52.png 980w" sizes="(max-width: 638px) 100vw, 638px" /></p>
<p>If we will click on the link in EventXML column we can observe message details and identify who executed which DDL statement:</p>
<pre class="lang:xhtml decode:true">&lt;EVENT_INSTANCE&gt;
  &lt;EventType&gt;CREATE_TABLE&lt;/EventType&gt;
  &lt;PostTime&gt;2010-07-11T10:55:04.710&lt;/PostTime&gt;
  &lt;SPID&gt;51&lt;/SPID&gt;
  &lt;ServerName&gt;TESTSERVER&lt;/ServerName&gt;
  &lt;LoginName&gt;TESTSERVER\Admin&lt;/LoginName&gt;
  &lt;UserName&gt;dbo&lt;/UserName&gt;
  &lt;DatabaseName&gt;master&lt;/DatabaseName&gt;
  &lt;SchemaName&gt;dbo&lt;/SchemaName&gt;
  &lt;ObjectName&gt;TestTable&lt;/ObjectName&gt;
  &lt;ObjectType&gt;TABLE&lt;/ObjectType&gt;
  &lt;TSQLCommand&gt;
    &lt;SetOptions ANSI_NULLS="ON" ANSI_NULL_DEFAULT="ON" ANSI_PADDING="ON" QUOTED_IDENTIFIER="ON" ENCRYPTED="FALSE" /&gt;
    &lt;CommandText&gt;CREATE TABLE dbo.TestTable (Id int)&lt;/CommandText&gt;
  &lt;/TSQLCommand&gt;
&lt;/EVENT_INSTANCE&gt;</pre>
<p>If you would like to implement a more robust and security compliant solution then please review <a href="https://docs.microsoft.com/en-us/sql/relational-databases/security/auditing/sql-server-audit-database-engine?view=sql-server-ver15">SQL Server Audit</a> functionality for more details.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlpowered.com/logging-schema-changes-using-ddl-trigger/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
