我想在与其他表链接的表上创建插入更新和删除的触发器 当发生上述任何一种情况时,触发器应从插入的表中提取一个值,并从连接的表中提取其他值 并将这些插入另一个表
这是一个简化的答案,但我在员工加入时收到错误说多部分标识符无法绑定
Create TRIGGER triggertest
ON salary
FOR INSERT
AS
BEGIN
DECLARE @salary int
DECLARE @firstName int
SELECT @salary = i.salary, @firstName =employee.firstName
FROM inserted i
INNER JOIN employee ON
i.employeeID = emplopyee.employeeID
INNER JOIN deleted d ON i.salaryID = d.salaryID
INSERT INTO testTAble
(employeeFirstName,EmployeeSalary)
select @firstName ,@salary
END
答案 0 :(得分:1)
您的要求可以是:
插入强>
CREATE TRIGGER triggertestI
ON salary
FOR INSERT
AS
BEGIN
INSERT INTO testTAble
(employeeFirstName,EmployeeSalary)
SELECT i.salary, employee.firstName
FROM inserted i
INNER JOIN employee
ON i.employeeID = employee.employeeID
END
<强>更新强>
CREATE TRIGGER triggertestU
ON salary
FOR UPDATE
AS
BEGIN
UPDATE testTAble
SET EmployeeSalary =
(SELECT i.salary
FROM inserted i
INNER JOIN employee
ON i.employeeID = employee.employeeID
where i.employeeID = testTable.employeeID)
END
删除强>
CREATE TRIGGER triggertestD
ON salary
FOR DELETE
AS
BEGIN
DELETE FROM testTAble
WHERE employeeID in (
SELECT i.employeeID
FROM deleted i
INNER JOIN employee
ON i.employeeID = employee.employeeID
where i.employeeID = testTable.employeeID)
END
P.S。我认为,在您的查询中,错误是:emplopyee(您在表名员工中添加了意外的非p)
答案 1 :(得分:0)
错误:
i.employeeID = emplopyee.employeeID
您的'p'
中添加了额外emplopyee.employeeID
。 emplopyee
必须为employee