Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating a ChangeLog Database on SQL Server 2005

Posted on 2014-01-03
2
Medium Priority
?
575 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:Carla Romere
[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 2000 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:Carla Romere
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

660 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