如果父 table 中有新值,则触发更新或插入其他 table 的 row/values
Trigger for update or insert row/values of other table if new vlaues in parent table
我有两个 tables Test1 和 Test 2.First table 从另一个作业中获取所有数据,第二个 table 在 ID 列的基础上具有唯一数据.
我需要编写一个触发器,如果在 table test1 中插入任何新行,则需要检查新插入行的 Price1 和 Price2 列的值是否为 id,如果其新值然后在 test2 的列 Price1 和 Price2 中更新以获取相应的 ID .
如果 Test2 中没有该 ID,则只需将其插入即可。
Table : Test1
-----------------------------
| ID | Price1 | Price2 |
-----------------------------
| 1 | 11 | 9 |
| 2 | 21 | 1 |
| 2 | 33 | 2 |
-----------------------------
Table : Test2
---------------------------------
| ID(PK) | Price1 | Price2 |
---------------------------------
| 1 | 11 | 9 |
| 2 | 33 | 2 |
---------------------------------
我尝试了下面的触发器,但它不起作用
CREATE OR REPLACE TRIGGER Test_Ord_Update
AFTER
insert or update on test1
for each row
DECLARE
v_id1 NUMBER (5);
begin
SELECT ID INTO v_id1
FROM test1;
update test2 set id = v_id1, Price1 = new.Price1, Price2 = new.Price2 where id = v_id1;
end;
你需要的是merge;至少,它确实 upsert 这就是你想要的。
示例表:
SQL> desc test1
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
PRICE1 NUMBER
PRICE2 NUMBER
SQL> desc test2
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
PRICE1 NUMBER
PRICE2 NUMBER
触发器:
SQL> create or replace trigger trg_aiu_test1
2 after insert or update on test1
3 for each row
4 begin
5 merge into test2 b
6 using dual a
7 on (:new.id = b.id)
8 when matched then update set
9 b.price1 = :new.price1,
10 b.price2 = :new.price2
11 when not matched then insert (id, price1, price2)
12 values (:new.id, :new.price1, :new.price2);
13 end;
14 /
Trigger created.
测试:
SQL> insert into test1 (id, price1, price2) values (1, 11, 9);
1 row created.
SQL> select * from test1;
ID PRICE1 PRICE2
---------- ---------- ----------
1 11 9
SQL> select * from test2;
ID PRICE1 PRICE2
---------- ---------- ----------
1 11 9
SQL> update test1 set price1 = 100 where id = 1;
1 row updated.
SQL> select * from test1;
ID PRICE1 PRICE2
---------- ---------- ----------
1 100 9
SQL> select * from test2;
ID PRICE1 PRICE2
---------- ---------- ----------
1 100 9
SQL>
我有两个 tables Test1 和 Test 2.First table 从另一个作业中获取所有数据,第二个 table 在 ID 列的基础上具有唯一数据. 我需要编写一个触发器,如果在 table test1 中插入任何新行,则需要检查新插入行的 Price1 和 Price2 列的值是否为 id,如果其新值然后在 test2 的列 Price1 和 Price2 中更新以获取相应的 ID . 如果 Test2 中没有该 ID,则只需将其插入即可。
Table : Test1
-----------------------------
| ID | Price1 | Price2 |
-----------------------------
| 1 | 11 | 9 |
| 2 | 21 | 1 |
| 2 | 33 | 2 |
-----------------------------
Table : Test2
---------------------------------
| ID(PK) | Price1 | Price2 |
---------------------------------
| 1 | 11 | 9 |
| 2 | 33 | 2 |
---------------------------------
我尝试了下面的触发器,但它不起作用
CREATE OR REPLACE TRIGGER Test_Ord_Update
AFTER
insert or update on test1
for each row
DECLARE
v_id1 NUMBER (5);
begin
SELECT ID INTO v_id1
FROM test1;
update test2 set id = v_id1, Price1 = new.Price1, Price2 = new.Price2 where id = v_id1;
end;
你需要的是merge;至少,它确实 upsert 这就是你想要的。
示例表:
SQL> desc test1
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
PRICE1 NUMBER
PRICE2 NUMBER
SQL> desc test2
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
PRICE1 NUMBER
PRICE2 NUMBER
触发器:
SQL> create or replace trigger trg_aiu_test1
2 after insert or update on test1
3 for each row
4 begin
5 merge into test2 b
6 using dual a
7 on (:new.id = b.id)
8 when matched then update set
9 b.price1 = :new.price1,
10 b.price2 = :new.price2
11 when not matched then insert (id, price1, price2)
12 values (:new.id, :new.price1, :new.price2);
13 end;
14 /
Trigger created.
测试:
SQL> insert into test1 (id, price1, price2) values (1, 11, 9);
1 row created.
SQL> select * from test1;
ID PRICE1 PRICE2
---------- ---------- ----------
1 11 9
SQL> select * from test2;
ID PRICE1 PRICE2
---------- ---------- ----------
1 11 9
SQL> update test1 set price1 = 100 where id = 1;
1 row updated.
SQL> select * from test1;
ID PRICE1 PRICE2
---------- ---------- ----------
1 100 9
SQL> select * from test2;
ID PRICE1 PRICE2
---------- ---------- ----------
1 100 9
SQL>