PostgreSQL Stored Procedure with INOUT Parameters
Summary: in this tutorial, you will learn how to create PostgreSQL stored procedures with INOUT
parameters.
Creating stored procedures with INOUT parameters
Sometimes, you may want to return values from stored procedures. To achieve this, you can use the create procedure
statement with INOUT
parameters.
Here’s the basic syntax for creating a stored procedure with INOUT
parameters:
Calling stored procedures with INOUT parameters
To call a stored procedure, you use the call
statement without providing the INOUT
parameters:
If you call a stored procedure with INOUT
parameters in an anonymous block, you need to pass arguments to the stored procedure call as follows:
PostgreSQL Stored Procedures with INOUT parameter examples
Let’s take some examples of creating stored procedures with INOUT
parameters. We’ll use the film
table in the sample database for the demonstration:
1) Basic PostgreSQL stored procedures with INOUT parameter example
First, create a stored procedure that counts the number of rows from the film
table:
Second, call the stored procedure without providing the total_film
parameter:
Output:
Third, call the stored procedure count_film()
in an anonymous block:
Output:
2) Creating stored procedures with multiple INOUT parameters
First, create a new stored procedure that retrieves the film statistics including film count, total length, and average rental rate:
Second, call the stored procedure film_stat()
:
Since all the parameters in the film_stat()
stored procedure are the inout
parameters, you don’t need to pass any parameters.
Output:
Summary
- Use the
INOUT
parameters to return values from stored procedures in PostgreSQL.