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