我正在研究一个触发器,它允许我将Customer表上指定列中的所有先前数据存储到指定的audit-table中。我从客户表和存储中获取的字段是特定的,因为customers表上有多个字段,我只是试图存储( CustomerID,CustomerAddress和CustomerPostcod e)
以下是Customer表中的字段:
[CustomerID]
[CustomerName]
[CustomerAddress]
[CustomerPostcode]
[CustomerTelephone]
[CardNumber]
[CountyID]
我只想尝试 [CustomerID] , [CustomerAddress] 和 [CustomerPostcode]
为了存储这些特定字段,我尽我所能设置了一个审计表,它将存储这些字段,但也会根据触发器显示自动生成的字段。
以下是我设置的审核表:
CREATE TABLE Customer_Audit
(
Cust_UpdateID int IDENTITY (1,1),
Cust_User char (8),
Cust_Update_Date date,
CustomerID int,
CustomerAddresss nvarchar (255),
CustomerPostCode nvarchar (255),
CONSTRAINT [pk_Cust_UpdateID] PRIMARY KEY (Cust_UpdateID)
)
这是我设置的触发器查询:
CREATE TRIGGER Customer_Update_Trigger ON tblCustomer
AFTER UPDATE
AS
BEGIN
INSERT INTO Customer_Audit (Cust_User, Cust_Update_Date, Cust_ID, CustomerAddresss, CustomerPostCode)
SELECT
CURRENT_USER,
GETDATE(),
d.CustomerID,
d.CustomerAddress,
d.CustomerPostcode
FROM deleted
END
为了存储数据,我正在尝试从删除的表中获取字段,但每次执行此操作时,我都会收到以下错误消息:
Msg 4104, Level 16, State 1, Procedure Customer_Update_Trigger, Line 11
The multi-part identifier "d.CustomerID" could not be bound.
Msg 4104, Level 16, State 1, Procedure Customer_Update_Trigger, Line 12
The multi-part identifier "d.CustomerAddress" could not be bound.
Msg 4104, Level 16, State 1, Procedure Customer_Update_Trigger, Line 13
The multi-part identifier "d.CustomerPostcode" could not be bound
我不知道它是什么我做错了,我知道已删除表中的字段名称与customer表中的字段名称匹配,但仍然拒绝处理。
答案 0 :(得分:1)
您错过了deleted
的别名,请尝试添加FROM deleted as d
CREATE TRIGGER Customer_Update_Trigger ON tblCustomer
AFTER UPDATE AS
BEGIN;
INSERT INTO Customer_Audit (
Cust_User
, Cust_Update_Date
, Cust_ID
, CustomerAddresss
, CustomerPostCode
)
SELECT
CURRENT_USER
, GETDATE()
, d.CustomerID
, d.CustomerAddress
, d.CustomerPostcode
FROM deleted as d;
END;