{"id":604,"date":"2007-07-24T13:31:00","date_gmt":"2007-07-24T13:31:00","guid":{"rendered":"\/blogs\/kimberly\/post\/EXECUTE-AS-and-an-important-update-your-DDL-Triggers-(for-auditing-or-prevention).aspx"},"modified":"2007-07-24T13:31:00","modified_gmt":"2007-07-24T13:31:00","slug":"execute-as-and-an-important-update-your-ddl-triggers-for-auditing-or-prevention","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/execute-as-and-an-important-update-your-ddl-triggers-for-auditing-or-prevention\/","title":{"rendered":"&#8220;EXECUTE AS&#8221; and an important update your DDL Triggers (for auditing or prevention)"},"content":{"rendered":"<p><span style=\"font-family: Verdana; color: #003300; font-size: 10pt\">DDL Triggers were a new feature of SQL Server 2005 and while seemingly simple, they are very powerful. DDL Triggers allow you to trap an attempted DDL operation to audit it, prevent it, or do anything you want to validate\/verify\/&rdquo;authorize&rdquo;\/etc &ndash; you write the code. And, since a trigger fires as part of the transaction, you can roll it back. <\/span><span style=\"font-family: Verdana; color: #003300; font-size: 10pt\">In many conference demos\/webcasts, etc., I have provided a sample script that prevents ddl within a [production] database. That script has been&nbsp;really helpful\/useful but recently I thought about an update to it&hellip; <\/span><span style=\"font-family: Verdana; color: #003300; font-size: 10pt\">SQL Server 2005 has another new feature &quot;execute as&quot;. While I definitely see many benefits, I&rsquo;m also a bit concerned. To a certain extent, I feel that the potential for SQL Injection is actually higher. If a developer creates a poorly written\/tested stored procedure (ok, therein lies the problem, really!) that includes dynamic string execution AND then uses &quot;execute as&quot; to essentially elevate a user with minimal privileges to a higher level (so that they don&rsquo;t need to give the base object rights to the user), a malicious user could &ldquo;inject&rdquo; code in and actually succeed if the &ldquo;execute as&rdquo; user has rights to the injected code. In prior releases, and with the default behavior (execute as caller), this is not possible (which is good for security but bad for dynamically executed strings within stored procedures as base object rights are necessary). <\/span><\/p>\n<p>\n<span style=\"font-family: Verdana; color: #003300; font-size: 10pt\">Having said that, and since security is always a concern, my DDL Trigger only audited for the login of the user who executed the statement, not for the actual user that&rsquo;s logged in. In other words, if EXECUTE AS is used (or SETUSER is used), then the context of the user executing is actually different then the logged in user. To see this shift in context, SQL Server 2005 added a new function: ORIGINAL_LOGIN(). <\/span>\n<\/p>\n<p>\n<span style=\"font-family: Verdana; color: #003300; font-size: 10pt\">(<em>reading between the lines is even more frightening in that prior to SQL Server 2005, the original user could not be tracked from SETUSER. The good news is that SETUSER is ONLY allowed to be used by DBOs so it&rsquo;s not as widespread as the potential for &ldquo;execute as&rdquo;<\/em>).<\/span><span style=\"font-family: Verdana; color: #003300; font-size: 10pt\">OK, so how can we put all of this together? We&rsquo;ll want to add the ORIGINAL_LOGIN function into our audit table in our DDL Trigger. Even if you choose NOT to rollback, at least you&rsquo;ll know who performed the operation (even if from a dynamically executed string!).<\/span>\n<\/p>\n<p>\n<span style=\"font-family: Verdana; color: #003300; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">USE<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\" color=\"#000000\"> AdventureWorks<span style=\"color: gray\"><font face=\"courier new,courier\">;<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; color: green; font-size: 10pt\">&#8211;Create a login\/user &#8211; just for this exercise<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; color: green; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">CREATE<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: blue\">LOGIN<\/span><font color=\"#000000\"> Paul <span style=\"color: blue\">WITH<\/span><font color=\"#000000\"> PASSWORD <span style=\"color: gray\">=<\/span><font color=\"#000000\"> <span style=\"color: red\">&#39;PxKoJ29!07&#39;<\/span><span style=\"color: gray\"><font face=\"courier new,courier\">;<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">CREATE<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: fuchsia\">USER<\/span><font color=\"#000000\"> Paul <span style=\"color: blue\">FOR<\/span><font color=\"#000000\"> <span style=\"color: blue\">LOGIN<\/span><font color=\"#000000\"> Paul<span style=\"color: gray\"><font face=\"courier new,courier\">;<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: maroon; font-size: 10pt\"><font face=\"courier new,courier\">sp_addrolemember<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: red\">&#39;db_ddladmin&#39;<\/span><span style=\"color: gray\">,<\/span><font color=\"#000000\"> <span style=\"color: red\"><font face=\"courier new,courier\">&#39;Paul&#39;<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">&nbsp;<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">CREATE<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: blue\">SCHEMA<\/span><font face=\"courier new,courier\" color=\"#000000\"> SecurityAdministration<br \/>\n<\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">CREATE<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: blue\">TABLE<\/span><font color=\"#000000\"> SecurityAdministration<span style=\"color: gray\">.<\/span><font face=\"courier new,courier\" color=\"#000000\">AuditDDLOperations<br \/>\n<\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: gray; font-size: 10pt\"><font face=\"courier new,courier\">(<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>OpID<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: blue\">int<\/span><font color=\"#000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: gray\">NOT<\/span><font color=\"#000000\"> <span style=\"color: gray\">NULL<\/span><font color=\"#000000\"> <font face=\"courier new,courier\"><span style=\"color: blue\">identity<br \/>\n<\/span><font color=\"#000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><span><font color=\"#000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">CONSTRAINT<\/span><font face=\"courier new,courier\" color=\"#000000\"> AuditDDLOperationsPK<br \/>\n<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">PRIMARY<\/span><font color=\"#000000\"> <span style=\"color: blue\">KEY<\/span><font color=\"#000000\"> <span style=\"color: blue\">CLUSTERED<\/span><span style=\"color: gray\"><font face=\"courier new,courier\">,<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>OriginalLoginName<span>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: blue\">sysname<\/span><span><font color=\"#000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: gray\">NOT<\/span><font color=\"#000000\"> <span style=\"color: gray\"><font face=\"courier new,courier\">NULL,<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>LoginName<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: blue\">sysname<\/span><span><font color=\"#000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: gray\">NOT<\/span><font color=\"#000000\"> <span style=\"color: gray\"><font face=\"courier new,courier\">NULL,<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>UserName<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: blue\">sysname<\/span><span><font color=\"#000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: gray\">NOT<\/span><font color=\"#000000\"> <span style=\"color: gray\"><font face=\"courier new,courier\">NULL,<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>PostTime<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: blue\">datetime<\/span><span><font color=\"#000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: gray\">N<\/span><\/span><span style=\"color: gray\">OT<\/span><font color=\"#000000\"> <span style=\"color: gray\"><font face=\"courier new,courier\">NULL,<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>EventType<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: blue\">nvarchar<\/span><span style=\"color: gray\">(<\/span><font color=\"#000000\">100<span style=\"color: gray\">)<\/span><font color=\"#000000\"><span>&nbsp;<\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: gray\">NOT<\/span><font color=\"#000000\"> <span style=\"color: gray\"><font face=\"courier new,courier\">NULL,<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>DDLOp<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: blue\">nvarchar<\/span><span style=\"color: gray\">(<\/span><font color=\"#000000\">2000<span style=\"color: gray\">)<\/span><span><font color=\"#000000\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: gray\">NOT<\/span><font color=\"#000000\"> <span style=\"color: gray\">NULL<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; color: gray; font-size: 10pt\">);<br \/>\n<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">GRANT<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: blue\">INSERT<\/span><font color=\"#000000\"> <span style=\"color: blue\">ON<\/span><font color=\"#000000\"> SecurityAdministration<span style=\"color: gray\">.<\/span><font color=\"#000000\">AuditDDLOperations <span style=\"color: blue\">TO<\/span><font color=\"#000000\"> <span style=\"color: blue\">public<\/span><span style=\"color: gray\"><font face=\"courier new,courier\">;<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">CREATE<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: blue\">TRIGGER<\/span><font face=\"courier new,courier\" color=\"#000000\"> PreventAllDDL<br \/>\n<\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">ON<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\" color=\"#000000\"> <span style=\"color: blue\"><font face=\"courier new,courier\">DATABASE<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">WITH<\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\"> ENCRYPTION<br \/>\n<\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\">FOR<\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\"> DDL_DATABASE_LEVEL_EVENTS<br \/>\n<\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\">AS<\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\"> <br \/>\n<\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\">DECLARE<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\" color=\"#000000\"> @data <span style=\"color: blue\"><font face=\"courier new,courier\">XML<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">SET<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> @data <span style=\"color: gray\">=<\/span><font color=\"#000000\"> EVENTDATA<span style=\"color: gray\"><font face=\"courier new,courier\">()<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\">RAISERROR<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\"> <span style=\"color: gray\">(<\/span><span style=\"color: red\">&#39;DDL Operations are prohibited on this production database. Please contact ITOperations for proper policies and change control procedures.&#39;<\/span><span style=\"color: gray\">,<\/span><font color=\"#000000\"> 16<span style=\"color: gray\">,<\/span><font color=\"#000000\"> <span style=\"color: gray\">&#8211;<\/span><font color=\"#000000\">1<span style=\"color: gray\">)<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\">ROLLBACK<br \/>\n<\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\">INSERT<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> SecurityAdministration<span style=\"color: gray\">.<\/span><font face=\"courier new,courier\" color=\"#000000\">AuditDDLOperations<br \/>\n<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><span><font color=\"#000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: gray\">(<\/span><font color=\"#000000\">OriginalLoginName<span style=\"color: gray\"><font face=\"courier new,courier\">,<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\"><font face=\"courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;<\/span>LoginName<font face=\"courier new,courier\"><span style=\"color: gray\">,<br \/>\n<\/span><font color=\"#000000\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\"><font face=\"courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;<\/span>UserName<span style=\"color: gray\"><font face=\"courier new,courier\">,<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\"><font face=\"courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;<\/span>PostTime<span style=\"color: gray\"><font face=\"courier new,courier\">,<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\"><font face=\"courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;<\/span>EventType<span style=\"color: gray\"><font face=\"courier new,courier\">,<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\"><font face=\"courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;<\/span>DDLOp<span style=\"color: gray\"><font face=\"courier new,courier\">)<br \/>\n<\/span><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">VALUES<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><span><font color=\"#000000\">&nbsp;&nbsp; <\/span><span style=\"color: gray\">(<\/span><font color=\"#000000\">ORIGINAL_LOGIN<span style=\"color: gray\">(),<\/span><font color=\"#000000\"> <span style=\"color: fuchsia\">SYSTEM_USER<\/span><span style=\"color: gray\">,<\/span><font color=\"#000000\"> <span style=\"color: fuchsia\">CURRENT_USER<\/span><span style=\"color: gray\">,<\/span><font color=\"#000000\"> <span style=\"color: fuchsia\">GETDATE<\/span><font face=\"courier new,courier\"><span style=\"color: gray\">(),<br \/>\n<\/span><font color=\"#000000\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"><span>&nbsp;&nbsp; <\/span>@data<span style=\"color: gray\">.<\/span><span style=\"color: blue\">value<\/span><span style=\"color: gray\">(<\/span><span style=\"color: red\">&#39;(\/EVENT_INSTANCE\/EventType)[1]&#39;<\/span><span style=\"color: gray\">,<\/span><font color=\"#000000\"> <span style=\"color: red\">&#39;nvarchar(100)&#39;<\/span><font face=\"courier new,courier\"><span style=\"color: gray\">),<br \/>\n<\/span><font color=\"#000000\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"><span>&nbsp;&nbsp; <\/span>@data<span style=\"color: gray\">.<\/span><span style=\"color: blue\">value<\/span><span style=\"color: gray\">(<\/span><span style=\"color: red\">&#39;(\/EVENT_INSTANCE\/TSQLCommand)[1]&#39;<\/span><span style=\"color: gray\">,<\/span><font color=\"#000000\"> <span style=\"color: red\">&#39;nvarchar(2000)&#39;<\/span><span style=\"color: gray\">)<\/span><font color=\"#000000\"> <span style=\"color: gray\">)<\/span><font face=\"courier new,courier\" color=\"#000000\"> <br \/>\n<\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">RETURN<\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; color: gray; font-size: 10pt\">;<br \/>\n<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">&nbsp;<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; color: green; font-size: 10pt\">&#8211;Test the trigger.<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; color: green; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">CREATE<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: blue\">TABLE<\/span><font color=\"#000000\"> TestTable <span style=\"color: gray\">(<\/span><font color=\"#000000\">col1 <span style=\"color: blue\">int<\/span><span style=\"color: gray\"><font face=\"courier new,courier\">);<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">DROP<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: blue\">TABLE<\/span><font color=\"#000000\"> SecurityAdministration<span style=\"color: gray\">.<\/span><font color=\"#000000\">AuditDDLOperations<span style=\"color: gray\"><font face=\"courier new,courier\">;<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">EXECUTE<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: blue\">AS<\/span><font color=\"#000000\"> <span style=\"color: blue\">LOGIN<\/span><font color=\"#000000\"> <span style=\"color: gray\">=<\/span><font color=\"#000000\"> <span style=\"color: red\">&#39;Paul&#39;<\/span><font color=\"#000000\"> <span style=\"color: green\"><font face=\"courier new,courier\">&#8212; note: Remember, Paul is a DDL_admin<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">DROP<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: blue\">TABLE<\/span><font color=\"#000000\"> SecurityAdministration<span style=\"color: gray\">.<\/span><font color=\"#000000\">AuditDDLOperations<span style=\"color: gray\"><font face=\"courier new,courier\">;<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">REVERT<\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; color: gray; font-size: 10pt\">;<br \/>\n<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">&nbsp;<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">SELECT<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: gray\">*<\/span><font color=\"#000000\"> <span style=\"color: blue\">FROM<\/span><font color=\"#000000\"> SecurityAdministration<span style=\"color: gray\">.<\/span><font color=\"#000000\">AuditDDLOperations<span style=\"color: gray\"><font face=\"courier new,courier\">;<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">DROP<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: blue\">TRIGGER<\/span><font color=\"#000000\"> <font color=\"#000000\">PreventAllDDL <span style=\"color: blue\">ON<\/span><font color=\"#000000\"> <span style=\"color: blue\">DATABASE<\/span><span style=\"color: gray\"><font face=\"courier new,courier\">;<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">DROP<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: blue\">TABLE<\/span><font color=\"#000000\"> SecurityAdministration<span style=\"color: gray\">.<\/span><font color=\"#000000\">AuditDDLOperations<span style=\"color: gray\"><font face=\"courier new,courier\">;<br \/>\n<\/span><\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">DROP<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: blue\">SCHEMA<\/span><font face=\"courier new,courier\" color=\"#000000\"> SecurityAdministration;<br \/>\n<\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><\/span><span style=\"font-family: 'Lucida Sans Unicode'; color: blue; font-size: 10pt\"><font face=\"courier new,courier\">DROP<\/span><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><font color=\"#000000\"> <span style=\"color: fuchsia\">USER<\/span><font face=\"courier new,courier\" color=\"#000000\"> Paul;<br \/>\n<\/span><font face=\"courier new,courier\"><span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font color=\"#000000\">go<span style=\"color: blue\">&nbsp;<\/span><\/span>\n<\/p>\n<p>\n<span style=\"font-family: 'Lucida Sans Unicode'; font-size: 10pt\"><font face=\"courier new,courier\"><span style=\"color: blue\">DROP<\/span><font color=\"#000000\"> <span style=\"color: blue\">LOGIN<\/span><font face=\"courier new,courier\" color=\"#000000\"> Paul;<br \/>\ngo<\/span><font face=\"courier new,courier\"><span style=\"font-size: 10pt\"><font color=\"#000000\">&nbsp;<\/span>\n<\/p>\n<p>\n<span style=\"font-size: 10pt\"><\/span><span style=\"font-family: Verdana; color: #003300; font-size: 10pt\">So, have fun testing with this one.<\/span><span style=\"font-family: Verdana; color: #003300; font-size: 10pt\">&nbsp;<\/span>\n<\/p>\n<p>\n<span style=\"font-family: Verdana; color: #003300; font-size: 10pt\"><\/span><span style=\"font-family: Verdana; color: #003300; font-size: 10pt\">Thanks for reading!<\/span> <br \/>\n<span style=\"font-family: Verdana; color: #003300; font-size: 10pt\">kt<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>DDL Triggers were a new feature of SQL Server 2005 and while seemingly simple, they are very powerful. DDL Triggers allow you to trap an attempted DDL operation to audit it, prevent it, or do anything you want to validate\/verify\/&rdquo;authorize&rdquo;\/etc &ndash; you write the code. And, since a trigger fires as part of the transaction, [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,28,59,62,65,78],"tags":[],"class_list":["post-604","post","type-post","status-publish","format-standard","hentry","category-dynamic-string-execution","category-execute-as","category-security","category-sp_executesql","category-sql-server-2005","category-tips"],"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/comments?post=604"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/604\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/media?parent=604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/categories?post=604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/tags?post=604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}