Database Reference
In-Depth Information
Using statements
You use statements in the PL/pgSQL code whenever you are assigning a value to a
variable, calling a function, or using conditions such as
IF
/
ELSE
.
The order of the execution of statements is controlled by the organization of
statements. Within the space of the
BEGIN
and
END
blocks, the main chunk of
statements are placed along with a few declarative statements in the
DECLARE
block
as well.
Remember that statements ends with a semicolon.
The assignment statement
The assignment statement is the most common statement you will use in code.
Assignment means assigning a value to a variable. Its syntax is as follows:
target := expression;
Here,
target
can be anything; it can be a variable, a column, a function parameter,
or a row but not a constant. During execution,
expression
is irst evaluated to a
value when PL/pgSQL executes an assignment statement. It starts by evaluating
expression
and assigning the value to
target
. If the type for value and
target
mismatches, PostgreSQL converts the type accordingly or generates an error if the
conversion is not possible.
You have already observed the examples of assignment statements; let's recall them:
•
SELECT
INTO
usage in the
getRecords()
function:
SELECT
COUNT(*)
INTO
TOTAL FROM warehouse_tbl;
• Default value assignment in the
func_param()
function:
plus := a + b;
The call/return function
All PostgreSQL functions return a value, and to call a function is to simply run
a
SELECT
statement query or an assignment statement. The following are some
of the examples:
• The
SELECT
statement in the
function_identifier()
function:
SELECT function_identifier(arguments);