Home Forums SQL Server 2008 T-SQL (SS2K8) Update a Record Using a Trigger with SELECT Statement RE: Update a Record Using a Trigger with SELECT Statement

  • Your trigger has one major flow.

    When INSERT operation is performed the TRIGGER is fired for an operation, not for every inserted record.

    The way your trigger is written it will only process your logic for one of the inserted records.

    You should never read from INSERTED or DELETED into variables in triggers.

    Here is example of how it should be done properly:

    ALTER TRIGGER TR_Update_Interaction

    ON dbo.cust_hist

    AFTER INSERT AS

    BEGIN

    SET NOCOUNT ON;

    UPDATE cust_hist

    SET notes = v.consignment

    FROM cust_hist ch

    JOIN inserted i

    ON i.tkey = ch.tkey

    JOIN VLoadnoteCarr v

    ON v.load_note = i.primary_ref

    END

    Now, if it still doesn't update as expected, you need to analyse JOINs. Are they using relevant key columns?

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]