Sie sind auf Seite 1von 30

PL/SQL

Deepa Rajesh

2004 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice

Introduction

Content

Introduction to PL/SQL Database Objects (Tables, Views, Data Types, indexes) Anonymous Blocks

February 17, 2014

'HP Restricted'

Basic Concepts
The following definitions explain concepts central to SQL*Plus: command : An instruction you give SQL*Plus or Oracle. block : A group of SQL and PL/SQL commands related to one another through procedural logic.

table
query

: The basic unit of storage in Oracle.


: A SQL command (specifically, a SQL SELECT command) that retrieves information from one or more tables.

query results : The data retrieved by a query. report : Query results formatted by you through SQL*Plus commands.
February 17, 2014 'HP Restricted' 3

PL/SQL Introdution
PL/SQL is Oracles procedural language extension to the SQL language. It has its origins in ADA, a high-level programming language developed for the US Department of Defense. ADA was named after a daughter of Lord Byron, Augusta ADA, Countess of Lovelace, a 19th century mathematician, whom many regard as the worlds first computer programmer because of her work with Charles Babbages Analytical Engine. PL/SQL is an extremely powerful structured language, which is not only tightly integrated with SQL but also incorporates many of the advanced features of other procedural languages. It provides the foundation for distributing and processing transactions across the network for both client/server and Web applications. Despite its structured approach, PL/SQL it is also a language that , is very easy to abuse and misuse
February 17, 2014 'HP Restricted' 4

What is a table ?

February 17, 2014

'HP Restricted'

How to create a table


create table emp ( empno number(6,0), empname varchar2(25), deptcode number(3,0) ) ; Table Attribute

column constraints table constraints storage parameters default value for a column cache and no-cache

February 17, 2014

'HP Restricted'

Types of Tables

Stage Tables

Operational Tables
Functional Tables Disposition Tables

Archive tables
Audit Tables

February 17, 2014

'HP Restricted'

Views

A logical table based on one or more tables or views. A view contains no data itself. (Base Tables) To provide an additional level of table security, by restricting access to a predetermined set of rows and/or columns of a base table. To hide data complexity. For example, a view may be used to act as one table when actually several tables are used to construct the result. To present data from another perspective. For example, views provide a means of renaming columns without actually changing the base tables definition.

February 17, 2014

'HP Restricted'

Graphical view of a View

February 17, 2014

'HP Restricted'

How to create views

select empno,ename,dept.dname from emp, dept where emp.deptno = dept.deptno; create view empdept as select empno, ename, dept.dname from emp, dept where emp.deptno = dept.deptno ; select * from empdept ;

February 17, 2014

'HP Restricted'

10

PL/SQL

what is PL/SQL? A programming language which use SQL statements to maninpuate Oracle data and flow-control statements to process data Architecture Advantages of PL/SQL Block Structure Variable and Constants (not case sensitive) Cursors Attributes Control structures Modularity Error Handling

February 17, 2014

'HP Restricted'

11

Structure

PL/SQL is an algorithmic, block-structured language. A block is the basic unit from which all PL/SQL programs are built. A block can be named (functions and procedures) or anonymous. An anonymous block can exist only within a named block, a SQL script, or a trigger. A block consists of between one and four sections

Declaration Section : Declares variables, cursors etc. to be used in the block. BEGIN : Executable code Exception Section (optional): Exception (error) handling END;

Example: DECLARE <variable name> <data type><(length precision)>; BEGIN <valid statement>; BEGIN <valid statement>; EXCEPTION <exception handler>; END; EXCEPTION <exception handler>; END; /

February 17, 2014

'HP Restricted'

12

A Sample PL/SQL program


DECLARE qty_on_hand NUMBER(5); BEGIN SELECT quantity INTO qty_on_hand FROM inventory WHERE product = TENNIS RACKET FOR UPDATE OF quantity; IF qty_on_hand > 0 THEN -- check quantity UPDATE inventory SET quantity = quantity - 1 WHERE product = TENNIS RACKET; INSERT INTO purchase_record VALUES (Tennis racket purchased, SYSDATE); ELSE INSERT INTO purchase_record VALUES (Out of tennis rackets, SYSDATE); END IF; COMMIT; END;

February 17, 2014

'HP Restricted'

13

PL/SQL Architecture

How PL/SQL Works . . . . .

February 17, 2014

'HP Restricted'

14

Advantages of PL/SQL

SQL Support Performance

February 17, 2014

'HP Restricted'

15

Data types

February 17, 2014

'HP Restricted'

16

Comments

Single line comments


- - this is a single line comment

Multi line comments


/* This is a multi-line comment spanning two lines */

Comment restrictions
Nested comments are not allowed

February 17, 2014

'HP Restricted'

17

How to declare and initialize variables


birthday DATE;

birthday DATE := NULL;


credit_limit CONSTANT REAL := 5000.00;

February 17, 2014

'HP Restricted'

18

Attributes

%TYPE
balance number(7,2) ; minimum_balance balance%TYPE := 10.00; maximum_balace balance%TYPE ; local_empname scott.emp.empname%TYPE;

%ROWTYPE
cursor cur1 select * from emp ; emp_cur cur1%ROWTYPE; dept_cur scott.dept%ROWTYPE;

February 17, 2014

'HP Restricted'

19

Scope and Visibility

The scope of an identifier is that region of a program unit from which you can reference the identifier. An identifier is visible only in the regions from which you can reference the identifier using an unqualified name.

February 17, 2014

'HP Restricted'

20

Scope and Visibility

February 17, 2014

'HP Restricted'

21

Built in Functions

February 17, 2014

'HP Restricted'

22

Control Structures

February 17, 2014

'HP Restricted'

23

Conditional Control

IF.. then .. end if;


If .. then .. else .. end if; If .. then .. Elsif .. else .. end if ;

February 17, 2014

'HP Restricted'

24

Iterative control

LOOP .. END LOOP

WHILE <COND> LOOP .. END LOOP;


FOR CTR IN minvalue..maxvalue LOOP .. END LOOP; EXIT EXIT WHEN

February 17, 2014

'HP Restricted'

25

Iterative Control - Examples

LOOP . IF CTR > 10 THEN EXIT ; END IF; END LOOP;

WHIL ctr < max LOOP .. EXIT WHEN CUR%NOTFOUND END LOOP;

<<OUTER>>
FOR I IN 1..5 loop .. For j in 1..10 LOOP fetch c1 into emp_rec; EXIT outer WHEN c1%NOTFOUND . END LOOP; END LOOP ;

February 17, 2014

'HP Restricted'

26

Sequential Control

Syntax
Goto <<label>> ; Null (do nothing)
IF rating > 90 THEN GOTO calc_raise; -- branch to label END IF; ... <<calc_raise>> IF job_title = 'SALESMAN' THEN -- control resumes here amount := commission * 0.25; ELSE amount := salary * 0.10; END IF;

Restrictions
cannot branch into IF cannot branch into else cannot branch into LOOP cannot branch into sub-block cannot branch out of a procedure cannot branch out of execption handler to a block

February 17, 2014

'HP Restricted'

27

Cursors

cursor is used to manipulate or process queries that result in more than one row. Cursors can be declared in the declarative part of any PL/SQL block, subprogram, or package.
DECLARE CURSOR c1 IS SELECT empno, ename, job FROM emp WHERE deptno = 20;

February 17, 2014

'HP Restricted'

28

Cursor control commands

Cursors can be controlled with the following cursor control commands


DECLARE OPEN FETCH CLOSE
DECLARE CURSOR c1 IS SELECT ename, sal, hiredate, deptno FROM emp; ... BEGIN FOR emp_rec IN c1 LOOP ... salary_total := salary_total + emp_rec.sal; END LOOP;

February 17, 2014

'HP Restricted'

29

Complete Cursor Example


DECLARE my_sal emp.sal%TYPE; my_job emp.job%TYPE; factor INTEGER := 2;
CURSOR c1 IS SELECT factor*sal FROM emp WHERE job = my_job;

BEGIN OPEN c1; -- here factor equals 2 LOOP FETCH c1 INTO my_sal; EXIT WHEN c1%NOTFOUND; factor := factor + 1; -- does not affect FETCH END LOOP; CLOSE c1; END;

February 17, 2014

'HP Restricted'

30

Das könnte Ihnen auch gefallen