I am writing a trigger function in PostgreSQL 9.5. When a column named action_flag
is updated from FALSE
to TRUE
, a function is called.
If order_num
is in some table, then I want the update from FALSE
to TRUE
to proceed.
Else, if order_num
is in some other table, then I want to raise an exception and prevent the update from happening.
I need assistance writing the UPDATE
within my IF
statement. The ELSEIF
part functions as expected.
CREATE OR REPLACE FUNCTION check_ingredient_availability()
RETURNS TRIGGER AS
$Body$
BEGIN
if (OLD.order_num IN ( SELECT order_num
FROM can_prep_m_orders )) THEN
RAISE NOTICE 'This is a notice.';
--UPDATE meal_order
--SET actioned_flag=TRUE
--WHERE order_num=OLD.order_NUM;
--EXECUTE 'UPDATE meal_order
-- SET actioned_flag=TRUE
--WHERE order_num=$1'
--USING OLD.order_num;
RETURN NULL;
ELSEIF (OLD.order_num IN ( SELECT order_num
FROM cannot_prep_m_orders )) THEN
RAISE EXCEPTION 'Missing ingredients. Cannot prepare order number %.',
OLD.order_num;
RETURN NULL;
ELSE
RETURN NULL;
END IF;
END;
$Body$ LANGUAGE plpgsql;