I'm new to SQL Server.
I need a one answer that it is, I have a table tblemployee
and the columns are
empid,
name,
paddr1,
paddr2,
pcity,
pstate,
pzip,
caddr1,
caddr2,
ccity,
cstate,
czip
I have a another table tblpaddresshistory
and the columns are
paddrlistid,
paddr1,
paddr2,
pcity,
pstate,
pzip
and I have yet another table tblcaddresshistory
with these columns
caddrlistid,
caddr1,
caddr2,
ccity,
cstate,
czip
Here is my question: when I'm updating the tblemployee
of column values paddr1, paddr2, pcity, pstate, pzip, caddr1, caddr2, ccity, cstate, czip
, the old values should be inserted into tblpaddresshistory
and tblcaddresshistory
and new values should be updated in tblemployee
.
Try with this trigger
CREATE TRIGGER dbo.tr_tblemployee
ON dbo.tblemployee
AFTER UPDATE
AS
BEGIN
IF ( UPDATE(paddr1)
OR UPDATE(paddr2)
OR UPDATE (pcity)
OR UPDATE(pstate)
OR UPDATE(pzip) )
BEGIN
INSERT INTO dbo.tblpaddresshistory
(paddr1,
paddr2,
pcity,
pstate,
pzip)
SELECT paddr1,
paddr2,
pcity,
pstate,
pzip
FROM deleted;
END
IF ( UPDATE(caddr1)
OR UPDATE(caddr2)
OR UPDATE (ccity)
OR UPDATE(cstate)
OR UPDATE(czip) )
BEGIN
INSERT INTO dbo.tblcaddresshistory
(caddr1,
caddr2,
ccity,
cstate,
czip)
SELECT caddr1,
caddr2,
ccity,
cstate,
czip
FROM deleted;
END
END;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.