触发从 table 中删除记录(如果存在)
Trigger to delete the record from the table if it exists in it
我有以下 table 和 table employee_assignment_future
,它们具有相同的列。
CREATE TABLE dbo.employee_assignment (
employee_id BIGINT,
department_id BIGINT,
job_title_id BIGINT,
manager_id BIGINT,
effective_start_date SMALLDATETIME,
effective_end_date SMALLDATETIME,
action_code VARCHAR(60)
);
我需要一个触发器来检查 employee_id
和 department_id
以及 job_title_id
和 manager_id
是否存在于 employee_assignment_future
每当来自 [=19 的记录时=] 已更新,如果是,则将其从 employee_assignment_future
中删除。这 4 个 ID 是外键。
我想到了这样的东西:
CREATE OR ALTER TRIGGER t_v_employee_assignment_update3 on employee_assignment
AFTER UPDATE
AS
BEGIN
DECLARE @emp_id BIGINT = (SELECT employee_id from inserted)
DECLARE @dep_id BIGINT = (SELECT department_id from inserted)
DECLARE @job_id BIGINT = (SELECT job_title_id from inserted)
DECLARE @man_id BIGINT = (SELECT manager_id from inserted)
DELETE FROM employee_assignment_future
WHERE @emp_id = employee_assignment_future.employee_id
AND @dep_id = employee_assignment_future.department_id
AND @job_id = employee_assignment_future.job_title_id
AND @man_id = employee_assignment_future.manager_id
END;
SELECT * FROM employee_assignment_future
SELECT * FROM employee_assignment
UPDATE employee_assignment
SET employee_id = 4, department_id = 2, job_title_id = 8, manager_id = 3, effective_start_date ='2019-11-13 00:00:00', effective_end_date = NULL, action_code = NULL
WHERE employee_id = 64;
但是,它不会从 employee_assignment_future
中删除任何内容,也不会更新 employee_assignment
中的任何内容。
一个简单的DELETE
+ JOIN
应该检测记录是否存在,然后可以删除。
下面的删除可以是你触发器的整个主体。
DELETE F
FROM employee_assignment_future F
INNER JOIN Inserted I
ON I.employee_id = F.employee_id
AND I.department_id = F.department_id
AND I.job_title_id = F.job_title_id
AND I.manager_id = F.manager_id;
注意:您不能假设 Inserted
(或 Deleted
)只有一行。您需要使用基于集合的操作并处理它有 0-N 行。
我有以下 table 和 table employee_assignment_future
,它们具有相同的列。
CREATE TABLE dbo.employee_assignment (
employee_id BIGINT,
department_id BIGINT,
job_title_id BIGINT,
manager_id BIGINT,
effective_start_date SMALLDATETIME,
effective_end_date SMALLDATETIME,
action_code VARCHAR(60)
);
我需要一个触发器来检查 employee_id
和 department_id
以及 job_title_id
和 manager_id
是否存在于 employee_assignment_future
每当来自 [=19 的记录时=] 已更新,如果是,则将其从 employee_assignment_future
中删除。这 4 个 ID 是外键。
我想到了这样的东西:
CREATE OR ALTER TRIGGER t_v_employee_assignment_update3 on employee_assignment
AFTER UPDATE
AS
BEGIN
DECLARE @emp_id BIGINT = (SELECT employee_id from inserted)
DECLARE @dep_id BIGINT = (SELECT department_id from inserted)
DECLARE @job_id BIGINT = (SELECT job_title_id from inserted)
DECLARE @man_id BIGINT = (SELECT manager_id from inserted)
DELETE FROM employee_assignment_future
WHERE @emp_id = employee_assignment_future.employee_id
AND @dep_id = employee_assignment_future.department_id
AND @job_id = employee_assignment_future.job_title_id
AND @man_id = employee_assignment_future.manager_id
END;
SELECT * FROM employee_assignment_future
SELECT * FROM employee_assignment
UPDATE employee_assignment
SET employee_id = 4, department_id = 2, job_title_id = 8, manager_id = 3, effective_start_date ='2019-11-13 00:00:00', effective_end_date = NULL, action_code = NULL
WHERE employee_id = 64;
但是,它不会从 employee_assignment_future
中删除任何内容,也不会更新 employee_assignment
中的任何内容。
一个简单的DELETE
+ JOIN
应该检测记录是否存在,然后可以删除。
下面的删除可以是你触发器的整个主体。
DELETE F
FROM employee_assignment_future F
INNER JOIN Inserted I
ON I.employee_id = F.employee_id
AND I.department_id = F.department_id
AND I.job_title_id = F.job_title_id
AND I.manager_id = F.manager_id;
注意:您不能假设 Inserted
(或 Deleted
)只有一行。您需要使用基于集合的操作并处理它有 0-N 行。