【发布时间】:2018-12-29 20:25:40
【问题描述】:
create trigger thequery4
on employees
instead of insert, delete
as
declare @var3 int
select @var3 = d.D_ID
from inserted d, DEPARTMENTS dp
where d.D_ID = dp.D_ID
if (@var3 is null)
begin
print 'you are NOT allowed , B/C IS Its d_Id is not present in department '
end
else
begin
---- insert ----
end
【问题讨论】:
-
和普通插入一样。没有魔法
-
Bad habits to kick : using old-style JOINs - 旧式 逗号分隔的表格列表 样式已替换为 ANSI 中的 proper ANSI
JOIN语法-92 SQL 标准(25 多年前),不鼓励使用。 -
触发器也可以影响多行,这里假设只有一个。并且您为 DELETE 和 INSERT 声明它,但在删除情况下 INSERTED 表将为空
-
看起来你需要一个外键而不是触发器
标签: sql-server triggers