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.

Similar Reads

How to Declare Variables in PL/SQL?

When writing PL/SQL code it is important to declare variables properly to store and manipulate data effectively. Variables act as containers for values and enable various operations on the stored data....

1. Using Declare Variables in PL/SQL

Syntax:...

2. Using Initializing Variables in PL/SQL

In this method Variables can be initialized in two ways either during declaration or later in the code....

3. Using Variable Scope in PL/SQL

In PL/SQL variable scope can be a local or global. Declaring variable within a block or subprogram are only accessible within that block or subprogram and Declaring variable in the outermost block are accessible to nested blocks and subprograms....

4. Using Variable Attributes

PL/SQL provides %TYPE and %ROWTYPE attribute:...

Conclusion

Overall, After reading Whole article now you have good understanding about declare variable in PL/SQL. We have seen various methods like Using Declare Variables in PL/SQL, Using Initializing Variables in PL/SQL, Using Variable Scope in PL/SQL and Using Variable Attributes. Now you can easily dclare variable with the help of above methods easily....

Contact Us