Groups | Blog | Home
all groups > sql server programming > december 2006 >

sql server programming : Event Notification not generating.!!


bill k.
12/18/2006 10:23:00 PM
Hi, I ran below to setup Event Notification but it did not seem to work
(SELECT * FROM EventLog returns 0 row). Can you help?

-- enable Service Broker
USE AdventureWorks
ALTER DATABASE AdventureWorks SET ENABLE_BROKER;

-- drop and create the eventLog table
IF exists (select * from dbo.sysobjects where id =
object_id(N'dbo.EventLog') and OBJECTPROPERTY(id, N'IsTable') = 1)
DROP TABLE dbo.EventLog
GO
CREATE TABLE EventLog
(Command nvarchar(1000),
PostTime nvarchar(24),
HostName nvarchar(100),
LoginName nvarchar(100)
)
GO

-- create a queue
CREATE QUEUE NotifyQueue
GO

-- create an event processing service
CREATE SERVICE NotifyService
ON QUEUE NotifyQueue
(
[http://schemas.microsoft.com/SQL/Notifications/PostEventNotification]
)
GO

-- create a route
CREATE ROUTE NotifyRoute
WITH SERVICE_NAME = 'NotifyService',
ADDRESS = 'LOCAL'
GO


--create the database event notifications
CREATE EVENT NOTIFICATION NotifyCREATE_TABLE
ON DATABASE
FOR CREATE_TABLE
TO SERVICE 'NotifyService', 'current database'
GO

CREATE EVENT NOTIFICATION NotifyALTER_TABLE
ON DATABASE
FOR ALTER_TABLE
TO SERVICE 'NotifyService', 'current database'
GO

--create a table
CREATE TABLE T1 (col1 int)
GO

--process the event notifications
DECLARE @messageTypeName nvarchar(256),
@messageBody xml
;RECEIVE TOP(1)
@messageTypeName = message_type_name,
@messageBody = message_body
FROM dbo.NotifyQueue;

IF @@ROWCOUNT = 0
RETURN

PRINT CONVERT(nvarchar(1000),@messagebody)

DECLARE @cmd nvarchar(1000)
DECLARE @posttime nvarchar(24)
DECLARE @spid nvarchar(6)
DECLARE @hostname nvarchar(100)
DECLARE @loginname nvarchar(100)
SET @cmd = @messagebody.value
('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]',
'nvarchar(1000)')
SET @posttime = @messagebody.value
('(/EVENT_INSTANCE/PostTime)[1]', 'nvarchar(24)')
SET @spid = @messagebody.value
('(/EVENT_INSTANCE/SPID)[1]', 'nvarchar(6)')
SET @loginname = @messagebody.value
('(/EVENT_INSTANCE/LoginName)[1]', 'nvarchar(100)')
SET @hostname = HOST_NAME()


INSERT INTO EventLog(Command,PostTime,HostName,LoginName)
VALUES(@cmd, @posttime, @hostname, @loginname)
GO

Remus Rusanu [MSFT]
12/18/2006 10:58:15 PM
Check if the event is sitting in the sys.transmision_queue and cannot be
delivered. The transmission_status column should explain why it cannot be
delivered.

HTH,
~ Remus

[quoted text, click to view]

Uri Dimant
12/19/2006 12:00:00 AM
bill
http://www.sqljunkies.com/WebLog/ktegels/articles/EventNotifications.aspx ---Event
Notifications




[quoted text, click to view]

bill k.
12/19/2006 2:01:02 AM
Hi, Remus

It says this is the reason. What is problem here and how to resolve it?
Thank you,

An exception occurred while enqueueing a message in the target queue. Error:
15517, State: 1. Cannot execute as the database principal because the
principal "dbo" does not exist, this type of principal cannot be
impersonated, or you do not have permission.

[quoted text, click to view]
Remus Rusanu [MSFT]
12/19/2006 9:34:10 AM
The 'dbo' user is probably mapped to a login that no longer exists or
doesn't make sense on the instance. E.g this can happen if you create the
database on one machine logged in as a local user ('machine1\username'),
then copy the database to a different machine 'machine2' where the login
'machine1\username' is invalid.
The solution is to change the 'dbo' user to map to a valid login, like this:

ALTER AUTHORIZATION ON DATABASE::[databasename] TO [sa];

HTH,
~ Remus

[quoted text, click to view]

AddThis Social Bookmark Button