trigger problem

  • I've some problem wif my triger statement... after execute this trigger, i got this message "Msg 156, Level 15, State 1, Procedure trig_updateOrganization, Line 28

    Incorrect syntax near the keyword 'ELSE'."

    What shud i do? Below is my trigger. Somebody help me....

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    go

    ALTER TRIGGER [trig_updateOrganization]

    ON [dbo].[OrganizationDetail]

    FOR UPDATE

    AS

    DECLARE @oldName VARCHAR(100)

    DECLARE @newName VARCHAR(100)

    DECLARE @oldAdress VARCHAR(100)

    DECLARE @newAdress VARCHAR(100)

    IF NOT UPDATE(Org_name)

    BEGIN

    RETURN

    END

    SELECT @oldName = (SELECT Org_name + ' ' FROM Deleted)

    SELECT @newName = (SELECT Org_name + ' ' FROM Inserted)

    PRINT 'Organization name changed from "' + @oldName +'" to "' + @newName + '"'

    PRINT 'Have a nice day'

    ELSE IF NOT UPDATE(Org_address)

    BEGIN

    RETURN

    END

    SELECT @oldAdress = (SELECT Org_address + ' ' FROM Deleted)

    SELECT @newAdress = (SELECT Org_address + ' ' FROM Inserted)

    PRINT 'Organization adress changed from "' + @oldAdress +'" to "' + @newAdress + '"'

    PRINT 'Have a nice day'

    T

  • you have messed up your if - sequence.

    if not update(...)

    begin

    ...

    end

    -- no other statements can exist between the end and the else

    else -- elseif does not exist in tsql

    begin

    if not ...

    ...

    end

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution πŸ˜€

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • If I'm interpreting your logic correctly you should just get rid of the ELSE and you'll be fine. Actually, try this

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    go

    ALTER TRIGGER [trig_updateOrganization]

    ON [dbo].[OrganizationDetail]

    FOR UPDATE

    AS

    DECLARE @oldName VARCHAR(100)

    DECLARE @newName VARCHAR(100)

    DECLARE @oldAddress VARCHAR(100)

    DECLARE @newAddress VARCHAR(100)

    declare @Changed bit

    set @Changed = 0

    IF UPDATE(Org_name)

    BEGIN

    SELECT @oldName = (SELECT Org_name + ' ' FROM Deleted)

    SELECT @newName = (SELECT Org_name + ' ' FROM Inserted)

    PRINT 'Organization name changed from "' + @oldName +'" to "' + @newName + '"'

    set @Changed = 1

    END

    IF UPDATE(Org_address)

    BEGIN

    SELECT @oldAddress = (SELECT Org_address + ' ' FROM Deleted)

    SELECT @newAddress = (SELECT Org_address + ' ' FROM Inserted)

    PRINT 'Organization address changed from "' + @oldAddress +'" to "' + @newAddress + '"'

    END

    if @Changed = 1

    PRINT 'Have a nice day'

    Thanks very much for using the code formatting block too - it makes it easier to read! πŸ™‚ Finally, why the two spellings of address? There's no spelling of it without the double-d in any dictionary I've seen. I'm a bit of a pedant in that regard (saw a sign today at a clothes store saying "Priced too clear" - didn't buy a thing!) so I changed it :w00t: 😎 πŸ˜€

  • I'm going to say this so that everyone gets the point... πŸ˜‰

    No, No, No, No!!!!

    Now that I have your attention, the reason why I'm being so adamant is because all of the code on this thread, so far, is designed to handle one and only one row! That's a form of "Death by SQL" especially where triggers are concerned. You MUST ALWAYS write triggers to handle more than 1 row! It's not always a RBAR GUI that's going to be adding or modifying a row in a table... it could be a batch job that updates thousands of rows in one shot! The way the trigger examples have been written so far, I could update a million Org_Name and Address changes and only the first of each would be handled by the trigger. In other words, the trigger wouldn't even see the other 999,999 rows...

    ... actually, it would... the following two lines would give you an error about a sub-query in the Select list returning more than 1 value if you tried to update more than 1 row in the table...

    SELECT @oldName = (SELECT Org_name + ' ' FROM Deleted)

    SELECT @newName = (SELECT Org_name + ' ' FROM Inserted)

    Like I said, you MUST ALWAYS write triggers to handle more than just simple RBAR. Something like this would do...


    [font="Courier New"]ALTER&nbspTRIGGER&nbsp[trig_updateOrganization]

    &nbsp&nbsp&nbsp&nbsp&nbspON&nbsp[dbo].[OrganizationDetail]

    &nbsp&nbsp&nbsp&nbspFOR&nbspUPDATE&nbsp

    &nbsp&nbsp&nbsp&nbsp&nbspAS

    &nbsp&nbsp&nbsp&nbsp&nbspIF&nbspUPDATE(Org_name)&nbsp

    --=====&nbspReturn&nbspa&nbspcomplete&nbsplist&nbspof&nbspALL&nbsprows&nbspthat&nbspchanged&nbspnames

    &nbsp&nbsp&nbsp&nbsp&nbsp--&nbspSpecial&nbsphandling&nbspIS&nbsprequired&nbspto&nbspdetect&nbspthe&nbspfirst&nbspchange

    &nbsp&nbsp&nbsp&nbsp&nbsp--&nbspbecause&nbspit&nbspstarts&nbspout&nbspas&nbspNULL&nbspwhich&nbspcannot&nbspbe&nbspcompared&nbspdirectly.

    &nbsp&nbspBEGIN

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspPRINT&nbsp'The&nbspfollowing&nbsporganization&nbspnames&nbsphave&nbspchanged...'

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT&nbspd.Org_Name&nbspAS&nbspOldOrgName,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspi.Org_Name&nbspAS&nbspNewOrgName

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFROM&nbspDELETED&nbspd

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspINNER&nbspJOIN&nbspINSERTED&nbspi

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON&nbspd.PKColName&nbsp=&nbspi.PKColName&nbsp--<<LOOK!!!&nbspChange&nbspto&nbspcorrect&nbspcolumn&nbspname!!!

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHERE&nbspISNULL(d.Org_Name,'')&nbsp<>&nbspISNULL(i.Org_Name,'')

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspPRINT&nbsp'Have&nbspa&nbspnice&nbspday'

    &nbsp&nbsp&nbsp&nbspEND

    &nbsp&nbsp&nbsp&nbsp&nbspIF&nbspUPDATE(Org_address)&nbsp

    --=====&nbspReturn&nbspa&nbspcomplete&nbsplist&nbspof&nbspALL&nbsprows&nbspthat&nbspchanged&nbspaddresses

    &nbsp&nbsp&nbsp&nbsp&nbsp--&nbspSpecial&nbsphandling&nbspIS&nbsprequired&nbspto&nbspdetect&nbspthe&nbspfirst&nbspchange

    &nbsp&nbsp&nbsp&nbsp&nbsp--&nbspbecause&nbspit&nbspstarts&nbspout&nbspas&nbspNULL&nbspwhich&nbspcannot&nbspbe&nbspcompared&nbspdirectly.

    &nbsp&nbspBEGIN

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspPRINT&nbsp'The&nbspfollowing&nbspaddresses&nbsphave&nbspchanged...'

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT&nbspd.Org_address&nbspAS&nbspOldAddress,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspi.Org_address&nbspAS&nbspNewAddress

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFROM&nbspDELETED&nbspd

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspINNER&nbspJOIN&nbspINSERTED&nbspi

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON&nbspd.PKColName&nbsp=&nbspi.PKColName&nbsp--<<LOOK!!!&nbspChange&nbspto&nbspcorrect&nbspcolumn&nbspname!!!

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHERE&nbspISNULL(d.Org_address,'')&nbsp<>&nbspISNULL(i.Org_address,'')

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspPRINT&nbsp'Have&nbspa&nbspnice&nbspday'

    &nbsp&nbsp&nbsp&nbspEND[/font]


    Of course, since no one bothered to post the table schema nor any sample data, I've not tested the code above... πŸ˜›

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks Jeff for adding this little - but oh so crucial remark :w00t:

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution πŸ˜€

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Heh... I guess I get carried away a bit on the "little" stuff, Johan. πŸ˜‰ But, you're correct... it's a crucial error that both Newbies and those that write Oracle triggers make in SQL Server.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Beyond Jeff's correction to the trigger (very, very necessary), I have to question using "Print" in a trigger?

    Is this schoolwork of some sort? The only way a Print command will every come up from a trigger is if you are doing an update in Query Analyzer/Management Studio, so far as I know. (Am I mistaken about that?)

    In which case, either this trigger is some sort of demo, or you're telling the server to waste time and effort on an output that nobody will ever see.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Print commands, like rowcounts, can sometimes be made to bubble up through the app.

    However, I agree... I prefer to use Raiserror properly rather than the likes of PRINT... nothing should be returned to the app by a trigger, in my book, unless an error occurred.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Just as a note, PRINT statements sometimes apear in the errors collection.

    PRINT statements in SQL Server can also populate the ADO errors collection. However, PRINT statements are severity level zero (0) so, at least one RAISERROR statement is required in the stored procedure to retrieve a PRINT statement with ADO through the Errors collection.

    http://support.microsoft.com/kb/194792/en-us

    Best Regards,

    Chris BΓΌttner

  • Heh... I love instant confirmation! Thanks, Chris!

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply