I have two tables, tbl_stock and tbl_stockEntry, those two tables are identical.
They have these fields: warehouse cultivar variety quantity
when an entry is entered in to the tbl_stockEntry table I would like it to either update the matching entry in the tbl_stock table or create a new entry if there is no matching entry.
But I need it to match 3 fields, warehouse, cultivar and variety.
This is my trigger to create the entry, but I can't seem to get it to insert a record if none is found.
BEGIN
UPDATE tbl_stock
SET quantity = quantity + NEW.quantity
WHERE vegetableCultivar = NEW.vegetableCultivar
AND warehouse = NEW.warehouse;
END
I have tried this and but it doesnt work:
BEGIN
UPDATE tbl_stock
SET quantity = quantity + NEW.quantity
WHERE vegetableCultivar = NEW.vegetableCultivar
AND warehouse = NEW.warehouse;
if sql%rowcount = 0 then
-- nothing was updated, so the record doesn't exist, insert it.
insert into tbl_stock (warehouse, vegetableCultivar, vegetableVariety, quantity)
values (NEW.warehouse, NEW.vegetableCultivar, NEW.vegetableVariety, NEW.quantity);
end if;
END
If anyone knows how I could accomplish this I would really appreciate some assistance.