PL SQL Introduction
PL SQL Introduction
Disadvantages of SQL:
Features of PL/SQL:
SQL PL/SQL
DECLARE
declaration statements;
BEGIN
executable statements
EXCEPTIONS
exception handling statements
END;
PL/SQL identifiers
Variables:
Like several other programming languages, variables in PL/SQL must be
declared prior to its use. They should have a valid name and data type as well.
Syntax for declaration of variables:
variable_name datatype [NOT NULL := value ];
1.
Example to show how to declare variables in PL/SQL :
SQL> SET SERVEROUTPUT
ON;
SQL> DECLARE
var1 INTEGER;
var2 REAL;
var3 varchar2(20)
;
BEGIN
null;
END;
/
Output:
PL/SQL procedure successfully completed.
2.
Explanation:
○ SET SERVEROUTPUT ON: It is used to display the buffer
used by the dbms_output.
○ var1 INTEGER : It is the declaration of variable, named var1
which is of integer type. There are many other data types that
can be used like float, int, real, smallint, long etc. It also
supports variables used in SQL as well like NUMBER(prec,
scale), varchar, varchar2 etc.
○ PL/SQL procedure successfully completed.: It is displayed
when the code is compiled and executed successfully.
○ Slash (/) after END;: The slash (/) tells the SQL*Plus to
execute the block.
3.
1.1) INITIALISING VARIABLES:
The variables can also be initialised just like in other programming
languages. Let us see an example for the same:
BEGIN
null;
END;
/
Output:
PL/SQL procedure successfully completed.
4.
Explanation:
○ Assignment operator (:=) : It is used to assign a value to a
variable.
5. Displaying Output:
The outputs are displayed by using DBMS_OUTPUT which is a built-in
package that enables the user to display output, debugging information,
and send messages from PL/SQL blocks, subprograms, packages, and
triggers.
Let us see an example to see how to display a message using PL/SQL :
BEGIN
dbms_output.put_line(var);
END;
/
Output:
I love GeeksForGeeks
6.
Explanation:
-- I am a comment, so i will be
ignored.
var varchar2(40) := 'I love
GeeksForGeeks' ;
BEGIN
dbms_output.put_line(var);
END;
/
Output:
I love GeeksForGeeks
9.
SQL> DECLARE
BEGIN
null;
END;
/
Output:
Enter value for a: 24
old 2: a number := &a;
new 2: a number := 24;
Enter value for b: 'GeeksForGeeks'
old 3: b varchar2(30) := &b;
new 3: b varchar2(30) := 'GeeksForGeeks';
11.
SQL> DECLARE
BEGIN
c := a + b ;
dbms_output.put_line('Sum of '||a||' and '||b||'
is = '||c);
END;
/
Enter value for a: 2
Enter value for b: 3
Sum of 2 and 3 is = 5
PL/SQL Variables
A variable is a meaningful name which facilitates a programmer to store data
temporarily during the execution of code. It helps you to manipulate data in PL/SQL
programs. It is nothing except a name given to a storage area. Each variable in the
PL/SQL has a specific data type which defines the size and layout of the variable's
memory.
A variable should not exceed 30 characters. Its letter optionally followed by more letters,
dollar signs, numerals, underscore etc.
1. It needs to declare the variable first in the declaration section of a PL/SQL block before
using it.
2. By default, variable names are not case sensitive. A reserved PL/SQL keyword cannot be
used as a variable name.
Here, variable_name is a valid identifier in PL/SQL and datatype must be valid PL/SQL
data type. A data type with size, scale or precision limit is called a constrained
declaration. The constrained declaration needs less memory than unconstrained
declaration.
HTML Tutorial
● The name of the variable must begin with ASCII letter. The PL/SQL is not case
sensitive so it could be either lowercase or uppercase. For example: v_data and
V_DATA refer to the same variables.
● You should make your variable easy to read and understand, after the first
character, it may be any number, underscore (_) or dollar sign ($).
You can also specify NOT NULL constraint to avoid NULL value. If you specify the NOT
NULL constraint, you must assign an initial value for that variable.
You must have a good programming skill to initialize variable properly otherwise,
sometimes program would produce unexpected result.
1. DECLARE
2. a integer := 30;
3. b integer := 40;
4. c integer;
5. f real;
6. BEGIN
7. c := a + b;
8. dbms_output.put_line('Value of c: ' || c);
9. f := 100.0/3.0;
10. dbms_output.put_line('Value of f: ' || f);
11.END;
1. Value of c: 70
2. Value of f: 33.333333333333333333
3.
4. PL/SQL procedure successfully completed.
● Local Variable: Local variables are the inner block variables which are not
accessible to outer blocks.
1. DECLARE
2. -- Global variables
3. num1 number := 95;
4. num2 number := 85;
5. BEGIN
6. dbms_output.put_line('Outer Variable num1: ' || num1);
7. dbms_output.put_line('Outer Variable num2: ' || num2);
8. DECLARE
9. -- Local variables
10. num1 number := 195;
11. num2 number := 185;
12. BEGIN
13. dbms_output.put_line('Inner Variable num1: ' || num1);
14. dbms_output.put_line('Inner Variable num2: ' || num2);
15. END;
16.END;
17./
PL/SQL Constants
A constant is a value used in a PL/SQL block that remains unchanged throughout the
program. It is a user-defined literal value. It can be declared and used instead of actual
values.
Suppose, you have to write a program which will increase the salary of the employees
upto 30%, you can declare a constant and use it throughout the program. Next time if
you want to increase the salary again you can change the value of constant than the
actual value throughout the program.
● Constant_name:it is the name of constant just like variable name. The constant
word is a reserved word and its value does not change.
1. DECLARE
2. -- constant declaration
3. pi constant number := 3.141592654;
4. -- other declarations
5. radius number(5,2);
6. dia number(5,2);
7. circumference number(7, 2);
8. area number (10, 2);
9. BEGIN
10. -- processing
11. radius := 9.5;
12. dia := radius * 2;
13. circumference := 2.0 * pi * radius;
14. area := pi * radius * radius;
15. -- output
16. dbms_output.put_line('Radius: ' || radius);
17. dbms_output.put_line('Diameter: ' || dia);
18. dbms_output.put_line('Circumference: ' || circumference);
19. dbms_output.put_line('Area: ' || area);
20.END;
21./
After the execution of the above code at SQL prompt, it will produce the following
result:.
● Numeric Literals
● Character Literals
● String Literals
● BOOLEAN Literals
Literals Examples
PL/SQL Constants
A constant is a value used in a PL/SQL block that remains unchanged throughout the
program. It is a user-defined literal value. It can be declared and used instead of actual
values.
Suppose, you have to write a program which will increase the salary of the employees
upto 30%, you can declare a constant and use it throughout the program. Next time if
you want to increase the salary again you can change the value of constant than the
actual value throughout the program.
1. DECLARE
2. -- constant declaration
3. pi constant number := 3.141592654;
4. -- other declarations
5. radius number(5,2);
6. dia number(5,2);
7. circumference number(7, 2);
8. area number (10, 2);
9. BEGIN
10. -- processing
11. radius := 9.5;
12. dia := radius * 2;
13. circumference := 2.0 * pi * radius;
14. area := pi * radius * radius;
15. -- output
16. dbms_output.put_line('Radius: ' || radius);
17. dbms_output.put_line('Diameter: ' || dia);
18. dbms_output.put_line('Circumference: ' || circumference);
19. dbms_output.put_line('Area: ' || area);
20.END;
21./
After the execution of the above code at SQL prompt, it will produce the following
result:.
PL/SQL Literals
Literals are the explicit numeric, character, string or boolean values which are not
represented by an identifier. For example: TRUE, NULL, etc. are all literals of type
boolean. PL/SQL literals are case-sensitive. There are following kinds of literals in
PL/SQL:
● Numeric Literals
● Character Literals
● String Literals
● BOOLEAN Literals
1. IF condition
2. THEN
3. Statement: {It is executed when condition is true}
4. END IF;
This syntax is used when you want to execute statements only when condition is TRUE.
1. IF condition
2. THEN
3. {...statements to execute when condition is TRUE...}
4. ELSE
5. {...statements to execute when condition is FALSE...}
6. END IF;
This syntax is used when you want to execute one set of statements when condition is
TRUE or a different set of statements when condition is FALSE.
This syntax is used when you want to execute one set of statements when condition1 is
TRUE or a different set of statements when condition2 is TRUE.
1. IF condition1
2. THEN
3. {...statements to execute when condition1 is TRUE...}
4. ELSIF condition2
5. THEN
6. {...statements to execute when condition2 is TRUE...}
7. ELSE
8. {...statements to execute when both condition1 and condition2 are FALSE...}
9. END IF;
It is the most advance syntax and used if you want to execute one set of
statements when condition1 is TRUE, a different set of statement when
condition2 is TRUE or a different set of statements when both the condition1 and
condition2 are FALSE.
When a condition is found to be TRUE, the IF-THEN-ELSE statement will execute the
corresponding code and not check the conditions any further.
If there no condition is met, the ELSE portion of the IF-THEN-ELSE statement will be
executed.
1. DECLARE
2. a number(3) := 500;
3. BEGIN
4. -- check the boolean condition using if statement
5. IF( a < 20 ) THEN
6. -- if condition is true then print the following
7. dbms_output.put_line('a is less than 20 ' );
8. ELSE
9. dbms_output.put_line('a is not less than 20 ' );
10. END IF;
11. dbms_output.put_line('value of a is : ' || a);
12.END;
After the execution of the above code in SQL prompt, you will get the following result:
The CASE statement works like the IF statement, only using the keyword WHEN. A CASE
statement is evaluated from top to bottom. If it get the condition TRUE, then the
corresponding THEN calause is executed and the execution goes to the END CASE
clause.
1. DECLARE
2. grade char(1) := 'A';
3. BEGIN
4. CASE grade
5. when 'A' then dbms_output.put_line('Excellent');
6. when 'B' then dbms_output.put_line('Very good');
7. when 'C' then dbms_output.put_line('Good');
8. when 'D' then dbms_output.put_line('Average');
9. when 'F' then dbms_output.put_line('Passed with Grace');
10. else dbms_output.put_line('Failed');
11. END CASE;
12.END;
After the execution of above code, you will get the following result:
Excellent
PL/SQL Loop
The PL/SQL loops are used to repeat the execution of one or more statements for
specified number of times. These are also known as iterative control statements.
1. LOOP
2. Sequence of statements;
3. END LOOP;
1.LOOP
2. Sequence of statements;
3.END LOOP;
1.LOOP
2. statements;
3. EXIT;
4. {or EXIT WHEN condition;}
5.END LOOP;
1.DECLARE
2.i NUMBER := 1;
3.BEGIN
4.LOOP
5.EXIT WHEN i>10;
6.DBMS_OUTPUT.PUT_LINE(i);
7.i := i+1;
8.END LOOP;
9.END;
After the execution of the above code, you will get the
following result:
7
8
10
Note: You must follow these steps while using PL/SQL Exit Loop.
● You should use EXIT WHEN statement to exit from the Loop. Otherwise the EXIT
statement without WHEN condition, the statements in the Loop is executed only
once.
Output:
100
200
300
400
500
600
700
800
900
1000
1. DECLARE
2. i INTEGER := 1;
3. BEGIN
4. WHILE i <= 10 LOOP
5. DBMS_OUTPUT.PUT_LINE(i);
6. i := i+1;
7. END LOOP;
8. END;
After the execution of the above code, you will get the following result:
1
2
10
Note: You must follow these steps while using PL/SQL WHILE Loop.
Output:
200
400
600
800
1000
1200
1400
1600
1800
2000
1. BEGIN
2. FOR k IN 1..10 LOOP
3. -- note that k was not declared
4. DBMS_OUTPUT.PUT_LINE(k);
5. END LOOP;
6. END;
After the execution of the above code, you will get the following result:
7
8
10
Note: You must follow these steps while using PL/SQL WHILE Loop.
● You don't need to declare the counter variable explicitly because it is declared
implicitly in the declaration section.
● You can use EXIT WHEN statements and EXIT statements in FOR Loops but it is
not done often.
10
20
30
40
50
60
70
80
90
100
PL/SQL For Loop REVERSE Example 3
Let's see an example of PL/SQL for loop where we are using REVERSE keyword.
1. DECLARE
2. VAR1 NUMBER;
3. BEGIN
4. VAR1:=10;
5. FOR VAR2 IN REVERSE 1..10
6. LOOP
7. DBMS_OUTPUT.PUT_LINE (VAR1*VAR2);
8. END LOOP;
9. END;
Output:
100
90
80
70
60
50
40
30
20
10
The continue statement is not a keyword in Oracle 10g. It is a new feature encorporated
in oracle 11g.
For example: If a continue statement exits a cursor FOR LOOP prematurely then it exits
an inner loop and transfer control to the next iteration of an outer loop, the cursor closes
(in this context, CONTINUE works like GOTO).
Syntax:
1. continue;
1. DECLARE
2. x NUMBER := 0;
3. BEGIN
4. LOOP -- After CONTINUE statement, control resumes here
5. DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' || TO_CHAR(x));
6. x := x + 1;
7. IF x < 3 THEN
8. CONTINUE;
9. END IF;
10. DBMS_OUTPUT.PUT_LINE
11. ('Inside loop, after CONTINUE: x = ' || TO_CHAR(x));
12. EXIT WHEN x = 5;
13. END LOOP;
14.
15. DBMS_OUTPUT.PUT_LINE (' After loop: x = ' || TO_CHAR(x));
16.END;
17./
After the execution of above code, you will get the following result:
Inside loop: x = 0
Inside loop: x = 1
Inside loop: x = 2
Inside loop: x = 3
Inside loop, after CONTINUE: x = 4
Inside loop: x = 4
After loop: x = 5
Here the label declaration which contains the label_name encapsulated within the << >>
symbol and must be followed by at least one statement to execute.
Syntax:
Here the label declaration which contains the label_name encapsulated within the << >>
symbol and must be followed by at least one statement to execute.
1. DECLARE
2. a number(2) := 30;
3. BEGIN
4. <<loopstart>>
5. -- while loop execution
6. WHILE a < 50 LOOP
7. dbms_output.put_line ('value of a: ' || a);
8. a := a + 1;
9. IF a = 35 THEN
10. a := a + 1;
11. GOTO loopstart;
12. END IF;
13. END LOOP;
14.END;
15./
After the execution of above code, you will get the following result:
value of a: 30
value of a: 31
value of a: 32
value of a: 33
value of a: 34
value of a: 36
value of a: 37
value of a: 38
value of a: 39
value of a: 40
value of a: 41
value of a: 42
value of a: 43
value of a: 44
value of a: 45
value of a: 46
value of a: 47
value of a: 48
value of a: 49
Statement processed.
● Cannot transfer control from one IF statement clause to another or from one
CASE statement WHEN clause to another.