January 14, 2014 at 8:20 am
Hi I am a newbie and I am trying to setup a Trigger which will then use Raiserror to log a windows event log. So far I have done the following:
exec sp_addmessage @msgnum=50004,@severity=16,@msgtext='error',@with_log='true'
My trigger is as follows:
CREATE TRIGGER tgr_XXXX
on dbo.XXXXX
AFTER INSERT
AS
BEGIN
SELECT COUNT(*) FROM EVENTS
IF COUNT(*).=2
RAISERROR ( 50004,16,1)
***I get the error incorrect syntax near ')' line 9 which is the Raiserror string however if I run independently it works ***
Has anyone got any ideas desperate need of help !!!:w00t:
January 14, 2014 at 8:26 am
You are missing and END and you also have a period after count(*).CREATE TRIGGER tgr_XXXX
on dbo.XXXXX
AFTER INSERT
AS
BEGIN
SELECT COUNT(*) FROM EVENTS
IF COUNT(*)=2
RAISERROR ( 50004,16,1)
end
January 14, 2014 at 9:46 am
Hi Thanks for the swift response, that did get rid of my syntax error thanks.:-)
January 14, 2014 at 7:53 pm
The line "IF COUNT(*)=2" probably is not doing what you think it is doing. In this context "COUNT(*)" actually returns zero - I suspect that you really want the number of records in the EVENT table.
Also the statement "SELECT COUNT(*) FROM EVENTS" simply returns a number to the application that is Inserting data into table dbo.XXXXX. This is not likely to be what you intended either.
Perhaps the code below is closer to what you want...
CREATE TRIGGER tgr_XXXX
on dbo.XXXXX
AFTER INSERT
AS
BEGIN
-- SELECT COUNT(*) FROM EVENTS
IF (SELECT COUNT(*) FROM EVENTS)=2
RAISERROR ( 50004,16,1)
end
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply