Seaquel
PostgreSQL error code

PostgreSQL error P0002

P0002 is the SQLSTATE PostgreSQL reports for no data found, in class P0, PL/pgSQL Error.

SQLSTATEP0002
Condition nameno_data_found
ClassP0 — PL/pgSQL Error

Reproduce it, then fix it

SELECT INTO STRICT requires exactly one row and there’s no product 999. Without STRICT, a missing row leaves the variable NULL, and FOUND tells you what happened. Run the broken query to see the error, then switch to the fixed one. Edit either freely; every run is rolled back.

DO $$
DECLARE
  p products;
BEGIN
  SELECT * INTO STRICT p FROM products WHERE id = 999;
END $$;
DO $$
DECLARE
  p products;
BEGIN
  SELECT * INTO p FROM products WHERE id = 999;
  IF NOT FOUND THEN
    RAISE NOTICE 'no product 999';
  END IF;
END $$;

Source: PostgreSQL documentation, Appendix A