RAISEERROR

  • 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:

  • 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



    Microsoft Certified Master - SQL Server 2008
    Follow me on twitter: @keith_tate

    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Hi Thanks for the swift response, that did get rid of my syntax error thanks.:-)

  • 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