PL/pgSQL Select Into
Summary: in this tutorial, you will learn how to use the PL/pgSQL select into
statement to select data from the database and assign it to a variable.
Introduction to PL/pgSQL Select Into statement
The select into
statement allows you to select data from the database and assign it to a variable.
Here’s the basic syntax of the select into
statement:
In this syntax,
- First, specify one or more columns from which you want to retrieve data in the
select
clause. - Second, place one or more variables after the
into
keyword. - Third, provide the name of the table in the
from
clause.
The select
into
statement will assign the data returned by the select
clause to the corresponding variables.
Besides selecting data from a table, you can use other clauses of the select
statement such as join
, group by,
and having
.
PL/pgSQL Select Into statement examples
Let’s take some examples of using the select into
statement.
1) Basic select into statement example
The following example uses the select into
statement to retrieve the number of actors from the actor
table and assign it to the actor_count
variable:
Output:
In this example:
- First, declare a variable called
actor_count
that stores the number of actors from theactor
table. - Second, assign the number of actors to the
actor_count
using theselect into
statement. - Third, display a message that shows the value of the
actor_count
variable using theraise notice
statement.
2) Using the select into with multiple variables
The following example uses the select into
statement to assign the first and last names of the actor id 1 to two variables:
Output:
How it works.
First, declare two variables v_first_name
and v_last_name
with the types varchar
:
Second, retrieve the first_name
and last_name
of the actor id 1 from the actor
table and assign them to the v_first_name
and v_last_name
variables:
Third, show the values of v_first_name
and v_last_name
variables:
Because we assign data retrieved from the first_name
and last_name
columns of the actor
table, we can use the type-copying technique to declare the v_first_name
and v_last_name
variables:
Summary
- Use the
select into
statement to select data from the database and assign it to a variable.