Solved

Creating a ChangeLog Database on SQL Server 2005

Posted on 2014-01-03
2
565 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

710 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