多表触发器
我正在尝试创建一个触发器,该触发器将从一个表中获取一个值,然后与另一个表中的其他值进行减法。
触发器将在表(存款)中操作,然后我想获取用户刚刚插入存款表的值,并用另一个表(帐户)中的值减去它。我可以毫无问题地编译以下代码:
CREATE OR REPLACE TRIGGER aifer_insättning
AFTER INSERT
ON deposit
FOR EACH ROW
WHEN (new.belopp is not null)
BEGIN
UPDATE account SET SALDO = saldo + :new.belopp;
end;
/
但是当然,这会更新表帐户中的所有行。我已尝试一切方法使其仅适用于一行,但我无法得到它。有人有什么建议我可以解决这个问题吗? 这是我的表格:
SQL> desc account
Name Null? Type
----------------------------------------- -------- ------------
KNR NOT NULL NUMBER(8)
KTNR NOT NULL NUMBER(6)
REGDATUM NOT NULL DATE
SALDO NUMBER(10,2)
SQL> desc deposit
Name Null? Type
----------------------------------------- -------- ------------
RADNR NOT NULL NUMBER(9)
PNR NOT NULL VARCHAR2(11)
KNR NOT NULL NUMBER(8)
BELOPP NUMBER(10,2)
DATUM NOT NULL DATE
I am trying to make a trigger that are going to take a value from one table and then make a subtraction with an other value in another table.
The trigger are going to operate in a table could (deposit), and then I would like to take the value that the user just inserted to the deposit table and subtract it with a value in another table(account). I have no problem to get following code to compile:
CREATE OR REPLACE TRIGGER aifer_insättning
AFTER INSERT
ON deposit
FOR EACH ROW
WHEN (new.belopp is not null)
BEGIN
UPDATE account SET SALDO = saldo + :new.belopp;
end;
/
But of course this update al the rows in the table account. I have tried everything to make it work with only one row, but I can´t get it. Does anyone have some suggestion hove I can resolve this?
Here is my tables:
SQL> desc account
Name Null? Type
----------------------------------------- -------- ------------
KNR NOT NULL NUMBER(8)
KTNR NOT NULL NUMBER(6)
REGDATUM NOT NULL DATE
SALDO NUMBER(10,2)
SQL> desc deposit
Name Null? Type
----------------------------------------- -------- ------------
RADNR NOT NULL NUMBER(9)
PNR NOT NULL VARCHAR2(11)
KNR NOT NULL NUMBER(8)
BELOPP NUMBER(10,2)
DATUM NOT NULL DATE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设公共列KNR是ACCOUNT的主键,我认为你想要的代码是:
注意,我添加了表别名以使代码更清晰。
Presuming that the common column KNR is the primary key of ACCOUNT, I think the code you want is:
Note that I have added a table alias to make the code clearer.
您不需要在触发器主体中的
update
语句中添加一个where
子句吗?或者我错过了什么?Wouldn't you just need to add a
where
clause to theupdate
statement in the trigger body? Or am I missing something?分隔符 $$
在更新员工之前创建触发器 ss_ss
对于每一行
开始
更新收货人设置
创建者 = NEW.employee_pin
其中created_by = OLD.employee_pin;
结束
$$
分隔符;
NOTE->员工和收货人是两个表
delimiter $$
create trigger ss_ss before update on employee
for each row
begin
update consignee set
created_by = NEW.employee_pin
where created_by = OLD.employee_pin;
end
$$
delimiter ;
NOTE-> employee and consignee are two tables
**
为所有表生成触发器
**
嗯,我最初这样做是为了为数据库中的所有表生成触发器来审计数据更改,这很简单,只需将整个行从已删除的表移动到镜像审计表即可。
但有人想要跟踪桌子上的活动,所以它更简单一些。这里我们创建一个日志表,任何时候发生DML操作,都会将其写入其中。
享受
**
Generate Triggers for all Tables
**
Well, I did this originally to generate triggers for all tables in a database to audit data changes, and that is simple enough, just move the entire row from the deleted table to a mirrored audit table.
But someone wanted to track activity on tables, so it's a little more simple. Here we create one log table, and any time a DML operation occurs, it is written there.
Enjoy