PL/SQL

advertisement
An introduction to using Oracle PL/SQL
Definition:
PL/SQL is an Oracle’s procedural
extension of SQL, it is an advanced
fourth-generation programming
language (4GL).
.
4-1
Database 1
Oracle PL/SQL
Objective:
PL/SQL
• offers modern features such as data
encapsulation, overloading, collection types,
exception handling, and information hiding.
• improve database performance, make your
applications simpler and easier to maintain for
an Oracle developer or DBA.
4-2
Database 1
Oracle PL/SQL
Advantages of Oracle PLSQL
•
PL/SQL has over other programming languages
its tight coupling with the Oracle database.
•
This makes PL/SQL programs easier to write and
very efficient at accessing the database.
•
4-3
Reduced Network Traffic
Database 1
Oracle PL/SQL
Advantages of Oracle PLSQL
• PLSQL is also fully portable. You can use
PL/SQL on any platform which runs Oracle
and you can transfer PLSQL programs from
one platform to another without having to
recompile them.
4-4
Database 1
Oracle PL/SQL
Disadvantages of Oracle PLSQL
•Proprietary to Oracle
PL/SQL is proprietary to Oracle (means if you
change database then you would have to re-write
all your Oracle PL/SQL programs or write your
applications in a database-neutral language like
Java, VB or C and use the APIs to access the
database.
4-5
Database 1
Oracle PL/SQL
Disadvantages of Oracle PLSQL
•Poor I/O Features
Oracle PL/SQL has very little support for i/o either
to read/write files or to read from or write to a user
interface. PLSQL is designed for manipulating
information from the database and not for
processing files or communicating with users.
4-6
Database 1
PL/SQL Data Types
Numeric types
Character types
Composite Types
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•RECORD
•TABLE
•VARRAY
4-7
BINARY_INTEGER
DEC
DECIMAL
FLOAT
INT
INTEGER
NATURAL
NUMBER
NUMERIC
PLS_INTEGER
POSITIVE
REAL
CHAR
CHARACTER
LONG
LONG RAW
NCHAR
LOB Types
STRING
•BFILE
•BLOB
•CLOB
•NCLOB
UROWID
Reference Types
VARCHAR
•REF CURSOR
•REF object_type
NVARCHAR2
RAW
ROWID
VARCHAR2
Database 1
PL/SQL Data Types
1- Built-in Datatypes
•The built-in data types comprise integer, real
(floating point), character, date/time and Boolean
and support the manipulation of LOB (large object)
data types.
•NUMBER(precision, scale)
•REAL
•FLOAT(binary precision).
4-8
Database 1
PL/SQL Data Types
Character types are available as:-
•fixed length (CHAR(length))
•variable length (VARCHAR2(max length))
•2- User-Defined Subtypes
•The standard PL/SQL data types can be extended
by defining your own types in the form of records
and PLSQL collections.
4-9
Database 1
Oracle PL/SQL Language Elements
PL/SQL the language elements consist of:-
1- loops
•numeric and cursor FOR loops (the latter act on
the results of queries)
•WHILE LOOPs
•Simple LOOPs
4 - 10
Database 1
Oracle PL/SQL Language Elements
2-Conditional and sequential control statements
•IF THEN and IF THEN ELSE
•GOTO (but the use of this is usually not
necessary)
•simple and searched CASE statements
3- Anonymous blocks
4- Named blocks
•PROCEDURES
•FUNCTIONS
4 - 11
Database 1
Oracle PL/SQL block
•The general syntax is …
4 - 12
Database 1
Oracle PL/SQL block
•The general syntax is ...
•DECLARATIONS
• BEGIN
/* start of anonymous block or
procedure/function */
< PL/SQL statements>
•EXCEPTION
WHEN <exception> THEN <PL/SQL statements> ;
•END ;
4 - 13
/*end of anonymous block or
procedure/function */
Database 1
Variables and Constants
•Examples of variable and constant declarations:
•credit_limit CONSTANT NUMBER := 5000;
•invalid BOOLEAN := FALSE;
•acct_id INTEGER(4) NOT NULL DEFAULT 9999;
•pi CONSTANT REAL := 3.14159;
•postal_code VARCHAR2(20);
•last_name VARCHAR2(20 CHAR);
•my_ename emp.ename%TYPE;
4 - 14
Database 1
Variables and Constants
%TYPE
•This attribute provides the datatype of a previously
declared collection, cursor variable, field, object,
record, database column, or variable. Example:
•DECLARE
•my_empno emp.empno%TYPE;
•...
•BEGIN
•my_empno := NULL;
4 - 15
Database 1
Variables and Constants
•%ROWTYPE
•This attribute provides a record type that
represents a row in a database table or a row
fetched from a previously declared cursor.
• Fields in the record and corresponding columns
in the row have the same names and data types.
4 - 16
Database 1
Variables and Constants
%ROWTYPE
•Example:
•DECLARE
•emp_rec emp%ROWTYPE;
•CURSOR c1 IS SELECT deptno, dname, loc FROM
dept;
•dept_rec c1%ROWTYPE;
•BEGIN
•SELECT * INTO emp_rec FROM emp WHERE ...
4 - 17
Database 1
PL/SQL USES TWO TYPES OF CURSORS
1- Implicit
2- Explicit.
Implicitly for all SQL data manipulation
statements, including queries that return
only one row.
For queries that return more than one row,
you must declare an explicit cursor, use a
cursor FOR loop.
4 - 18
Database 1
Cursor declarations
• Cursor
variable is used a to access the
information after executing a multi-row
query (Oracle opens an unnamed work area
that stores processing information).
4 - 19
Database 1
Cursor declarations
Examples of cursor declarations :
•CURSOR c1 IS SELECT empno, ename, job, sal FROM
emp WHERE sal > 2000;
•CURSOR c2 RETURN dept%ROWTYPE IS SELECT *
FROM dept WHERE deptno = 10;
4 - 20
Database 1
CURSOR
Every explicit cursor and cursor variable has four
characteristics:
•%FOUND
•%ISOPEN
•%NOTFOUND
• %ROWCOUNT.
4 - 21
Database 1
%ISOPEN
•This is a cursor attribute that can be
appended to the name of a cursor or
cursor variable.
• If a cursor is open,
cursor_name%ISOPEN yields TRUE;
otherwise, it yields FALSE.
4 - 22
Database 1
%NOTFOUND
This is a cursor attribute that can be
appended to the name of a cursor or
cursor variable.
Before the first fetch from an open cursor,
cursor_name%NOTFOUND yields NULL.
Thereafter, it yields FALSE if the last fetch
returned a row, or TRUE if the last fetch
failed to return a row.
4 - 23
Database 1
%ROWCOUNT
•This is a cursor attribute that can be appended
to the name of a cursor or cursor variable. When
a cursor is opened, %ROWCOUNT is zeroed.
Before the first fetch,
cursor_name%ROWCOUNT yields 0.
Thereafter, it yields the number of rows fetched
so far.
4 - 24
Database 1
•Example
declare
CURSOR my_cursor IS
SELECT sal *12 anual , ename FROM emp;
my_rec my_cursor%ROWTYPE;
BEGIN
OPEN my_cursor;
LOOP
FETCH my_cursor INTO my_rec;
EXIT WHEN my_cursor%NOTFOUND;
IF my_rec.anual > 2000 THEN
INSERT INTO temp VALUES (NULL, my_rec.anual, my_rec.ename);
END IF;
END LOOP;
CLOSE my_cursor;
END;
4 - 25
Database 1
Conditional Statements in PL/SQL
1) IF THEN ELSE STATEMENT
IF condition
THEN
statement 1;
ELSE
statement 2;
END IF;
4 - 26
Database 1
IF THEN ELSIF ELSE STATEMENT
•
2)IF condition 1 THEN
statement 1;
statement 2;
ELSIF condtion2 THEN
statement 3;
ELSE
statement 4;
END IF
4 - 27
Database 1
CASE statements
• CASE
• WHEN x = 1 THEN sequence_of_statements_1;
• WHEN x = 2 THEN sequence_of_statements_2;
• WHEN x = 3 THEN sequence_of_statements_3;
• WHEN x = 4 THEN sequence_of_statements_4;
• WHEN x = 5 THEN sequence_of_statements_5;
• ELSE sequence_of_statements_N;
• END CASE;
4 - 28
Database 1
CASE statement can be used with predefined
selector
CASE
x
WHEN 1 THEN sequence_of_statements_1;
WHEN 2 THEN sequence_of_statements_2;
WHEN 3 THEN sequence_of_statements_3;
WHEN 4 THEN sequence_of_statements_4;
WHEN 5 THEN sequence_of_statements_5;
ELSE sequence_of_statements_N;
END CASE;
4 - 29
Database 1
examples
CASE
WHEN a < b THEN
WHEN d < e THEN
'hello'
'goodbye‘
END
CASE
WHEN supplier_name = 'IBM' and supplier_type = 'Hardware'
THEN 'North office‘
WHEN supplier_name = 'IBM' and supplier_type = 'Software'
THEN 'South office‘
END
4 - 30
Database 1
PL/SQ Simple LOOP
•The simplest form of LOOP statement is the basic
(or infinite) loop, with the keywords LOOP and END
LOOP, as follows:
LOOP
sequence_of_statements
END LOOP;
4 - 31
Database 1
EXIT
•The EXIT statement forces a loop to complete
unconditionally. An example follows:
LOOP
<statements>
IF credit_rating < 3 THEN
<statements>
EXIT; -- exit loop immediately
•END IF;
•END LOOP;
•-- control resumes here
4 - 32
Database 1
EXIT WHEN
LOOP
FETCH c1 INTO ...
EXIT WHEN c1%NOTFOUND;
-- exit loop if condition is true
...
END LOOP;
CLOSE c1;
4 - 33
Database 1
PL/SQL
WHILE-LOOP
The WHILE-LOOP statement associates a
condition with a sequence of statements
enclosed by the keywords LOOP and END
LOOP, as follows:
WHILE condition LOOP
sequence_of_statements
END LOOP;
4 - 34
Database 1
PL/SQL
WHILE-LOOP
An example
WHILE total <= 25000 LOOP
...
SELECT sal INTO salary FROM emp WHERE ...
total := total + salary;
END LOOP;
4 - 35
Database 1
PL/SQL
FOR-LOOP
The number of iterations through a FOR loop is
known before the loop is entered. FOR loops iterate
over a specified range of integers.
The syntax follows:
FOR counter IN [REVERSE]
lower_bound..higher_bound LOOP
sequence_of_statements
END LOOP;
4 - 36
Database 1
Oracle PL/SQL for loop
An example
FOR i IN 1..3 LOOP
-- assign the values 1,2,3 to i
sequence_of_statements
-- executes three times
END LOOP;
FOR i IN REVERSE 1..3 LOOP
-- assign the values 3,2,1 to i
sequence_of_statements
-- executes three times
END LOOP;
4 - 37
Database 1
PL/SQ CURSOR FOR
Loops
The general structure is as follows:
•FOR <record> IN <cursor> LOOP
<PL/SQL statements>
END LOOP;
4 - 38
Database 1
•PL/SQ
CURSOR FOR Loops
Advantage
Oracle automatically OPENs the cursor, the
results are automatically FETCHed and when all
rows have been returned, Oracle automatically
CLOSEs the cursor for you.
4 - 39
Database 1
PL/SQ CURSOR FOR Loops
disadvantages
•
If the query returns no data, then the body of the
loop is not executed which causes an error.
•
Processing is exactly the same for each row
returned, which means you can't initialize
variables.
4 - 40
Database 1
Download