Sunday 9 August 2020

How To Handle Exception In A Stored Procedure In PostgreSQL || PL/pgSQL ...




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 😢.



The Purpose Is To Handle Exception In A Stored Procedure In PostgreSQL || PL/pgSQL Tutorials Exception Handling



-- without exception block



CREATE TABLE public.testing (demo_column text);



CREATE OR REPLACE PROCEDURE public.testing_procedure(p_num1 numeric,p_num2 numeric)

LANGUAGE 'plpgsql'

AS $BODY$

DECLARE

v_result numeric;

BEGIN

v_result := p_num1/p_num2;

INSERT INTO public.testing(demo_column) VALUES(v_result);

END;

$BODY$;



CALL public.testing_procedure(100,0);



SELECT * from public.testing;









-- with exception block



CREATE TABLE public.testing (demo_column text);



CREATE OR REPLACE PROCEDURE public.testing_procedure(p_num1 numeric,p_num2 numeric)

LANGUAGE 'plpgsql'

AS $BODY$

DECLARE

v_result text;

BEGIN

BEGIN

v_result := p_num1/p_num2;

EXCEPTION WHEN OTHERS THEN

v_result := 'You cannot divide a number by zero.';

END;

INSERT INTO public.testing(demo_column) VALUES(v_result);



END;

$BODY$;



CALL public.testing_procedure(100,20);



SELECT * from public.testing;


No comments:

Post a Comment