22
PL/SQL

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

Embed Size (px)

Citation preview

Page 1: 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

PL/SQL

Page 2: 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

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 .

Page 3: 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

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 .

Page 4: 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

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 ;

Page 5: 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

•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.

Page 6: 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

•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 ;

Page 7: 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

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);

Page 8: 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

.2Declareid emp.empno%TYPE;

name emp.ename%TYPE;

variable name

table name

column name

Page 9: 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

.3Declaredepttuple dept%ROWTYPE;

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

depttuple(deptno,dname,location)

Page 10: 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

•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;

Page 11: 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

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

Page 12: 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

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) ;

Page 13: 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

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

Page 14: 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

Control Flow in PL/SQL

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

Page 15: 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

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;

Page 16: 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

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 ;

Page 17: 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

LOOP-EXIT WHEN-END LOOP

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

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

END LOOP;

Page 18: 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

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 ;

Page 19: 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

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

Page 20: 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

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 .

Page 21: 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

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 ;

Page 22: 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

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 ;