PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access...

Preview:

Citation preview

PL/SQL

Introduction to PL/SQL

•PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various

environments (e.g. Forms, Reports to create triggers, procedures, functions, etc.) .

•PL/SQL provides high-level language features such as block structure, conditional statements, loop

statements, variable types, structured data and customized error handling

•PL/SQL is integrated with the database server. It does not exist as a standalone language .

Basic Structure of PL/SQL

•The basic unit in PL/SQL is a block. All PL/SQL programs are made up of blocks, which can be

nested within each other .

DECLARE    */ Declarative section: variables, types, and

local subprograms     /* .BEGIN  

*/  Executable section: procedural and SQL statements go here    /* .

*/ This section of the block is required     /* .EXCEPTION  

*/ Exception handling section: error handling statements go here     /* .

END ;

•Only the executable section is required. The other sections are optional.

•The only SQL statements allowed in a PL/SQL program are SELECT, INSERT, UPDATE, DELETE

and several other data manipulation statements plus some transaction control .

•Data definition statements like CREATE, DROP, or ALTER are not allowed.

•The executable section also contains constructs such as assignments, branches,

loops, procedure calls, and triggers.•PL/SQL is not case sensitive. C style comments

(/* ... */) may be used, or for a one line comment precede it with.)--(

•To execute a PL/SQL program, we must follow the program text itself by

•A line with a single dot ("."), and then •A line with run ;

Variables and Types

•Types in PL/SQL are the same as in SQL, in addition to the BOOLEAN type.

•Variables can be declared in the following ways:

.1Declareid NUMBER;

name VARCHAR(20);

.2Declareid emp.empno%TYPE;

name emp.ename%TYPE;

variable name

table name

column name

.3Declaredepttuple dept%ROWTYPE;

depttuple will be a record that contains fields that represent the columns in table dept.

depttuple(deptno,dname,location)

•A variable can be intialized:Declare

a NUMBER :=3;•A variable can be constrained to not null:

Declarea NUMBER NOT NULL:=3;

Or a NUMBER NOT NULL DEFAULT 3;

Select statement

•Select statement has a different formSELECT column INTO variables

FROM tableWHERE condition;

Select should return a single tuple, if several tuples are required, use a Cursor

Simple program

•Using the following table declaration:CREATE TABLE T1     (

e INTEGER     ,f INTEGER ;)

INSERT INTO T1 VALUES(1, 3) ;INSERT INTO T1 VALUES(2, 4) ;

DECLARE    a NUMBER    ;

b NUMBER ;BEGIN   

SELECT e,f INTO a,b FROM T1 WHERE e>1    ; INSERT INTO T1 VALUES(b,a) ;

END ;

e f

1 32 4

e f1 32 44 2

Control Flow in PL/SQL

•IF-THEN-END-IF•LOOP-EXIT WHEN-END LOOP•FOR-END LOOP•WHILE-END LOOP•GOTO

IF-THEN-END-IF

•IF <condition> THEN <statement_list> ELSE <statement_list> END IF;

•IF <condition_1> THEN ... ELSIF <condition_2> THEN ... ... ... ELSIF <condition_n> THEN ...

ELSE ... END IF;

DECLARE     a NUMBER     ;b NUMBER ;

BEGIN     SELECT e,f INTO a,b FROM T1 WHERE e>1     ;

IF b=1 THEN         INSERT INTO T1 VALUES(b,a)     ;

ELSE         INSERT INTO T1 VALUES(b+10,a+10)     ;

END IF ;END ;

LOOP-EXIT WHEN-END LOOP

•LOOP     <loop_body> /* A list of statements /* .

EXIT WHEN <condition ;>*/list of statements/*

END LOOP;

Insert each of the pairs (1, 1) through (100, 100) into T1 DECLARE    

i NUMBER := 1; BEGIN    

LOOP        INSERT INTO T1 VALUES(i,i)         ;

i := i+1        ;EXIT WHEN i>100    ;

END LOOP ;END ;

While - For

•WHILE <condition >LOOP        

<loop_body     >END LOOP ;

•FOR <var> IN [REVERSE]<start>..<finish >LOOP        

<loop_body     >END LOOP ;

<--var> will be declared implicitly

Cursors

•A cursor is a variable that runs through the tuples of some relation.

•By fetching into the cursor each tuple of the relation, we can write a program to read and

process the value of each such tuple.Ex: Delete every tuple whose first component is

less than the second, and insert the reverse tuple into T1 .

DECLARE        */Output variables to hold the result of the query    /* :

a T1.e%TYPE ;    b T1.f%TYPE        ;

*/ Cursor declaration  /* :    CURSOR T1Cursor IS          

SELECT e,f FROM T1 WHERE e < f         FOR UPDATE ;

BEGIN OPEN T1Cursor ; LOOP            */Retrieve each row of the result of the above query into PL/SQL variables/* :

FETCH T1Cursor INTO a, b           ;   */If there are no more rows to fetch, exit the loop/* :

EXIT WHEN T1Cursor%NOTFOUND        ;      */Delete the current tuple/* :

DELETE FROM T1 WHERE CURRENT OF T1Cursor     ; */Insert the reverse tuple/* : INSERT INTO T1 VALUES(b, a) ;

   END LOOP         ; */Free cursor used by the query /* .

CLOSE T1Cursor ; END ;

Recommended