PL/SQL blocks and variables

PL/SQL blocks are groups of one or more SQL or PL/SQL statements that are enclosed within a BEGIN and END block. PL/SQL variables are used to store values that can be referenced and manipulated within a PL/SQL block.

Here is an example of a simple PL/SQL block that declares a variable and prints its value:

BEGIN
  DECLARE
    name VARCHAR2(20) := 'John';
  BEGIN
    DBMS_OUTPUT.PUT_LINE('Hello, ' || name || '!');
  END;
END;

In this example, the PL/SQL block starts with the BEGIN keyword and ends with the END keyword. Within the block, a variable named "name" is declared and assigned the value 'John'. The DBMS_OUTPUT.PUT_LINE statement is used to print a message to the console, which includes the value of the "name" variable.

Another example that illustrates the use of variables in PL/SQL blocks is the following:

DECLARE
  num1 NUMBER := 10;
  num2 NUMBER := 20;
  result NUMBER;
BEGIN
  result := num1 + num2;
  DBMS_OUTPUT.PUT_LINE('The sum of ' || num1 || ' and ' || num2 || ' is ' || result);
END;

In this example, two variables named "num1" and "num2" are declared and assigned the values 10 and 20, respectively. Another variable named "result" is declared to store the sum of "num1" and "num2". The result is then printed to the console using the DBMS_OUTPUT.PUT_LINE statement.

PL/SQL variables can be of different data types such as VARCHAR2, NUMBER, DATE, BOOLEAN, and others. Variables can also be initialized with default values, and they can be passed as parameters to stored procedures and functions.