Chap 1 Introduction
Chap 1 Introduction
Introduction to PL/SQL
Objectives
At the end of this session, you will be able to :
Understand the need for PL/SQL Learn the structure of a PL/SQL Block Create PL/SQL Blocks Declare variables Write Nested Blocks
2
SQL Star International Ltd 2007
3
SQL Star International Ltd 2007
Benefits of PL/SQL
Benefits of using PL/SQL are :
Integration Performance enhancements of applications Modularized program development Portability Variable declaration Usage of control structures in programming Error handling
4
PL/SQL Engine
PL/SQL Engine
PL/SQL Block
Procedural
6
SQL Star International Ltd 2007
(contd.)
7
SQL Star International Ltd 2007
(contd.)
Declarative section: All variables and constants declaration. Keyword: DECLARE Executable Section: Actual logic of the program. Keywords: BEGIN Exception section: Code for handling errors. Keyword: EXCEPTION END keyword: Ends the executable section of PL/SQL block
8
SQL Star International Ltd 2007
Operators in PL/SQL
The operators supported in PL/SQL are :
9
SQL Star International Ltd 2007
Single Row number and Character functions Datatype Conversion functions Date Functions Timestamp functions GREATEST and LEAST functions
10
SQL Star International Ltd 2007
Variables
Named memory locations to store data temporarily. Declared in the declarative section and assigned values in the executable section. Must be declared before its usage in the program.
11
SQL Star International Ltd 2007
Types of Variables
Variables are of two types :
12
SQL Star International Ltd 2007
PL/SQL Variables
Have an associated datatype that specifies a storage format, constraint and a valid range of values. Syntax: identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; Example:
DECLARE BookID CHAR(13) NOT NULL; BookName CHAR(100); noofcopies NUMBER(5) := 15;
13
SQL Star International Ltd 2007
PL/SQL variables
(contd.)
14
SQL Star International Ltd 2007
PL/SQL variables
(contd.)
Non-PL/SQL variables
Non-PL/SQL variables include :
Substitution variables: Used to pass values at runtime. Host variables (Bind variables): Used to pass runtime values from the PL/SQL block back to iSQL*Plus environment.
16
SQL Star International Ltd 2007
Non-PL/SQL variables
Example: DECLARE
(contd.)
Substitution variables :
%TYPE attribute is used to declare a variable to be of the same data type as that of a previously declared variable or database column.
17
SQL Star International Ltd 2007
Non-PL/SQL variables
Host variables :
Example
(contd.)
VARIABLE new_price NUMBER DECLARE bookprice NUMBER(5) := &bprice BEGIN :new_price := bookprice-10; END; / PRINT new_price
18
SQL Star International Ltd 2007
Nested Blocks
Nesting of blocks :
Allowed wherever an executable statement exists Allowed even in Exception section of a program Breaks the program into Modules thereby, making maintenance easy
19
SQL Star International Ltd 2007
Variable Scope
Variable scope is the ability to refer to a variable within the program. Variables declared in the outermost block can always be referred within their enclosed blocks.
Example:
DECLARE a NUMBER; BEGIN .... DECLARE c NUMBER; BEGIN c := a; END; END;
20
SQL Star International Ltd 2007
Scope of c
Scope of a
Summary
In this session, we have discussed :
Need for Procedural language with SQL Benefits of using PL/SQL PL/SQL Block Types and Constructs PL/SQL Block Structure Operators and SQL Functions in PL/SQL Data Conversion functions Nested Blocks
21
SQL Star International Ltd 2007