Sie sind auf Seite 1von 9

UNIT II (PL/SQL)

PL/SQL is the procedural extension to SQL with design features of programming


languages. Data manipulation and query statements of SQL are included within
procedural units of code.
Benefits of PL/SQL:
PL/SQL can improve the performance of an application. The benefits differ depending
on the execution environment:

PL/SQL can be used to group SQL statements together within a single block and
to send the entire block to the server in a single call thereby reducing
networking traffic. Without PL/SQL, the SQL statements are sent to the Oracle
server one at a time. Each SQL statement results in another call to the Oracle
server and higher performance overhead. In a network environment, the
overhead can become significant.
PL/SQL can also operate with Oracle server application development tools such
as Oracle forms and Oracle reports.

PL/SQL Block Structure:


Every unit of PL/SQL comprises one or more blocks. These blocks can be entirely
separate or nested one within another. The basic units (procedures, functions, and
anonymous blocks) that make up a PL/SQL program are logical blocks which can
contain any number of nested sub-blocks. Therefore one block can represent a small
part of another block, which in turn can be part of the whole unit of code.
Modularized program development:
Group logically related statements within blocks.
Nest sub-blocks inside larger blocks to build powerful programs.
Break down a complex problem into a set of manageable well defined logical
modules and implement the modules with blocks.
Place reusable PL/SQL code in libraries to be shared between Oracle forms and
Oracle reports, applications, or store it in a Oracle server to make it accessible
to any application that can interact with an Oracle database.
Identifiers/Variables:
In PL/SQL, you can use identifiers to do the following:
Declare variables, cursors, constants and exceptions and then use them in SQL
and procedural statements.
Declare variables belonging to scalar, reference, composite and large object
(LOB) data types.

Declare variables dynamically based on the data structure of tables and


columns in the database.

Procedural Language Control Structures:


Execute a sequence of statements conditionally.
Execute a sequence of statements iteratively in a loop.
Process individually the rows returned by a multiple row query with a cursor.
Errors:
Process Oracle server error with exception-handling routines.
Declare user-defined error conditions and process them with exceptionhandling routines.
PL/SQL Block Syntax:
DECLARE [Optional]
Variables, cursors, user defined exceptions
BEGIN [Mandatory]
- SQL Statements
- PL/SQL Statements
Exception [Optional]
-- Actions to be performed when errors occur ---END [Mandatory];
Example:
DECLARE
v_variable VARCHAR2(5);
BEGIN
SELECT column_name
INTO v_variable
FROM table_name;
EXCEPTION
WHEN exception_name THEN
END;
Anonymous, procedure and function blocks:
Anonymous:
- No name.
- Starts with DECLARE statement.
Procedure:
- No return.
- PROCEDURE name IS

Function:
- Returns a value
- FUNCTION name RETURN data-type IS
Programming Constructs:
- Declaring Variables:
o Identifier [CONSTANT] data-type [NOT NULL]
[:= | DEFAULT expr];
-

Assignment:
o Identifier := expr;

IF Statement:
o IF condition THEN
Statements;
[ELSE IF condition THEN
Statements;]
[ELSE
Statements;]
END IF;

CASE Statement:
o CASE selector
WHEN expression1 THEN result1
WHEN expression2 THEN result2
.
.
.
WHEN expression THEN resultn
[ELSE resultn1;]
END;
BASIC Loops:
o LOOP
Statement 1;
.
.
.
EXIT [WHEN condition];
END LOOP;

WHILE Statement:
o WHILE condition LOOP
Statement1;
.
.

.
END LOOP;
-

FOR Statement:
o FOR counter IN [REVERSE]
Lower_bound..upper_bound LOOP
Statement1;
.
.
.
END LOOP;

Packages:
Packages are collections of variables, user defined data types, procedures and
functions which can either be either public or private. Packages are used to group
related things into one block that get loads into memory in one go. A package cannot
be invoked directly.

Experiment 20:
Problem Statement:
Write a PL/SQL block which declares a variable and reads the last name of employee
with id = 5 and outputs that to standard output.
Solution:
1. Declare a varchar variable
2. Write the select into clause inside PL/SQL Begin END block.
Experiment 21:
Problem Statement:
Write a PL/SQL block which declares a variable with a value and prints in all capitals
if the value starts with S, in all smalls if it starts with R, and in initial capitals if
otherwise.
Solution:
1. Use IF or CASE.
Experiment 22:
Problem Statement:
Write a PL/SQL block which declares two variables with values and prints first value if
it is not null and prints second value if first is null.
Solution:
Use NULLIF or COALESCE.
Experiment 23:
Problem Statement:
Write a PL/SQL block which declares a variable and reads the last name of employees
and outputs that to standard output.
Solution:
3. Declare a varchar variable
4. Write a LOOP.

Experiment 24:
Problem Statement:
Write a PL/SQL block which updates 2 tables. If the second update fails the first
update also has to be reversed.
Solution:
1. Use BEGIN transaction, END transaction.
2. Check for success of a query.
3. Use ROLLBACK or COMMIT depending on query success.
Experiment 25:
Problem Statement:
Write a PL/SQL block which updates 3 tables. If the third update fails the second
update has to be reversed while first should not get effected.
Solution:
1. Use BEGIN transaction, END transaction.
2. Define SAVEPOINT at the end of first update.
3. Run 3rd update and check for success.
4. If 3rd fails, Use ROLLBACK to SAVEPOINT.
Experiment 26:
Problem Statement:
Write a PL/SQL block with a simple Exception Block.
Solution:
1. Refer to Exception block syntax.
Experiment 27:
Problem Statement:
Write a PL/SQL block which declares a variable and reads the last name of employee
with id = 5. If there is no employee with id = 5 an error should be thrown. Otherwise
the name has to be printed.
Solution:
1. Use built-in exceptions.

Experiment 28:
Problem Statement:
Write a PL/SQL block which declares a variable and reads the last name of employee
with id = 5. If the name has 8 characters, raise an exception called too many
characters and handle it by cutting the last_name to first eight characters.
Solution:
1. Use user defined exceptions.
Experiment 29:
Problem Statement:
Think of a problem that generates an exception is caught but an application error has
to be raised because the exception in fatal.
Solution:
1. As a part of exception handling, raise application error if the situation is not
recoverable.
Experiment 30:
Problem Statement:
Create a stored procedure which takes deparment_id as parameter, inserts all
employees of that department in a table called dept_employee with the same
structure.
Solution:
1. Learn how to use parameter in to stored procedure.
Experiment 31:
Problem Statement:
Create a function which takes deparment_id as parameter and returns the name of
the department.
Solution:
1. Learn how to use parameter in functions.
Experiment 32:
Problem Statement:

Write a package which implements a set of functions that will compute HRA, DA
based on rules given salary.
Solution:
1. Implement one stored function that will compute HRA given basic.
2. Implement one stored function that will compute DA given basic.
3. Put these in a package so it gets loaded at one time.

Unit II Additional Exercises


1. Write a PL/SQL procedure which takes department id as parameter and gives
all employees belong to the department.
2. Write a PL/SQL procedure which takes lowest and highest salary as parameters
and returns all employees with-in that salary range. If there re no employees,
an exception has to be raised and an application error to be given?
3. Write a PL/SQL procedure where you define your own user exceptions and
handle exceptions appropriately.

Das könnte Ihnen auch gefallen