Thursday, May 9, 2013

trigger - to capture data into history table

Hi


@@rowcount returns the effected row of the last command, this could be from any command, use something like this;



CREATE TRIGGER [dbo].[trg_stuPropertyAddressLog] ON [dbo].[stuPropertyAddress]
FOR UPDATE
AS
DECLARE @MYROWS INT;

SELECT @MYROWS = COUNT(*)
FROM Deleted;

IF @MYROWS = 0
BEGIN
RETURN ;
END
ELSE
BEGIN

INSERT INTO dbo.[stuPropertyAddressLOG]
(StudentID ,
AddressID ,
ApartmentNum ,
MoveEffectiveDt ,
CreateDt ,
CreatedBy
)
SELECT del.StudentID ,
del.AddressID ,
del.ApartmentNum ,
del.MoveEffectiveDt ,
del.CreateDt ,
del.CreatedBy
FROM deleted del
END





-- Please mark my post as an answer if I helped you to resolve the issue or vote up if it helped, thank you--


No comments:

Post a Comment