Saturday 8 August 2020

How To Handle Exception In A Stored Function/Procedure In PostgreSQL | E...




Only 3.9% of viewers are subscribing to my channel 😓.
I request you to please give click on Subscribe button.
It really helps me grow 😢.



-- without exception block

-- purpose is to handle exception in a stored function in PostgreSQL and call it



SELECT public.testing_function(100,0);



CREATE OR REPLACE FUNCTION public.testing_function(p_num1 numeric, p_num2 numeric)

RETURNS numeric

LANGUAGE 'plpgsql'

COST 100

VOLATILE

AS $BODY$

DECLARE

v_result numeric;

BEGIN

v_result := p_num1/p_num2;

RETURN v_result;

END;

$BODY$;







---with exception block



SELECT public.testing_function(100,0);



CREATE OR REPLACE FUNCTION public.testing_function(p_num1 numeric, p_num2 numeric)

RETURNS text

LANGUAGE 'plpgsql'

COST 100

VOLATILE

AS $BODY$

DECLARE

v_result text;

BEGIN

BEGIN

v_result := p_num1/p_num2;

EXCEPTION WHEN OTHERS THEN

v_result := 'You cannot divide by Zero, try Again';

END;



RETURN v_result;

END;

$BODY$;


No comments:

Post a Comment