0

Write a trigger to count orders for a product where the supplier has less than 3 products left.

CREATE FUNCTION count_order() RETURNS TRIGGER
LANGUAGE PLPGSQL
AS 
$$
DECLARE
BEGIN 
IF NEW.stock < 3 THEN
select COUNT(*) FROM Products;
END IF;
RETURN NEW;
END;
$$

create trigger count_order after insert on Products
for each row execute procedure count_order();

Kept getting error message. Please assist me where I am wrong "ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. CONTEXT: PL/pgSQL function count_order() line 5 at SQL statement"

Husna
  • 1
  • 1) This function is not actually doing anything as far the trigger is concerned. 2) The error message is telling you the problem. See [Executing SQL Commands](https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-GENERAL-SQL). 3) What are you trying to achieve? 4) `RETURN NEW;` means nothing in an `AFTER` trigger. – Adrian Klaver Jun 17 '23 at 22:46

0 Answers0