SQL Server 2005 Update Trigger Effect Multiple Rows

I have two tables which I would like to keep records were changed in DeliveryTypeChanges if user updates the DeliveryCharge in the DeliveryType table.

First time I try to create a trigger that the “SELECT” statement without having “INNER JOIN Deleted” and keep value from “Deleted” to variable before “INSERT”, So what I get is only one record updates to the table.
First time I try to create a trigger that the “SELECT” statement without having “INNER JOIN Deleted” and keep value from “Deleted” to variable before “INSERT”, So what I get is only one record updates to the table.

There are two tables:

Create Table DeliveryType (
    DeliveryTypeID int not null
    Constraint pk_DeliveryType_DeliveryTypeID primary key,
    DeliveryTypeDescription varchar(10) not null,
    DeliveryCharge smallmoney not null
)

Create Table DeliveryTypeChanges (
    ChangeID int identity(1,1) not null
    Constraint pk_DeliveryTypeChanges_ChangeID primary key,
    ChangeDateTime datetime not null,
    DeliveryTypeDescription varchar(10) not null,
    OldDeliveryCharge smallmoney not null,
    NewDeliveryCharge smallmoney not null
)

Here is the trigger to make it works with multiple records:

CREATE TRIGGER trUpdateRecordDeliveryChargeChange
On DeliveryType
FOR UPDATE AS
IF UPDATE (DeliveryCharge)
BEGIN 

    INSERT DeliveryTypeChanges(ChangeDateTime, DeliveryTypeDescription, OldDeliveryCharge, NewDeliveryCharge)
    SELECT GETDATE(), Inserted.DeliveryTypeDescription, Deleted.DeliveryCharge, Inserted.DeliveryCharge
    FROM Inserted INNER JOIN Deleted
    ON Inserted.DeliveryTypeID = Deleted.DeliveryTypeID

END

2 people have left comments

Sachin - Gravatar

Sachin said:

Very good example, helpful

Posted on: December 18, 2008 at 1:08 pmQuote this Comment
Nikhilesh - Gravatar

Nikhilesh said:

Its really helpful
happy programming

Posted on: May 9, 2009 at 1:56 pmQuote this Comment

Leave a Comment-

Comment Guidelines: Basic XHTML is allowed (a href, strong, em, code). All line breaks and paragraphs are automatically generated. Off-topic or inappropriate comments will be edited or deleted. Email addresses will never be published. Keep it PG-13 people!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

All fields marked with "*" are required.