Tuesday, 11 August 2020

How To Create A Stored Procedure And Return Multiple Values From The Sto...



-- purpose is to return multiple values (more than one value) from a stored procedure

-- in PostgreSQL


How To Create A Stored Procedure And Return Multiple Values From The Stored Procedure In PostgreSQL

In this comprehensive tutorial, we’ll show you how to create a stored procedure in PostgreSQL that returns multiple values. Stored procedures are an essential tool in database management, allowing you to encapsulate complex logic and reuse it throughout your applications.

We begin by explaining the basics of stored procedures in PostgreSQL, including the syntax and the different ways to return values. You’ll learn how to declare variables, use the OUT parameters, and return multiple results from a single procedure. We’ll also cover how to call the stored procedure from pgAdmin and SQL Shell (psql), making sure you’re comfortable executing these procedures in different environments.

This video is ideal for developers, database administrators, and anyone looking to deepen their understanding of PostgreSQL. By the end of the tutorial, you’ll have the skills to create your own stored procedures that can return multiple values, enhancing the efficiency and flexibility of your database operations.

PostgreSQL, stored procedure, multiple values, PostgreSQL tutorial, PLpgSQL, pgAdmin, database development, SQL commands, database management, returning multiple values


CREATE OR REPLACE PROCEDURE public.testing_procedure(p_num1 IN numeric,p_num2 IN numeric,p_sum INOUT numeric,p_mult INOUT numeric,p_div INOUT numeric)

LANGUAGE 'plpgsql'

AS $BODY$

DECLARE

BEGIN

p_sum := p_num1 + p_num2;

p_mult := p_num1 * p_num2;

BEGIN

p_div := p_num1/p_num2;

EXCEPTION WHEN OTHERS THEN

p_div := 0;

END;

END;

$BODY$;



CALL public.testing_procedure(10,0,null,null,0);







CREATE OR REPLACE PROCEDURE public.testing_procedure(p_num1 IN numeric,p_num2 IN numeric,p_sum INOUT numeric,p_mult INOUT numeric,p_div INOUT text)

LANGUAGE 'plpgsql'

AS $BODY$

DECLARE

BEGIN

p_sum := p_num1 + p_num2;

p_mult := p_num1 * p_num2;

BEGIN

p_div := p_num1/p_num2;

EXCEPTION WHEN OTHERS THEN

p_div := 'You cannot divide a value by zero';

END;

END;

$BODY$;



CALL public.testing_procedure(10,0,null,null,'0');


No comments:

Post a Comment