Unit Iii
Unit Iii
2. High Productivity
4. Full Portability
5. Tight Security
Disadvantages of PL/SQL
1. Stored Procedures in PL/SQL uses high memory
SQL PL/SQL
01 SQL is a single query that PL/SQL is a block of codes that
is used to perform DML used to write the entire program
and DDL operations. blocks/ procedure/ function
02 It is declarative, that PL/SQL is procedural that defines
defines what need to be how the things needs to be done.
done, rather than how
things need to be done
03 Execute as a single Execute as a whole block.
statement.
04 Mainly used to manipulate Mainly used to create an
data. application.
05 Interaction with a No interaction with the database
Database server. server.
06 Cannot contain PL/SQL It is an extension of SQL, so that
code in it. it can contain SQL inside it.
OR
SQL PL/SQL
01 SQL can execute a single operation PL/SQL can perform multiple operation at
at a time. a time.
04 SQL directly interacts with PL/SQL does not directly interacts with
database server. database server.
DECLARE
Declaration statements;
BEGIN
Execution statements;
EXCEPTION—Optional
Exception handling statements; Optional
END;
/
DECLRE
Declarations
This section starts with the keyword DECLARE. It is an optional section and defines all
variables, cursors, subprograms, and other elements to be used in the program.
BEGIN
Executable Commands
This section is enclosed between the keywords BEGIN and END and it is a
mandatory section. It consists of the executable PL/SQL statements of the
program. It should have at least one executable line of code, which may be just a
NULL command to indicate that nothing should be executed
Exception Handling
This section starts with the keyword EXCEPTION. This optional section contains
exception(s) that handle errors in the program.
END
Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within
other PL/SQL blocks using BEGIN and END.
Example
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
PL/SQL If
PL/SQL supports the programming language features like conditional statements and
iterative statements. Its programming constructs are similar to how you use in
programming languages like Java and C++.
Syntax for IF Statement:
There are different syntaxes for the IF-THEN-ELSE statement.
This syntax is used when you want to execute statements only when condition is TRUE.
Syntax: (IF-THEN-ELSE statement):
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.
Example 1 : In this example, we are going to print message whether the given number
is odd or even.
DECLARE
a NUMBER:=11;
BEGIN
dbms_output.put_line (‘Program started');
IF( mod(a,2)=0) THEN
dbms_output.put_line('a is even number' );
ELSE
dbms_output.put_line('a is odd number1);
END IF;
dbms_output.put_line (‘Program completed.’);
END;
/
Example 2
Example of PL/SQL If Statement
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:
a is not less than 20
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.
Syntax for a basic loop:
1. LOOP
2. Sequence of statements;
3. END LOOP;
2. While Loop
3. For Loop
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;
OUTPUT
1
2
3
4
5
6
7
8
9
10
After the execution of the above code, you will get the following result:
OUTPUT-
1
2
3
4
5
6
7
8
9
10
OUTPUT-
10
9
8
7
6
5
4
3
2
1