PostgreSQL CREATE PROCEDURE by Examples
PostgreSQL CREATE PROCEDURE by Examples
Summary: in this tutorial, you will learn how to use the PostgreSQL CREATE PROCEDURE statement
to create new stored procedures.
To define a new stored procedure, you use the create procedure statement.
The following illustrates the basic syntax of the create procedure statement:
In this syntax:
First, specify the name of the stored procedure after the create procedure keywords.
https://www.postgresqltutorial.com/postgresql-plpgsql/postgresql-create-procedure/ 1/4
2023/7/14 下午5:25 PostgreSQL CREATE PROCEDURE By Examples
Second, define parameters for the stored procedure. A stored procedure can accept zero or
more parameters.
Third, specify plpgsql as the procedural language for the stored procedure. Note that you
can use other procedural languages for the stored procedure such as SQL, C, etc.
Finally, use the dollar-quoted string constant syntax to define the body of the stored
procedure.
Parameters in stored procedures can have the in and inout modes. They cannot have the out
mode.
A stored procedure does not return a value. You cannot use the return statement with a value
inside a store procedure like this:
return expression;
However, you can use the return statement without the expression to stop the stored
procedure immediately:
return;
If you want to return a value from a stored procedure, you can use parameters with the inout
mode.
https://www.postgresqltutorial.com/postgresql-plpgsql/postgresql-create-procedure/ 2/4
2023/7/14 下午5:25 PostgreSQL CREATE PROCEDURE By Examples
The following statement shows the data from the accounts table:
The following example creates a stored procedure named transfer that transfers a specified
amount of money from one account to another.
https://www.postgresqltutorial.com/postgresql-plpgsql/postgresql-create-procedure/ 3/4
2023/7/14 下午5:25 PostgreSQL CREATE PROCEDURE By Examples
commit;
end;$$
call stored_procedure_name(argument_list);
For example, this statement invokes the transfer stored procedure to transfer $1,000 from
Bob’s account to Alice’s account.
call transfer(1,2,1000);
The following statement verifies the data in the accounts table after the transfer:
It worked as expected.
Summary
https://www.postgresqltutorial.com/postgresql-plpgsql/postgresql-create-procedure/ 4/4