Trouble shooting a Update, Insert TRIGGER...what's wrong with this syntax ?

  • Hi folks...Im trying to update the Last Modified cell in Table B, when Last Modified cell in Table A changes, but only if the transaction is a "C";

    CREATE TRIGGER [dbo].[Joe_trigger]

    ON [dbo].[TableA]

    AFTER insert,UPDATE

    AS

    BEGIN

    UPDATE TableB

    set TableB.LAST_MODIFIED = TableA.LAST_MODIFIED

    FROM TABLEA INNER JOIN

    TABLEB ON TABLEA.MATTER_UNO = TABLEB.MATTER_UNO

    WHERE TableA.payor_Type = 'C'

    GO

    ERROR I receive: Msg 102, Level 15, State 1, Procedure kimupdate_trigger, Line 10

    Incorrect syntax near 'C'.

    What am I doing wrong ?

    Thank you

  • 1. You have BEGIN without an END - drop the BEGIN or add an END

    2. Use the INSERTED virtual table to limit your updates on TableB to only those rows inserted / updated in TableA

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • Thank you MM, that did the trick...but could you please expand what you meant with INSERTED virtual table function ?

    Doesnt my trigger now only update future rows in Table B, only if the the payor type in Table A is 'C' ?

    Could you please show me the possible syntax ?

    Thank you

  • koln (1/15/2013)


    Thank you MM, that did the trick...but could you please expand what you meant with INSERTED virtual table function ?

    Doesnt my trigger now only update future rows in Table B, only if the the payor type in Table A is 'C' ?

    Could you please show me the possible syntax ?

    Thank you

    Well your code does not consider which rows in A have been updated. It will update all rows in B regardless.

    I don't understand your bit about "future rows"...

    I think you should read about inserted and deleted virtual tables. They are critical to writing triggers that actually work.

    http://msdn.microsoft.com/en-us/library/ms191300%28v=sql.105%29.aspx

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • It should look something like this:

    CREATE TRIGGER [dbo].[Joe_trigger] ON [dbo].[TableA]

    AFTER INSERT, UPDATE

    AS

    BEGIN;

    UPDATE B

    SET LAST_MODIFIED = I.LAST_MODIFIED

    FROM INSERTED AS I

    INNER JOIN TABLEB AS B

    ON I.MATTER_UNO = B.MATTER_UNO

    WHERE I.payor_Type = 'C';

    END;

    If you're using SQL 2008 (as per forum posted in), consider switching the Update From into a Merge statement.

    That would look like:

    CREATE TRIGGER [dbo].[Joe_trigger] ON [dbo].[TableA]

    AFTER INSERT, UPDATE

    AS

    BEGIN;

    MERGE INTO TableB AS Tgt

    USING

    (SELECT LAST_MODIFIED,

    MATTER_UNO

    FROM INSERTED

    WHERE payor_Type = 'C') AS Src

    ON Tgt.MATTER_UNO = Src.MATTER_UNO

    WHEN MATCHED

    THEN UPDATE

    SET LAST_MODIFIED = Src.LAST_MODIFIED;

    END;

    Merge has some safeguards built into that prevent things like updating the same row twice. Has the added advantage of being ISO-compliant, if that matters to you (does to some, doesn't to others), while Update From is T-SQL proprietary.

    - 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

  • Thanks guys...I like that merge function, and I will read up a bit more.

    Overall, I only want this trigger to fire when a row is added or a row is changed going forward...this is what I meant with future rows...I dont want it to fire for what is in there now...does the syntax provided by GSquared do that ?

    Hope Im being clear enough.

  • koln (1/15/2013)


    Thanks guys...I like that merge function, and I will read up a bit more.

    Overall, I only want this trigger to fire when a row is added or a row is changed going forward...this is what I meant with future rows...I dont want it to fire for what is in there now...does the syntax provided by GSquared do that ?

    Hope Im being clear enough.

    Remember that triggers only fire when something happens. Both versions of the code G2 provided will do what you are asking. The best way for you to determine that is to run this in your test/dev environment and check it out. Above all else make sure you understand the code. You are the one that has to debug it when production comes crashing down. Most people won't be too keen on "I don't really understand the code, I got from somebody on the internet". 😀

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • koln (1/15/2013)


    Thanks guys...I like that merge function, and I will read up a bit more.

    Overall, I only want this trigger to fire when a row is added or a row is changed going forward...this is what I meant with future rows...I dont want it to fire for what is in there now...does the syntax provided by GSquared do that ?

    Hope Im being clear enough.

    Triggers only fire when data is modified. In this case, the trigger calls for firing on Update and Insert statements. If you delete a row (or more than one), the trigger will ignore that, and the Last Update data in TableB will be unchanged.

    The use of the "inserted" table in the trigger ensures that it only does anything with the rows in TableA that have been modified by the command that is causing the trigger to fire.

    For example, if you update Row 1, but not rows 2 through 1000, the inserted table will only contain data for Row 1. Since you join from that to TableB, the only row(s) affected in TableB will be those that match something in "inserted"; in this case, Row 1.

    There is also a "deleted" table that triggers can access. Insert statements only generate rows in "inserted". Updates generate rows in "deleted" (data before the update) and in "inserted" (new data). Delete statements only generate rows in "deleted".

    Read up them here: http://technet.microsoft.com/en-us/library/ms191300(v=sql.105).aspx

    (You can adjust what version of SQL Server you are reading about with a drop-down at the top of that page.)

    Does that help?

    - 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

  • that helps, thank you very much everybody !

  • Viewing 9 posts - 1 through 8 (of 8 total)

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