触发以从表中删除记录,如果它存在
我有下表和表lightee_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
和depastion_id
和job_title_id
and manager_id
lightsee_assignment_future /code>每当来自雇员_assignment
的记录被更新时,如果是这样,则从lightsee_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
中删除任何内容,也没有在雇用> lightsee_assignment
中更新任何内容。
I have the following table and table employee_assignment_future
, which has the same columns.
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)
);
I need a trigger that checks if a employee_id
and department_id
and job_title_id
and manager_id
exists in employee_assignment_future
whenever the record from employee_assignment
is UPDATED, and if so to delete it from employee_assignment_future
. These 4 id's are foreign keys.
I came up with something like this:
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;
However, it's not deleting anything from employee_assignment_future
, and not updating anything in employee_assignment
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的
delete
+JOIN
应检测到将来是否存在记录,然后可以删除。以下删除可以是扳机的整个主体。
注意:您不能假设
插入
(或已删除
)只有一个行。您需要使用基于设置的操作并具有0-N行的处理。A simple
DELETE
+JOIN
should detect whether the record exists in the future and can then be deleted.The following delete can be the entire body of your trigger.
Note: You can't assume
Inserted
(orDeleted
) have only a single row. You need to use set based operations and handle it having 0-N rows.