How to use Initializing Variables in PL/SQL In SQL
In this method Variables can be initialized in two ways either during declaration or later in the code.
Initializing during declaration
Syntax:
DECLARE
my_variable NUMBER := value;
BEGIN
-- PL/SQL code
END;
Example:
DECLARE
name VARCHAR2(20) := 'w3wiki';
BEGIN
DBMS_OUTPUT.PUT_LINE(name);
END;
Output:
We can also Initialize later in the code.
Syntax:
DECLARE
my_variable NUMBER;
BEGIN
my_variable := value;
END;
Example:
DECLARE
num1 NUMBER;
num2 NUMBER;
result NUMBER;
BEGIN
num1 := 5;
num2 := 3;
result := num1 + num2;
DBMS_OUTPUT.PUT_LINE('Sum: ' || result);
END;
Output:
How to Declare a Variable in PL/SQL?
Declaring variables in PL/SQL is a fundamental step towards building powerful and efficient database applications. Variables act as placeholders for data which enable us to manipulate and store information within our PL/SQL programs.
Understanding how to declare variables is essential for writing effective and scalable code. In this article, we will learn about how to declare a variable with the help of various methods along with the syntax and examples.
Contact Us