Summary: in this tutorial, you will learn about PL/SQL variables and how to use them effectively.
In PL/SQL, a variable is named storage location that stores a value of a particular data type. The value of the variable changes through the program. Before using a variable, you must declare it in the declaration section of a block.
Declaring variables #
The syntax for a variable declaration is as follows:
variable_name datatype [NOT NULL] [:= initial_value];
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)
In this syntax:
- First, specify the name of the variable. The name of the variable should be as descriptive as possible, such as
l_total_sales
,l_credit_limit
, andl_sales_revenue
. - Second, choose an appropriate data type for the variable. The data type depends on the kind of value that you want the variable to store, for example, number, character, Boolean, and datetime.
By convention, local variable names should start with l_
and global variable names should have a prefix of g_
.
The following example declares three variables l_total_sales
, l_credit_limit
, and l_contact_name
:
DECLARE
l_total_sales NUMBER(15,2);
l_credit_limit NUMBER (10,0);
l_contact_name VARCHAR2(255);
BEGIN
NULL;
END;
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)
Default values #
PL/SQL allows you to set a default value for a variable at the declaration time. To assign a default value to a variable, you use the assignment operator (:=
) or the DEFAULT
keyword.
The following example declares a variable named l_product_name
with an initial value 'Laptop'
:
DECLARE
l_product_name VARCHAR2( 100 ) := 'Laptop';
BEGIN
NULL;
END;
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)
It is equivalent to the following block:
DECLARE
l_product_name VARCHAR2(100) DEFAULT 'Laptop';
BEGIN
NULL;
END;
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)
In this example, instead of using the assignment operator :=
, we used the DEFAULT
keyword to initialize a variable.
NOT NULL constraint #
If you impose the NOT NULL
constraint on a value, then the variable cannot accept NULL
. Additionally, a variable declared with the NOT NULL
must be initialized with a non-null value.
Note that PL/SQL treats an empty string ''
as NULL
.
For example:
DECLARE
l_shipping_status VARCHAR2( 25 ) NOT NULL := 'Shipped';
BEGIN
l_shipping_status := '';
END;
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)
How it works:
- First, declare a variable named
l_shipping_status
with theNOT NULL
constraint. - Then, assign a zero-length string to the variable.
PL/SQL issued the following error:
ORA-06502: PL/SQL: value or conversion error
Code language: plaintext (plaintext)
Since the variable l_shipping_status
declared with the NOT NULL
constraint, it could not accept a NULL
value or zero-length string in this case.
Variable assignments #
To assign a value to a variable, you use the assignment operator (:=
), for example:
DECLARE
l_customer_group VARCHAR2(100) := 'Silver';
BEGIN
l_customer_group := 'Gold';
DBMS_OUTPUT.PUT_LINE(l_customer_group);
END;
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)
To display the message on the script output, you should execute the SET SERVEROUPUT ON
command for your session.
You can assign a value of a variable to another as shown in the following example:
DECLARE
l_business_parter VARCHAR2(100) := 'Distributor';
l_lead_for VARCHAR2(100);
BEGIN
l_lead_for := l_business_parter;
DBMS_OUTPUT.PUT_LINE(l_lead_for);
END;
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)
Additionally, you can select a value from a table and assign it to a variable using the SELECT INTO statement. For example:
DECLARE
l_order_count INT;
BEGIN
SELECT COUNT(*) INTO l_order_count FROM orders;
DBMS_OUTPUT.PUT_LINE(l_order_count);
END;
Code language: CSS (css)
Output:
105
How it works:
First, declare a variable l_order_count
with type INT
:
l_order_count INT;
Second, count the orders in the orders
table from the sample database using the COUNT()
aggregate function and assign its result to the l_order_count
variable:
SELECT COUNT(*) INTO l_order_count FROM orders;
Third, display value of the l_order_count
variable:
DBMS_OUTPUT.PUT_LINE(l_order_count);
Code language: CSS (css)
Anchored declarations #
Typically, you declare a variable and select a value from a table column for this variable. If the data type of the table column changes, you must adjust the program to make it work with the new type.
PL/SQL allows you to declare a variable whose data type anchor to a table column or another variable. Consider the following example:
DECLARE
l_customer_name customers.name%TYPE;
l_credit_limit customers.credit_limit%TYPE;
BEGIN
SELECT
name, credit_limit
INTO
l_customer_name, l_credit_limit
FROM
customers
WHERE
customer_id = 38;
DBMS_OUTPUT.PUT_LINE(l_customer_name || ':' || l_credit_limit );
END;
/
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)
In this example:
- First, declare two variables
l_customer_name
andl_credit_limit
whose data type anchors to thename
andcredit_limit
columns respectively, in the declaration section of the block. - Second, query the customer name and credit limit of the customer id
38
and assign these column values to thel_customer_name
andl_credit_limit
variables in the execution block. - Third, display the customer name and credit limit.
PL/SQL returned the following output:
Kraft Heinz:500
Code language: SQL (Structured Query Language) (sql)
This example illustrates how to declare variables that anchor to another variable:
DECLARE
l_credit_limit customers.credit_limit%TYPE;
l_average_credit l_credit_limit%TYPE;
l_max_credit l_credit_limit%TYPE;
l_min_credit l_credit_limit%TYPE;
BEGIN
SELECT
MIN(credit_limit),
MAX(credit_limit),
AVG(credit_limit)
INTO
l_min_credit,
l_max_credit,
l_average_credit
FROM customers;
SELECT
credit_limit
INTO
l_credit_limit
FROM
customers
WHERE
customer_id = 100;
dbms_output.put_line('Min Credit: ' || l_min_credit);
dbms_output.put_line('Max Credit: ' || l_max_credit);
dbms_output.put_line('Avg Credit: ' || l_average_credit);
dbms_output.put_line('Customer Credit: ' || l_credit_limit);
END;
Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)
Output:
Min Credit: 100
Max Credit: 5000
Avg Credit: 1894.67
Customer Credit: 1894.67
Code language: plaintext (plaintext)
Summary #
- Use a variable to store a value of a specific type.