Editing DdlDatabaseTriggerLog (database trigger)

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 5: Line 5:
 
| '''databasetrigger
 
| '''databasetrigger
 
| ddlDatabaseTriggerLog
 
| ddlDatabaseTriggerLog
|-
+
|- valign="top"
 +
 
 
|}
 
|}
  
Line 64: Line 65:
  
 
|-
 
|-
| Insert
 
| Table
 
| [[dbo.DatabaseLog_(table)|dbo.DatabaseLog]]
 
 
|}
 
== wikibot ==
 
 
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 
|- valign="top"
 
| '''Database Trigger
 
| ddlDatabaseTriggerLog
 
|- valign="top"
 
| '''Description
 
| Database trigger to audit all of the DDL changes made to the AdventureWorks database.
 
|}
 
 
=== Source ===
 
<pre>
 
CREATE TRIGGER [ddlDatabaseTriggerLog] ON DATABASE
 
FOR DDL_DATABASE_LEVEL_EVENTS AS
 
BEGIN
 
    SET NOCOUNT ON;
 
 
    DECLARE @data XML;
 
    DECLARE @schema sysname;
 
    DECLARE @object sysname;
 
    DECLARE @eventType sysname;
 
 
    SET @data = EVENTDATA();
 
    SET @eventType = @data.value('(/EVENT_INSTANCE/EventType)[1]', 'sysname');
 
    SET @schema = @data.value('(/EVENT_INSTANCE/SchemaName)[1]', 'sysname');
 
    SET @object = @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'sysname')
 
 
    IF @object IS NOT NULL
 
        PRINT '  ' + @eventType + ' - ' + @schema + '.' + @object;
 
    ELSE
 
        PRINT '  ' + @eventType + ' - ' + @schema;
 
 
    IF @eventType IS NULL
 
        PRINT CONVERT(nvarchar(max), @data);
 
 
    INSERT [dbo].[DatabaseLog]
 
        (
 
        [PostTime],
 
        [DatabaseUser],
 
        [Event],
 
        [Schema],
 
        [Object],
 
        [TSQL],
 
        [XmlEvent]
 
        )
 
    VALUES
 
        (
 
        GETDATE(),
 
        CONVERT(sysname, CURRENT_USER),
 
        @eventType,
 
        CONVERT(sysname, @schema),
 
        CONVERT(sysname, @object),
 
        @data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(max)'),
 
        @data
 
        );
 
END;
 
</pre>
 
 
=== References ===
 
{| border="1" cellpadding="5" cellspacing="0" style="border-collapse:collapse"
 
|- style="background:silver"
 
| '''Dependency Type
 
| '''Object Type
 
| '''Referenced Object
 
 
|- valign="top"
 
 
| Insert
 
| Insert
 
| Table
 
| Table

Please note that all contributions to dbscript Online Help may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Project:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)