我的公司需要Customer
表中的列,该列将显示任何客户的先前地址数。每当地址更新时,此号码必须自动更新。
CREATE TRIGGER deleted_address ON tblcustomer
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
end
到目前为止,我有这个但是我已经遇到了一个问题,我不知道如果编辑了客户的地址,它会将旧地址添加到名为{{ 1}}然后更新PreviousAddress
答案 0 :(得分:0)
尝试看看这是否适合您:
CREATE TRIGGER deleted_address ON tblcustomer
AFTER UPDATE
AS
BEGIN
IF UPDATE (CustomerAddress)
UPDATE t
SET t.[PreviousAddress] = t.PreviousAddress + 1,
t.CustomerAddress = i.CustomerAddress
FROM tblcustomer t
JOIN INSERTED i ON t.customer_id = i.customer_id
END
GO
答案 1 :(得分:0)
要更新以前的地址,您可以编写这样的触发器。
CREATE TRIGGER trg_UpdateAddress
ON Customer
AFTER UPDATE
AS
BEGIN
DECLARE @id INT = (SELECT ID FROM DELETED)
UPDATE dbo.Customer SET PreviousAddress = (SELECT Address FROM DELETED )
WHERE ID = @id
END
答案 2 :(得分:0)
使用AFTER UPDATE
触发器:
CREATE TRIGGER deleted_address ON tblcustomer
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON
IF UPDATE (Address)
UPDATE t SET countPrev = countPrev + 1, oldAddress = d.Address
FROM tblcustomer t
JOIN INSERTED i ON t.customer_id = i.customer_id
JOIN DELETED d ON t.customer_id = d.customer_id
END