0
I need to make a Rigger after insert
calling a function that takes the last record inserted into a certain table and inserts the ID of that last record into another table according to a select
that returns multiple ID’s. EX:
tb_1
.-------------.
| ID | NOME |
.-------------.
| 1 | A |
| 2 | B |
| 3 | C | <- Último ID inserido
'------'------'
tb_2
.-------------.
| ID | NOME |
|------|------|
| 41 | AAA |
| 42 | BBB |
| 43 | CCC |
| 44 | AAA |
| 45 | AAA |
'------'------'
INSERT INTO tb_3 (tb_1_id,tb_2_id)
VALUES (
(SELECT MAX(ID) FROM tb_1),
(SELECT ID FROM tb_2 WHERE NOME = 'AAA')
)
tb_3
.----------.----------.
| tb_1_id | tb_2_id |
|----------|----------|
| 3 | 41 |
| 3 | 44 |
| 3 | 45 |
'----------'----------'
How to make a insert
what makes this logic that I have shown? It is possible?
The structures of trigger
and function
I’ll do it later.
I don’t think it’s possible by Trigger because it’s two tables
– Motta
You basically explained the behavior, it makes sense your logic to me. Try to do, if you have any problem ask a question of the respective problem.
– David
I want to know if it is possible to make an Insert in this type, because the example I showed was wrong, but the logic is this.
– Spec4422
Vote today! Vote tomorrow! Vote forever! Vote consciously! Your vote is very important to our community, contribute to us, and help make Stack Overflow in Portuguese (Sopt) bigger and bigger. You can learn more at: Vote early, vote often
– Marcos Henzel
Have you evaluated the use of the RETURNING clause of the INSERT command? I believe it can facilitate your procedure.
– anonimo