Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL更新触发条件_Sql_Sql Server_Triggers - Fatal编程技术网

SQL更新触发条件

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.

我正在研究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.
       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上的检查约束