Question:
I’m new to programming and I’m doing a project for myself to consolidate the material. I tried all the methods, but without success. How to correctly write a trigger that, after entering data in the second table in the MOVING_QUANTITY attribute, automatically subtracted SP_PRODUCT_QUANTITY from the first table from MOVING_QUANTITY in the second. And the new result was already recorded in SP_PRODUCT_QUANTITY. In other words, a replacement for the first. I wanted to implement an idea where, for example, I transfer a certain amount of goods and enter the quantity I need, and he took the quantity of a certain product from the warehouse table. Below I have given the tables, and my own trigger, but it does not work correctly as I wanted.The result I want to achieve:
FK_SP_STORAGE_ID | FK_SP_PRODUCT_ID | FK_SP_PRODUCT_ID |
---|---|---|
Storage-1 | Coco-cola | 500 |
Storage-1 | Fanta | 500 |
MOVING_PRODUCT | MOVING_QUANTITY |
---|---|
Coco-cola | 400 |
Fanta | 400 |
Result:
FK_SP_STORAGE_ID | FK_SP_PRODUCT_ID | FK_SP_PRODUCT_ID |
---|---|---|
Storage-1 | Coco-cola | 100 |
Storage-1 | Fanta | 100 |
Answer:
You are referencing the wrong column name in the trigger.Your join condition is wrong. You are not joining to the column in
Moving_list
at allSP_PRODUCT_QUANTITY
with all MOVING_QUANTITY
from table Moving_list
. It will result in error when the Moving_list
table contains more than 1 rowMOVING_PRODUCT
not MOVING_ID
If you have better answer, please add a comment about this, thank you!