PL/pgSQL IF Statement
Summary: in this tutorial, you will learn how to use the PL/pgSQL if
statements to execute a command based on a specific condition.
Introduction to PL/pgSQL IF Statement
The if
statement allows you to execute one or more statements based on a condition. PL/pgSQL provides you with three forms of the if
statements:
if then
if then else
if then elsif
1) PL/pgSQL if-then statement
The following illustrates the simplest form of the if
statement:
The if
statement executes statements
when a condition
is true. If the condition
evaluates to false
, the control is passed to the next statement after the end if
.
The condition
is a boolean expression that evaluates to true
or false
.
The statements
can be one or more statements that you want to execute when the condition
is true. It may contain other if
statements.
When you place an if
statement is within another if
statement, you’ll have a nested-if statement.
The following flowchart illustrates the simple if
statement.
The following example uses an if
statement to check if a query returns any rows:
Output:
In this example, we select a film by a specific film id (0
).
The found
is a global variable that is available in PL/pgSQL. The select into
statement sets the found
variable if a row is assigned or false
if no row is returned.
We use the if
statement to check if the film with id (0) exists and raise a notice if it does not.
If you change the value of the input_film_id
variable to some value that exists in the film table like 100, you will not see any message.
2) PL/pgSQL if-then-else statement
The if...then...else
statement executes the statements in the if
branch if the condition
evaluates to true; otherwise, it executes the statements in the else
branch.
Here’s the syntax of the if...then...else
statement:
The following flowchart illustrates the if else
statement.
The following example uses an if…then…else statement to display a message showing that a film with a specific id exists or not:
Output:
In this example, because the film id 100 exists in the film table the found
variable is true. Therefore, the statement in the else
branch is executed.
3) PL/pgSQL if-then-elsif Statement
Unlike the if
and if...then...else
statements that evaluate only one condition, the if then elsif
statement allows you to evaluate multiple conditions. and execute one or more statements when a condition is true.
Here’s the syntax of the if...then...elsif
statement:
In this syntax, if the condition_1
is true
then the if...then...elsif
statement executes the statement_1
and stops evaluating the other conditions such as condition_2
, condition_3
, and so on.
If all conditions are evaluated to false
, the if...then...elsif
executes the statements in the else
branch.
The following flowchart illustrates the if then elsif
statement:
Let’s look at the following example:
Output:
How it works:
- First, select the film with id 100. If the film does not exist, raise a notice that the film is not found.
- Second, use the
if...then....elsif
statement to assign a description to a film based on the length of the film.
Summary
- Use the
if..then
statement to execute one or more statements when a condition istrue
. - Use the
if...then...else
statement to execute statements when a condition istrue
and execute other statements when the condition isfalse
. - Use the
if...then...elsif
statement to evaluate multiple conditions and execute statements when the corresponding condition istrue
.