Tải bản đầy đủ (.ppt) (115 trang)

oracle slides02 fp2005 ver 1.0

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (404.19 KB, 115 trang )

PL/SQL
Oracle Day 2
2
I-2
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
Objectives

To explain the need for a procedural language and the constructs
of PL/SQL.

To explain Exception/Error handling in PL/SQL blocks such as
Named and Un-named System Exceptions, User defined
Exceptions
3
I-3
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
About PL/SQL

PL/SQL is the procedural extension to SQL with design
features of programming languages.

Data manipulation and query statements of SQL are included
within procedural units of code.
4
I-4


Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
PL/SQL
block
PL/SQL engine
Oracle server
Procedural
statement
executor
PL/SQL
SQL
SQL statement executor
PL/SQL
block
PL/SQL Environment
5
I-5
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
Application
Oracle server
Shared
library
Integration
Benefits of PL/SQL 1/4
6

I-6
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
Application
Other DBMSs
Application
Oracle with
PL/SQL
SQL
SQL
SQL
SQL
SQL
IF THEN
SQL
ELSE
SQL
END IF;
SQL
Improved performance
Benefits of PL/SQL 2/4
7
I-7
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
DECLARE

BEGIN
END;
EXCEPTION
Benefits of PL/SQL 3/4
Modularize program development
8
I-8
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0

PL/SQL is portable

You can declare variables

You can program with procedural language control
structures.

PL/SQL can handle errors.

Benefits of PL/SQL 4/4
9
I-9
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
DECLARE (Optional)
Variables, cursors, user-defined exceptions

BEGIN (Mandatory)

SQL statements

PL/SQL statements
EXCEPTION (Optional)
Actions to perform when errors occur
END; (Mandatory)
END;
PL/SQL Block Structure
10
I-10
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
DECLARE
var1 VARCHAR2(5);
BEGIN
SELECT column_name
INTO var1
FROM t1;
EXCEPTION
WHEN exception THEN

END;
Executing Statements and PL/SQL Blocks
11
I-11
Copyright © 2005, Infosys

Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
Anonymous Procedure Function
[DECLARE]
BEGIN
statements
[EXCEPTION]
END;
PROCEDURE name
IS
BEGIN
statements
[EXCEPTION]
END;
FUNCTION name
RETURN datatype
IS
BEGIN
statements
RETURN value;
[EXCEPTION]
END;
Block Types
12
I-12
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0

Variables can be used for:

Temporary storage of data

Manipulation of stored values

Reusability

Ease of maintenance
Use of Variables
13
I-13
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0

Declare and initialize variables in the declaration
section.

Assign new values to variables in the executable
section.

Pass values into PL/SQL blocks through parameters.

View results through output variables.
Handling Variables in PL/SQL
14
I-14
Copyright © 2005, Infosys

Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0

PL/SQL variables:

Scalar

Composite

Reference

LOB (large objects)

Non-PL/SQL variables: Bind and host variables
Types of Variables
15
I-15
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
identifier [CONSTANT] datatype [NOT NULL]
[:= | DEFAULT expr];
DECLARE
v_date DATE;
v_dno NUMBER(2) NOT NULL := 5;
v_location VARCHAR2(13) := ‘Berhampur’;
c_incentive CONSTANT NUMBER := 100;
Syntax:

Examples:
Declaring PL/SQL Variables
16
I-16
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
identifier := expression;
Guidelines for Declaring PL/SQL Variables

Follow naming conventions.

Initialize variables designated as NOT NULL and
CONSTANT.

Declare one identifier per line.

Initialize identifiers by using the assignment operator (:=)
or the DEFAULT reserved word.
17
I-17
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
DECLARE
empno NUMBER(6);
BEGIN
SELECT empno

INTO empno
FROM emp
WHERE ename = ‘Sanjukta';
END;
/
Follow a naming
convention for
PL/SQL identifiers:
e.g.: v_empno

Two variables can have the same name, provided they are in different
blocks.

The variable name (identifier) should not be the same as the name of
table columns used in the block.
Naming Rules
18
I-18
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
identifier := expression;
v_joindate := '01-JAN-2004';
v_name := ‘Swetalina';

Assignment operator (:=)

DEFAULT keyword


NOT NULL constraint
Syntax:
Examples:
Variable Initialization and Keywords
19
I-19
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
Scalar Data Types

CHAR [(maximum_length)]

VARCHAR2 (maximum_length)

LONG

LONG RAW

NUMBER [(precision, scale)]

BINARY_INTEGER

PLS_INTEGER

BOOLEAN

DATE
20

I-20
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
DECLARE
v_name VARCHAR2(9);
v_count BINARY_INTEGER := 0;
v_sal NUMBER(9,2) := 0;
v_shipdate DATE := SYSDATE + 14;
c_tax CONSTANT NUMBER(3,2) := 6.75;
v_isMale BOOLEAN NOT NULL := TRUE;

Examples:
Scalar Variable Declarations
21
I-21
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0

Declare a variable according to:

A database column definition

Another previously declared variable

Prefix %TYPE with:


The database table and column

The previously declared variable name
The %TYPE Attribute
22
I-22
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0

v_name empl.name%TYPE;
v_bal NUMBER(7,2);
v_min_bal v_bal%TYPE := 10;

identifier Table.column_name%TYPE;
Declaring Variables
with the %TYPE Attribute
Examples:
Syntax:
23
I-23
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
Declaring Boolean Variables

Only the values TRUE, FALSE, and NULL can be
assigned to a Boolean variable.


The variables are compared by the logical operators
AND, OR, and NOT.

The variables always yield TRUE, FALSE, or NULL.

Arithmetic, character, and date expressions can be
used to return a Boolean value.
24
I-24
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
BFILE
BLOB
CLOB
LOB Data Type Variables
25
I-25
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 1.0
Server
Bind variable
Bind Variables

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×