You are on page 1of 22

PL/SQL - I

Your IT Partner
PL/SQL-1 1

Objectives
Write your own PL/SQL code using conditional statements and loop constructs

PL/SQL-1

CMC

Introduction
SQL does not have any procedural capabilities such as looping and branching nor does it have any conditional checking capabilities vital for data testing before storage. PL/SQL adds to the power of SQL and provides the user with all the facilities of a programming environment. PL/SQL bridges the gap between database technology and procedural programming languages.
PL/SQL-1

CMC

Introduction contd.. Via PL/SQL you can insert, delete, update and retrieve table data as well as use procedural techniques such as writing loops or branching to another block of code. PL/SQL is really an extension of SQL. PL/SQL blocks can contain any number of SQL statements. It allows you to logically group a number of SQL statements and pass them to the DBA as a single block. Without PL/SQL, DBA has to process SQL statements one at a time.
PL/SQL-1

CMC

PL/SQL Block
A standard PL/SQL code segment is called a block. A standard PL/SQL block is made up of three sections. 1. Declaration Section is meant for declaring variables and constants. 2. Executable Statements Section is meant for manipulating objects during execution. 3. Exception Handling Section - any errors or exceptions raised during execution of the PL/SQL block are handled here.
PL/SQL-1

CMC

PL/SQL Engine

The PL/SQL Engine accepts the procedural requests and database queries from the PL/SQL code block and transfers them to the Procedural Statement Executer and the SQL Statement Executer respectively.
PL/SQL-1

CMC

Advantages of PL/SQL
Procedural Capabilities: Offers constructs for conditional selection, iteration etc. Variable and constant declarations are possible. Improved Performance: Processing is fast since entire block is sent to RDBMS rather than one statement at a time. Enhanced Productivity: PL/SQL can be embedded in ORACLE application development tools and 3 GL interface program.
PL/SQL-1

CMC

Advantages of PL/SQL contd..

Portability: An application developed in PL/SQL is portable across h/w and o.s. platforms. Integration with RDBMS: Variables in PL/SQL can belong to any SQL datatype such as - CHAR, DATE etc.

PL/SQL-1

CMC

Variables and Constants


Variables
A PL/SQL variable can be used either in SQL or PL/SQL statements provided it is declared before referencing it.

Data Types: BINARY_INTEGER Type: You use the BINARY_INTEGER datatype to store signed integers.
PL/SQL-1

CMC

Variables contd.. NUMBER Type: You use the NUMBER datatype to store fixed or floating point numbers of virtually any size. CHAR Type: You use the CHAR datatype to store fixed-length character data. VARCHAR2 Type: You use the VARCHAR2 datatype to store variable-length character data. LONG Type: You use the LONG datatype to store variable-length character strings.

PL/SQL-1

CMC

10

Variables contd.. RAW Type: You use the RAW datatype to store binary data or byte strings. LONG RAW Type: You use the LONG RAW datatype to store binary data or byte strings. BOOLEAN Type: You use the BOOLEAN datatype to store the values TRUE and FALSE and the non-value NULL. DATE Type: You use the DATE datatype to store fixedlength date values. The DATE datatype takes no parameters.
PL/SQL-1

CMC

11

Variables contd..

ROWID Type: Each row in the database has an address of the type Data Object Number.Relative File Number.Block Number.Slot Number MLSLABEL Type: With Trusted Oracle, you use the MLSLABEL datatype to store variable- length, binary operating system labels. Trusted Oracle uses labels to control access to data.

PL/SQL-1

CMC

12

Variables and Constants contd..

Constants
Declaration of a constant is similar to the declaration of a variable except that the keyword CONSTANT must be added and a value must be assigned.

PL/SQL-1

CMC

13

Declaration Using Attributes


%TYPE attribute
This attribute provides the data type of a variable, constant or column. %TYPE attribute is useful while declaring a variable that has same data type as a table column.

PL/SQL-1

CMC

14

Declaration Using Attributes contd..

%ROWTYPE attribute
This attribute is used to declare a variable to be a record having the same structure as a row in a table. The row is represented as a record and its fields have the same names and data types as the columns in the table or view.

PL/SQL-1

CMC

15

Operators
Logical: AND OR NOT

PL/SQL-1

CMC

16

Conditional Control
if (condition) then statements else statements end if; if (condition) then statements elsif (condition) then statements else statements end if;

PL/SQL-1

CMC

17

Iterative Control
1. Endless Loop LOOP statements .......... END LOOP; LOOP and END LOOP are the keywords.

PL/SQL-1

CMC

18

Iterative Control contd..

2. For Loop FOR i IN LOOP statements ......... END LOOP; 1..10

FOR loop index is implicitly declared to be of data type NUMBER.

PL/SQL-1

CMC

19

Iterative Control contd..

3. While Loop WHILE (condition) LOOP ......... statements ......... END LOOP;

PL/SQL-1

CMC

20

GOTO Statement
The GOTO statement changes the flow of control within the PL/SQL block. GOTO statement is used in combination with labelnames. Labels are used as markers before the block or executable statements to which control is to be transferred.

PL/SQL-1

CMC

21

Scope of an Identifier
An identifier is a name of any object (variable, constant, record, cursor or exception). In a nested block environment the scope of an identifier determines which blocks may reference that identifier. 1. Identifiers declared in a block are local to that block and global to all sub-blocks. 2. Global identifiers can be redeclared in a sub-block in which case the local declaration prevails.
PL/SQL-1

CMC

22

You might also like