SQL更新触发条件
我正在研究sql中的触发器。我正在尝试执行更新触发器,但我希望它仅在满足特定条件时才起作用 例如,假设我有一个表X和两列a,B。 我希望仅当A小于B时才能更新A或B,以便更新新值 所以我在做一个这样的触发器SQL更新触发条件,sql,sql-server,triggers,Sql,Sql Server,Triggers,我正在研究sql中的触发器。我正在尝试执行更新触发器,但我希望它仅在满足特定条件时才起作用 例如,假设我有一个表X和两列a,B。 我希望仅当A小于B时才能更新A或B,以便更新新值 所以我在做一个这样的触发器 create trigger utrigger on X for update as if (update(A) OR update(B)) begin if (A>B) RAISERROR (N' Incorrect %s %d.', -- Message text.
create trigger utrigger
on X
for update as
if (update(A) OR update(B))
begin
if (A>B)
RAISERROR (N' Incorrect %s %d.', -- Message text.
10, -- Severity,
1, -- State,
N'number', -- First argument.
5); -- Second argument.
结束
然而,我认为我做错了。这有什么问题?您需要使用插入的虚拟表
create trigger utrigger
on X
for update as
if (update(A) OR update(B))
begin
if exists (SELECT * -- this subquery breaches the condition
FROM INSERTED
WHERE A>=B) -- might need some isnull if nulls are not allowed
RAISERROR (N' Incorrect %s %d.', -- Message text.
10, -- Severity,
1, -- State,
N'number', -- First argument.
5); -- Second argument.
end
您需要使用插入的虚拟表
create trigger utrigger
on X
for update as
if (update(A) OR update(B))
begin
if exists (SELECT * -- this subquery breaches the condition
FROM INSERTED
WHERE A>=B) -- might need some isnull if nulls are not allowed
RAISERROR (N' Incorrect %s %d.', -- Message text.
10, -- Severity,
1, -- State,
N'number', -- First argument.
5); -- Second argument.
end
此条件是否也适用于插入件?检查B上的检查约束此条件是否也适用于插入件?检查B上的检查约束