>>Proc UpdateAddress(@Addressid int,@line1 varchar(35),(@line2 varchar(35), >>(@line3varchar(35), >>(@line4 varchar(35)) >>As >>Insert into AddressHistory >>(Addressid int, >>line1 , >>line2, >>line3, >>line4, >>xline1, >>xline2, >>xline3, >>xline4) >>(Select >>Addressid, >>line1, >>line2, >>line3, >>line4, >>@line1, >>@line2, >>@line3, >>@line4 from address where addressid = @Addressid ) >> >>-- Then update the address table with new values >> >> >> >>>
>CREATE TRIGGER ON [dbo].[Address] > FOR UPDATE,INSERT >AS >BEGIN > *** Insert newly created records/UPDATEd in AddressHistory > INSERT INTO AddressHistory (Addressid, > line1 , > line2, > line3, > line4, > xline1, > xline2, > xline3, > xline4) > > SELECT AddressId, > line1 , > line2, > line3, > line4, > ISNULL(DELETED.Line1,''), > ISNULL(DELETED.Line2,''), > ISNULL(DELETED.Line3,''), > ISNULL(DELETED.Line4,'') > FROM INSERTED > LEFT JOIN DELETED ON INSERTED.AddressId = DELETED.AddressId >END >>
>INSERT INTO Address ..... >UPDATE Address SET Line1 = 'asdasdasdas' WHERE AddressId = 123 >Thanks Boris, works like a charm - I actually also pull fields from related tables into the history table could I still do this in a trigger ?