Solved

Creating a ChangeLog Database on SQL Server 2005

Posted on 2014-01-03
2
536 Views
Last Modified: 2014-01-03
I am following a script I got at a SQL conference that creates a ChangeLog table and then creates a trigger that fires on any creation, modification, or deletion of database objects. I ran the following script and the table was created successfully. However, it is failing on the trigger creation.
/****** Object: Table [dbo].[ChangeLog] ******/
SET
ANSI_NULLS ON
GO
SET
QUOTED_IDENTIFIER ON
GO
SET
ANSI_PADDING ON
GO
CREATE TABLE [dbo].[aer_ChangeLog](
[LogId] [bigint] IDENTITY(1,1) NOT NULL,
[DatabaseName] [nvarchar](256) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[EventType] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[ObjectName] [varchar](256) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[ObjectType] [nvarchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[SqlCommand] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[EventDate] [datetime] NOT NULL DEFAULT (getutcdate()),
[LoginName] [nvarchar](256) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
PRIMARY KEY NONCLUSTERED
	(
		[LogId] ASC
	)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
ON [PRIMARY]
GO
SET
ANSI_PADDING OFF


--Trigger
/****** Object: DdlTrigger [ddltrg_ObjectRevisionHistory] ******/
GO
SET
ANSI_NULLS ON
GO
SET
QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [aer_ddltrg_ObjectRevisionHistory]
ON [fin_pilot]
FOR
CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE, CREATE_TABLE, ALTER_TABLE, DROP_TABLE, CREATE_FUNCTION, ALTER_FUNCTION, DROP_FUNCTION,
CREATE_INDEX, ALTER_INDEX, DROP_INDEX, CREATE_TRIGGER, ALTER_TRIGGER, DROP_TRIGGER, CREATE_STATISTICS, DROP_STATISTICS, UPDATE_STATISTICS, CREATE_SCHEMA, ALTER_SCHEMA, DROP_SCHEMA, GRANT_DATABASE, DENY_DATABASE, REVOKE_DATABASE, CREATE_USER, ALTER_USER, DROP_USER, CREATE_VIEW, ALTER_VIEW, DROP_VIEW
as
BEGIN
SET NOCOUNT ON
DECLARE @data XML
SET @data = EVENTDATA()
INSERT INTO dbo.aer_ChangeLog
(databasename, eventtype, objectname, objecttype, sqlcommand, loginname)
VALUES
(
	@data.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'varchar(256)'),
	@data.value('(/EVENT_INSTANCE/EventType)[1]', 'varchar(50)'),
	@data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)'),
	@data.value('(/EVENT_INSTANCE/ObjectType)[1]', 'varchar(25)'),
	@data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'varchar(max)'),
	@data.value('(/EVENT_INSTANCE/LoginName)[1]', 'varchar(256)')
)
END


GO
SET
ANSI_NULLS OFF
GO
SET
QUOTED_IDENTIFIER OFF
GO
ENABLE
TRIGGER [aer_ddltrg_ObjectRevisionHistory] ON fin_pilot

Open in new window

The error that I receive on the trigger creation is:
Msg 1098, Level 15, State 1, Procedure aer_ddltrg_ObjectRevisionHistory, Line 21
The specified event type(s) is/are not valid on the specified target object.

Open in new window

I looked up the EVENTDATA() data types for sql server 2005 and the data types appear to be valid. The error is actually showing to be on the line containing the closing parentheses after the @data.value lines.

What is causing this error?
0
Comment
Question by:Hers2keep
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39755104
I think you have to change the below two lines in trigger creation script

from the below one
CREATE TRIGGER [aer_ddltrg_ObjectRevisionHistory]
ON [fin_pilot]

Open in new window


to

CREATE TRIGGER [aer_ddltrg_ObjectRevisionHistory]
ON DATABASE

Open in new window


Just keep it as database itself dont change it to database name.
0
 

Author Closing Comment

by:Hers2keep
ID: 39755126
That worked. I also had to modify the column names to be case specific. The final create trigger code:
CREATE TRIGGER [aer_ddltrg_ObjectRevisionHistory]
ON DATABASE
FOR
CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE, CREATE_TABLE, ALTER_TABLE, DROP_TABLE, CREATE_FUNCTION, ALTER_FUNCTION, DROP_FUNCTION,
CREATE_INDEX, ALTER_INDEX, DROP_INDEX, CREATE_TRIGGER, ALTER_TRIGGER, DROP_TRIGGER, CREATE_STATISTICS, DROP_STATISTICS, UPDATE_STATISTICS, CREATE_SCHEMA, ALTER_SCHEMA, DROP_SCHEMA, GRANT_DATABASE, DENY_DATABASE, REVOKE_DATABASE, CREATE_USER, ALTER_USER, DROP_USER, CREATE_VIEW, ALTER_VIEW, DROP_VIEW
as
BEGIN
SET NOCOUNT ON
DECLARE @data XML
SET @data = EVENTDATA()
INSERT INTO dbo.aer_ChangeLog
(DatabaseName, EventType, ObjectName, ObjectType, SqlCommand, LoginName)
VALUES
(
	@data.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'nvarchar(256)'),
	@data.value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(50)'),
	@data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)'),
	@data.value('(/EVENT_INSTANCE/ObjectType)[1]', 'nvarchar(25)'),
	@data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(max)'),
	@data.value('(/EVENT_INSTANCE/LoginName)[1]', 'nvarchar(256)')
)
END

Open in new window

0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now