PL/pgSQL Variables
Summary: in this tutorial, you will learn about PL/pgSQL variables and various ways to declare them
Introduction to PL/pgSQL variables
In PL/pgSQL, variables are placeholders for storing data within a block. These variables can hold values of various types such as integers, booleans, text, and more.
Variables allow you to hold values for calculations, store query results, and so on.
Before using variables, you must declare them in the declaration section of a block.
Variables are scoped to the block in which they’re declared. It means that variables are accessible only within the block and any nested blocks.
The following illustrates the syntax of declaring a variable.
In this syntax:
- First, specify the name of the variable. It is a good practice to assign a meaningful name to a variable. For example, instead of naming a variable
i
you should useindex
orcounter
. - Second, associate a specific data type with the variable. The data type can be any valid data type such as integer, numeric, varchar, and char.
- Third, optionally assign a default value to the variable. If you don’t do so, the initial value of the variable is
NULL
.
Please note that you can use either :=
or =
assignment operator to set an initial value for a variable.
To assign a value to a variable, you can use the assignment operator (=) as follows:
Alternatively, you can use the :=
assignment operator:
Basic PL/pgSQL variable example
1) Declare variables
The following example shows how to declare and initialize variables:
Output:
How it works.
First, declare four variables in the declaration part of the block:
- The
counter
variable is an integer with an initial value of 1. - The
first_name
andlast_name
arevarchar(50)
with the initial values of'John'
and'Doe'
respectively. - The
payment
variable has the numeric type with the initial value20.5
.
Second, display the values of the variables using the raise notice
statement.
2) Assign values to variables
The following example shows how to assign a value to a variable:
Output:
How it works.
First, declare a variable in the declaration block:
Second, split a string literal into two parts using a space, return the first part, and assign it to the first_name
variable:
Third, display the value of the first_name
variable using the raise notice
statement:
Variable initialization timing
PostgreSQL evaluates the initial value of a variable and assigns it when the block is entered. For example:
Here is the output:
In this example:
- First, declare a variable
created_at
and initialize its value to the current time. - Second, display the variable.
- Third, pause the execution for 3 seconds using the
pg_sleep()
function. - Finally, display the value of the
created_at
variable again.
The output indicates that the value of the created_at
variable is only initialized once when the block is entered.
Copying data types
The %type
provides the data type of a table column or another variable. Typically, you use the %type
to declare a variable that holds a value from the database or another variable.
The following illustrates how to declare a variable with the data type of a table column:
The following shows how to declare a variable with the data type of another variable:
We’ll use the following film
table from the sample database:
This example uses the type-copying technique to declare variables that hold values that come from the film
table:
Output:
In this example, we declare two variables:
- The
film_title
variable has the same data type as thetitle
column in thefilm
table from the sample database. - The
featured_title
has the same data type as the data type of thefilm_title
variable.
We use the select into statement to retrieve from the film_title
column of the film
table and assign it to the film_title
variable.
Using the type-copying feature offers the following advantages:
- First, you don’t need to know the type of column or reference being accessed.
- Second, if the data type of the referenced column name (or variable) changes, you don’t need to change the block.
Variables in blocks and subblocks
When you declare a variable in a subblock with the same name as another variable in the outer block, the variable in the outer block is hidden within the subblock.
To access a variable in the outer block, you use the block label to qualify its name, as shown in the following example:
In this example:
- First, declare a variable named
counter
in theouter_block
. - Next, declare a variable with the same name in the subblock.
- Then, before entering into the subblock, the value of
counter
is one. Within the subblock, we increase the value of thecounter
variable to ten and print it out. Note that this change only affects thecounter
variable within the subblock. - After that, reference the
counter
variable in the outer block using the block labelouter_block.counter
. - Finally, display the value of the
counter
variable in the outer block, its value remains unchanged.
Summary
- A variable is a named storage location with a data type that can hold a value.
- PostgreSQL evaluates the default value of a variable and assigns it to the variable when it enters the block.
- Declare variables and optionally an initial value to it in the declaration section of the block.