sql server UPDATE 触发器的问题

2024-11-23 12:27:20
推荐回答(2个)
回答1:

alter
trigger
dd
on
dingdanxinxi
after
update
as
begin
set
nocount
on;
declare
@id
int,@cha
int,@new
int,@old
int
select
@id=商品编号,@old=数量
from
deleted
select
@new=数量
from
inserted
if
@new
>@old
begin
set
@cha=@new-@old
update
kucunxinxi
set
库存数量=库存数量+@cha
where
商品编号=@id
end
else
begin
set
@cha=@new-@old
update
kucunxinxi
set
库存数量=库存数量+@cha
where
商品编号=@id
end
end
你的代码设计有问题,库存数量=库存数量+@cha
where
商品编号=@id,还有就是:else后也是一个整体,要用begin……end,我都改过了,自己在看一下……,不懂的话,继续问哈!!!

回答2:

CREATE OR REPLACE TRIGGER TI_TEST
BEFORE UPDATE
ON TABLE_TEST
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
DECLARE

BEGIN
IF :NEW.ENABLE = '1' AND :OLD.ENABLE = '0' THEN
UPDATE TABLE_TEST SET ENABLE = '0' WHERE ENABLE = '1';
END IF;
EXCEPTION
WHEN OTHERS
THEN
-- Consider logging the error and then re-raise
RAISE;
END;
/