introduction to pl-sql introduction to pl-sql aj. ประวัฒน์...

69
Introduction to PL-SQL Introduction to PL-SQL Aj. Aj. ปปปปปปปป ปปปปปปปปปปปปปป ปปปปปปปป ปปปปปปปปปปปปปป [email protected] Lecture by

Upload: leslie-holmes

Post on 04-Jan-2016

236 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

Introduction to PL-SQLIntroduction to PL-SQL

Aj. Aj. ประวั�ฒน์� เปรมธี รสมบู�รณ์�ประวั�ฒน์� เปรมธี รสมบู�รณ์�[email protected]

Lecture by

Page 2: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 22

ObjectivesObjectives

After completing this lesson, you should be able to do the following:

– List the benefits of PL/SQL– Recognize the basic PL/SQL block and

its sections– Describe the significance of variables

in PL/SQL– Declare PL/SQL variables– Execute a PL/SQL block

Page 3: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 33

Objectives (Con’t)Objectives (Con’t)

After completing this lesson, you should be able to do the following:

– Recognize the significance of the executable section

– Write statements in the executable section

– Describe the rules of nested blocks– Execute and test a PL/SQL block– Use coding conventions

Page 4: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 44

What is PLWhat is PL//SQL?SQL?

• PL/SQL is an extension to SQL with design features of programming languages.

• Data manipulation and query statements of SQL are included within procedural units of code.

ProceduralLanguage Extension

toSQL

PL/SQLPL/SQL

Page 5: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 55

Benefits of PLBenefits of PL//SQLSQL

• Integration

ApplicationApplication

Oracle ServerOracle ServerSharedSharedlibrarylibrary

Page 6: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 66

Benefits of PLBenefits of PL//SQLSQL

• Improved Performance

ApplicationApplication Other DBMSsOther DBMSs

ApplicationApplicationOracle with

PL/SQLOracle with

PL/SQL

SQLSQL

SQLSQLSQLSQL

SQLSQL

SQLSQLIFIF......THENTHEN

SQLSQLELSEELSE

SQLSQLEND IF;END IF;SQLSQL

Page 7: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 77

Benefits of PLBenefits of PL//SQLSQL

• Modularize program development

DECLAREDECLARE

BEGINBEGIN

EXCEPTIONEXCEPTION

END;END;

Page 8: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 88

Benefits of PLBenefits of PL//SQLSQL

• It is portable.• You can declare identifiers.• You can program with procedural

language control structures.• It can handle errors.

Page 9: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 99

PLPL//SQL Block StructureSQL Block Structure

• DECLARE – Optional– Variables, cursors, user-defined

exceptions

• BEGIN – Mandatory– SQL statements

– PL/SQL statements

• EXCEPTION – Optional– Actions to perform when errors occur

• END; – Mandatory

• DECLARE – Optional– Variables, cursors, user-defined

exceptions

• BEGIN – Mandatory– SQL statements

– PL/SQL statements

• EXCEPTION – Optional– Actions to perform when errors occur

• END; – MandatoryDECLAREDECLARE

BEGINBEGIN

EXCEPTIONEXCEPTION

END;END;

Page 10: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 1010

PLPL//SQL Block StructureSQL Block Structure

DECLARE v_variable VARCHAR2(5);BEGIN SELECT column_name INTO v_variable FROM table_name;EXCEPTION WHEN exception_name THEN ...END;

DECLARE v_variable VARCHAR2(5);BEGIN SELECT column_name INTO v_variable FROM table_name;EXCEPTION WHEN exception_name THEN ...END;

DECLAREDECLARE

BEGINBEGIN

EXCEPTIONEXCEPTION

END;END;

Page 11: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 1111

Block TypesBlock Types

• PL/SQL code is built of Blocks, with a unique structure

• There are two types of blocks in PL/SQL -:

1. Anonymous Blocks : have no name (like scripts)– Can be written and executed immediately in

SQLPLUS– Can be used in a trigger

2. Named Blocks :– Procedures– Functions

Page 12: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 1212

Block TypesBlock Types

AnonymousAnonymous ProcedureProcedureFunctionFunction

[[DECLAREDECLARE]]

BEGINBEGIN ----statementsstatements

[[EXCEPTIONEXCEPTION]]

END;END;

[[DECLAREDECLARE]]

BEGINBEGIN ----statementsstatements

[[EXCEPTIONEXCEPTION]]

END;END;

PROCEDURE namePROCEDURE nameISIS

BEGINBEGIN ----statementsstatements

[[EXCEPTIONEXCEPTION]]

END;END;

PROCEDURE namePROCEDURE nameISIS

BEGINBEGIN ----statementsstatements

[[EXCEPTIONEXCEPTION]]

END;END;

FUNCTION nameFUNCTION nameRETURN datatypeRETURN datatypeISISBEGINBEGIN ----statementsstatements RETURN value;RETURN value;[[EXCEPTIONEXCEPTION]]

END;END;

FUNCTION nameFUNCTION nameRETURN datatypeRETURN datatypeISISBEGINBEGIN ----statementsstatements RETURN value;RETURN value;[[EXCEPTIONEXCEPTION]]

END;END;

Page 13: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 1313

Program ConstructsProgram Constructs

AnonymousAnonymousblockblock

AnonymousAnonymousblockblock

ApplicationApplicationtriggertrigger

ApplicationApplicationtriggertrigger

Stored Stored procedureprocedure//

functionfunction

Stored Stored procedureprocedure//

functionfunction

DatabaseDatabasetriggertrigger

DatabaseDatabasetriggertrigger

ApplicationApplicationprocedureprocedure//

functionfunction

ApplicationApplicationprocedureprocedure//

functionfunction

PackagedPackagedprocedureprocedure//

functionfunction

PackagedPackagedprocedureprocedure//

functionfunction

DECLAREDECLARE

BEGINBEGIN

EXCEPTIONEXCEPTION

END;END;

Page 14: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 1414

Use of VariablesUse of Variables

Use variables for:• Temporary storage of data• Manipulation of stored values• Reusability• Ease of maintenance

Page 15: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 1515

Handling Variables in PLHandling Variables in PL//SQLSQL

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

Page 16: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 1616

Types of VariablesTypes of Variables

• PL/SQL variables:– Scalar– Composite– Reference– LOB (large objects)

• Non-PL/SQL variables : Bind and host variables

Page 17: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 1717

Types of VariablesTypes of Variables

• PL/SQL variables:– Scalar– Composite– Reference– LOB (large objects)

• Non-PL/SQL variables : Bind and host variables

Page 18: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 1818

Types of VariablesTypes of Variables

TRUETRUE25-OCT-9925-OCT-99

“Four score and seven years ago

our fathers brought forth upon

this continent, a new nation,

conceived in LIBERTY, and dedicated

to the proposition that all men

are created equal.”256120.08256120.08AtlantaAtlanta

Page 19: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 1919

Declaring PLDeclaring PL//SQL VariablesSQL Variables

• SyntaxSyntax

• ExamplesExamples

identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr];

identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr];

Declare v_hiredate DATE; v_deptno NUMBER(2) NOT NULL := 10; v_location VARCHAR2(13) := 'Atlanta'; c_comm CONSTANT NUMBER := 1400;

Declare v_hiredate DATE; v_deptno NUMBER(2) NOT NULL := 10; v_location VARCHAR2(13) := 'Atlanta'; c_comm CONSTANT NUMBER := 1400;

Page 20: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 2020

Declaring PLDeclaring PL//SQL VariablesSQL Variables

Guidelines– Follow naming conventions.– Initialize variables designated as NOT

NULL.– Initialize identifiers by using the

assignment operator (:=) or the DEFAULT reserved word.

– Declare at most one identifier per line.

Page 21: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 2121

Naming RulesNaming Rules

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

DECLARE empno NUMBER(4);BEGIN SELECT empno INTO empno FROM emp WHERE ename = 'SMITH';END;

DECLARE empno NUMBER(4);BEGIN SELECT empno INTO empno FROM emp WHERE ename = 'SMITH';END;

Adopt a naming convention fo

r

Adopt a naming convention fo

r

PLPL//SQL identifiers

SQL identifiers::

for example, v_empno

for example, v_empno

Page 22: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 2222

Assigning Values to VariablesAssigning Values to Variables

• SyntaxSyntax

• ExamplesExamples– Set a predefined hiredate for new employees

.

– Set the employee name to “Maduro.”v_ename := 'Maduro';v_ename := 'Maduro';

v_hiredate := '31-DEC-98';v_hiredate := '31-DEC-98';

•identifier := expr;•identifier := expr;

Page 23: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 2323

Scalar DatatypesScalar Datatypes

• Hold a single value• Have no internal components

AtlantaAtlanta

“Four score and seven years

ago our fathers brought

forth upon this continent, a

new nation, conceived in

LIBERTY, and dedicated to

the proposition that all men

are created equal.”

TRUETRUE

256120.08256120.08

25-OCT-9925-OCT-99

Page 24: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 2424

Base Scalar DatatypesBase Scalar Datatypes

• VARCHAR2 (maximum_length)• NUMBER [(precision, scale)]• DATE• CHAR [(maximum_length)]• LONG• LONG RAW• BOOLEAN• BINARY_INTEGER• PLS_INTEGER

Page 25: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 2525

Scalar Variable DeclarationsScalar Variable Declarations

Examples

v_job VARCHAR2(9);v_count BINARY_INTEGER := 0;v_total_sal NUMBER(9,2) := 0;v_orderdate DATE := SYSDATE + 7;c_tax_rate CONSTANT NUMBER(3,2) := 8.25;v_valid BOOLEAN NOT NULL := TRUE;

v_job VARCHAR2(9);v_count BINARY_INTEGER := 0;v_total_sal NUMBER(9,2) := 0;v_orderdate DATE := SYSDATE + 7;c_tax_rate CONSTANT NUMBER(3,2) := 8.25;v_valid BOOLEAN NOT NULL := TRUE;

Page 26: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 2626

Controlling PLControlling PL//SQL Flow SQL Flow of Executionof Execution

You can change the logical flow ofstatements using conditional IFstatements and loop control structures .

Conditional IF statements:– IF-THEN-END IF– IF-THEN-ELSE-END IF– IF-THEN-ELSIF-END IF

Page 27: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 2727

IF StatementsIF Statements

Syntax

Simple IF statement:Set the manager ID to 22 if the employee name is Osborne.

IF condition THEN statements;[ELSIF condition THEN statements;][ELSE statements;]END IF;

IF condition THEN statements;[ELSIF condition THEN statements;][ELSE statements;]END IF;

IF v_ename = 'OSBORNE' THEN v_mgr := 22;END IF;

IF v_ename = 'OSBORNE' THEN v_mgr := 22;END IF;

Page 28: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 2828

Simple IF StatementsSimple IF Statements

Set the job title to Salesman, the department number to 35, and the commission to 20% of the current salary if the last name is Miller.

Example. . .IF v_ename = 'MILLER' THEN v_job := 'SALESMAN'; v_deptno := 35; v_new_comm := sal * 0.20; END IF;. . .

. . .IF v_ename = 'MILLER' THEN v_job := 'SALESMAN'; v_deptno := 35; v_new_comm := sal * 0.20; END IF;. . .

Page 29: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 2929

IFIF--THENTHEN--ELSE Statement ELSE Statement Execution FlowExecution Flow

IF conditionIF conditionTRUETRUE

THEN actionsTHEN actions((including further IFsincluding further IFs))

THEN actionsTHEN actions((including further IFsincluding further IFs))

FALSEFALSE

ELSE actionsELSE actions((including further IFsincluding further IFs))

ELSE actionsELSE actions((including further IFsincluding further IFs))

Page 30: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 3030

IFIF--THENTHEN--ELSE StatementsELSE Statements

Set a flag for orders where there are fewer than five days between order date and ship date.

Example...IF v_shipdate - v_orderdate < 5 THEN v_ship_flag := 'Acceptable';ELSE v_ship_flag := 'Unacceptable';END IF;...

...IF v_shipdate - v_orderdate < 5 THEN v_ship_flag := 'Acceptable';ELSE v_ship_flag := 'Unacceptable';END IF;...

Page 31: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 3131

IFIF--THENTHEN--ELSIF ELSIF Statement Execution FlowStatement Execution Flow

IF conditionIF conditionIF conditionIF conditionTRUETRUE

THEN actionsTHEN actionsTHEN actionsTHEN actions

FALSEFALSE

ELSIFELSIFconditioncondition

ELSIFELSIFconditioncondition

TRUETRUE

THEN actionsTHEN actionsTHEN actionsTHEN actions

FALSEFALSE

ELSEELSEactionsactionsELSEELSE

actionsactions

Page 32: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 3232

IFIF--THENTHEN--ELSIF StatementsELSIF Statements

For a given value, calculate a percentage of that value based on a condition.

Example

. . .IF v_start > 100 THEN v_start := 2 * v_start;ELSIF v_start >= 50 THEN v_start := .5 * v_start;ELSE v_start := .1 * v_start;END IF;. . .

. . .IF v_start > 100 THEN v_start := 2 * v_start;ELSIF v_start >= 50 THEN v_start := .5 * v_start;ELSE v_start := .1 * v_start;END IF;. . .

Page 33: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 3333

Building Logical ConditionsBuilding Logical Conditions

• You can handle null values with the IS NULL operator.

• Any arithmetic expression containing a null value evaluates to NULL.

• Concatenated expressions with null values treat null values as an empty string.

Page 34: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 3434

Logic TablesLogic Tables

Build a simple Boolean condition with a comparison operator.

NOT

TRUE

FALSE

NULL

OR

TRUE

FALSE

NULL

TRUE FALSE NULL

FALSE

TRUE

NULL

AND

TRUE

FALSE

NULL

TRUE FALSE NULL

TRUE

NULL NULL

NULL

FALSE FALSE

FALSE

FALSE

FALSE

TRUE

TRUE

TRUE

TRUETRUE

FALSE

NULL NULL

NULL

Page 35: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 3535

Boolean ConditionsBoolean Conditions

What is the value of V_FLAG in each case?

v_flag := v_reorder_flag AND v_available_flag; v_flag := v_reorder_flag AND v_available_flag;

V_REORDER_FLAG V_AVAILABLE_FLAG V_FLAG

TRUE TRUE

TRUE FALSE

NULL TRUE

NULL FALSE

TRUETRUE

FALSEFALSE

NULLNULL

FALSEFALSE

Page 36: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 3636

Iterative ControlIterative Control: : LOOP StatementsLOOP Statements

• Loops repeat a statement or sequence of statements multiple times.

• There are three loop types:– Basic loop– FOR loop– WHILE loop

Page 37: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 3737

Basic LoopBasic Loop

Syntax

LOOP statement1; . . . EXIT [WHEN condition];END LOOP;

LOOP statement1; . . . EXIT [WHEN condition];END LOOP;

where: condition is a Boolean variable or expression (TRUE, FALSE, or NULL);

where: condition is a Boolean variable or expression (TRUE, FALSE, or NULL);

Page 38: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 3838

Basic LoopBasic Loop

Example

DECLARE v_ordid item.ordid%TYPE := 601; v_counter NUMBER(2) := 1;BEGIN LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, v_counter); v_counter := v_counter + 1; EXIT WHEN v_counter > 10; END LOOP;END;

DECLARE v_ordid item.ordid%TYPE := 601; v_counter NUMBER(2) := 1;BEGIN LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, v_counter); v_counter := v_counter + 1; EXIT WHEN v_counter > 10; END LOOP;END;

Page 39: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 3939

FOR LoopFOR Loop

Syntax

• Use a FOR loop to shortcut the test for the number of iterations.

• Do not declare the index; it is declared implicitly.

FOR counter in [REVERSE] lower_bound..upper_bound LOOP statement1; statement2; . . .END LOOP;

FOR counter in [REVERSE] lower_bound..upper_bound LOOP statement1; statement2; . . .END LOOP;

Page 40: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 4040

FOR LoopFOR Loop

Guidelines

• Reference the counter within the loop only; it is undefined outside the loop.

• Use an expression to reference the existing value of a counter.

• Do not reference the counter as the target of an assignment.

Page 41: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 4141

FOR LoopFOR Loop

Insert the first 10 new line items for order number 601.

Example

DECLARE v_ordid item.ordid%TYPE := 601;BEGIN FOR i IN 1..10 LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, i); END LOOP;END;

DECLARE v_ordid item.ordid%TYPE := 601;BEGIN FOR i IN 1..10 LOOP INSERT INTO item(ordid, itemid) VALUES(v_ordid, i); END LOOP;END;

Page 42: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 4242

WHILE LoopWHILE Loop

Syntax

Use the WHILE loop to repeat statements while a condition is TRUE.

WHILE condition LOOP statement1; statement2; . . .END LOOP;

WHILE condition LOOP statement1; statement2; . . .END LOOP;

Condition isCondition isevaluated at the evaluated at the beginning ofbeginning ofeach iterationeach iteration..

Page 43: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 4343

WHILE LoopWHILE Loop

Example

ACCEPT p_new_order PROMPT 'Enter the order number: 'ACCEPT p_items - PROMPT 'Enter the number of items in this order: ' DECLAREv_count NUMBER(2) := 1;BEGIN WHILE v_count <= &p_items LOOP INSERT INTO item (ordid, itemid) VALUES (&p_new_order, v_count); v_count := v_count + 1; END LOOP; COMMIT;END;/

ACCEPT p_new_order PROMPT 'Enter the order number: 'ACCEPT p_items - PROMPT 'Enter the number of items in this order: ' DECLAREv_count NUMBER(2) := 1;BEGIN WHILE v_count <= &p_items LOOP INSERT INTO item (ordid, itemid) VALUES (&p_new_order, v_count); v_count := v_count + 1; END LOOP; COMMIT;END;/

Page 44: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 4444

The The %%TYPE AttributeTYPE Attribute

• 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

Page 45: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 4545

Declaring Variables Declaring Variables with the with the %%TYPE AttributeTYPE Attribute

Examples

... v_ename emp.ename%TYPE; v_balance NUMBER(7,2); v_min_balance v_balance%TYPE := 10;...

... v_ename emp.ename%TYPE; v_balance NUMBER(7,2); v_min_balance v_balance%TYPE := 10;...

Page 46: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 4646

SELECT Statements in PL/SQLSELECT Statements in PL/SQL

Retrieve data from the database with SELECT.

SyntaxSELECT select_listINTO {variable_name[, variable_name]...

| record_name} FROM tableWHERE condition;

SELECT select_listINTO {variable_name[, variable_name]...

| record_name} FROM tableWHERE condition;

Page 47: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 4747

DECLARE v_employee tbl_m_employ

ee%ROWTYPE;BEGIN SELECT * INTO v_employee FROM tbl_m_employee WHERE emp_code=1000

1 ;EXCEPTION W W WW WWWWWWWWWWWWW WW WW DBMS_OUTPUT.PUT_LINE('

No Data Found');END;

Page 48: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 4848

SELECT Statements in PL/SQLSELECT Statements in PL/SQL

The INTO clause is required.

Example

DECLARE v_deptno NUMBER(2); v_loc VARCHAR2(15);BEGIN SELECT deptno, loc INTO v_deptno, v_loc FROM dept WHERE dname = 'SALES'; ...END;

DECLARE v_deptno NUMBER(2); v_loc VARCHAR2(15);BEGIN SELECT deptno, loc INTO v_deptno, v_loc FROM dept WHERE dname = 'SALES'; ...END;

Page 49: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 4949

Retrieving Data in PL/SQLRetrieving Data in PL/SQL

Retrieve the order date and the ship date for the specified order.

Example

DECLARE v_orderdate ord.orderdate%TYPE; v_shipdate ord.shipdate%TYPE; BEGIN SELECT orderdate, shipdate INTO v_orderdate, v_shipdate FROM ord WHERE id = 620;

...END;

DECLARE v_orderdate ord.orderdate%TYPE; v_shipdate ord.shipdate%TYPE; BEGIN SELECT orderdate, shipdate INTO v_orderdate, v_shipdate FROM ord WHERE id = 620;

...END;

Page 50: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 5050

Retrieving Data in PL/SQLRetrieving Data in PL/SQL

Return the sum of the salaries for all employees in the specified department.

Example

DECLARE v_sum_sal emp.sal%TYPE; v_deptno NUMBER NOT NULL := 10; BEGIN SELECT SUM(sal) -- group function INTO v_sum_sal FROM emp WHERE deptno = v_deptno;END;

DECLARE v_sum_sal emp.sal%TYPE; v_deptno NUMBER NOT NULL := 10; BEGIN SELECT SUM(sal) -- group function INTO v_sum_sal FROM emp WHERE deptno = v_deptno;END;

Page 51: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 5151

Manipulating Data Using PL/SQLManipulating Data Using PL/SQL

Make changes to database tables by using DML commands:

– INSERT– UPDATE– DELETE

INSERT

UPDATE

DELETE

Page 52: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 5252

Inserting DataInserting Data

Add new employee information to the emp table.

ExampleBEGIN INSERT INTO emp(empno, ename, job, deptno) VALUES(empno_seq.NEXTVAL, 'HARDING',

'CLERK', 10);END;

BEGIN INSERT INTO emp(empno, ename, job, deptno) VALUES(empno_seq.NEXTVAL, 'HARDING',

'CLERK', 10);END;

Page 53: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 5353

Inserting DataInserting Data

Same example using a procedure

CREATE OR REPLACE PROCEDURE new_emp (en emp.ename%TYPE, jt emp.job%TYPE, sl emp.sal%TYPE, dn emp.deptno%TYPE) IS msg VARCHAR2(30);BEGIN INSERT INTO emp (empno, ename, job, sal, deptno) VALUES (empno_seq.NEXTVAL, en, jt, sl, dn); msg := (SQL%ROWCOUNT || ' row inserted '); DBMS_OUTPUT.PUT_LINE(msg);END;/

CREATE OR REPLACE PROCEDURE new_emp (en emp.ename%TYPE, jt emp.job%TYPE, sl emp.sal%TYPE, dn emp.deptno%TYPE) IS msg VARCHAR2(30);BEGIN INSERT INTO emp (empno, ename, job, sal, deptno) VALUES (empno_seq.NEXTVAL, en, jt, sl, dn); msg := (SQL%ROWCOUNT || ' row inserted '); DBMS_OUTPUT.PUT_LINE(msg);END;/

Page 54: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 5454

Updating DataUpdating Data

Increase the salary of all employees in the emp table who are Analysts.

Example

DECLARE v_sal_increase emp.sal%TYPE := 2000; BEGIN UPDATE emp SET sal = sal + v_sal_increase WHERE job = 'ANALYST';END;

Page 55: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 5555

Deleting DataDeleting Data

Delete rows that belong to department 10 from the emp table.

Example

DECLARE v_deptno emp.deptno%TYPE := 10; BEGIN

DELETE FROM emp WHERE deptno = v_deptno;END;

DECLARE v_deptno emp.deptno%TYPE := 10; BEGIN

DELETE FROM emp WHERE deptno = v_deptno;END;

Page 56: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 5656

SQL CursorSQL Cursor

• A cursor is a private SQL work area.

• There are two types of cursors:– Implicit cursors– Explicit cursors

• The Oracle Server uses implicit cursors to parse and execute your SQL statements.

• Explicit cursors are explicitly declared by the programmer.

Page 57: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 5757

SQL Cursor AttributesSQL Cursor Attributes

Using SQL cursor attributes, you can test the outcome of your SQL statements.

SQL%ROWCOUNT Number of rows affected by the most recent SQL statement (an integer value)

SQL%FOUND Boolean attribute that evaluates to TRUE if the most recent SQL statement affects one or more rows

SQL%NOTFOUND Boolean attribute that evaluates to TRUE if the most recent SQLstatement does not affect any rows

SQL%ISOPEN Always evaluates to FALSE because PL/SQL closes implicit cursorsimmediately after they are executed

Page 58: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 5858

วิ�ธี�ใช้ Explicit Cursorการใช้� explicit cursor ม 4 ขั้��น์ตอน์1) ประกาศ cursor (Declare cursor)2) เป�ด cursor (Open cursor)3) ด�งขั้�อม�ลจาก cursor คร��งละ 1 row

(Fetch data from cursor)4) ป�ด cursor (Close cursor)

Page 59: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 5959

DECLARE1) CURSOR cs_emp IS

SELECT emp_name, salary FROM tbl_m_employee;v_emp_name tbl_m_employee.emp_name%TYPE;v_salary tbl_m_employee.salary%TYPE;

BEGIN2) OPEN cs_emp;LOOP3) FETCH cs_emp INTO v_emp_name, v_salary; EXIT WHEN cs_emp%NOTFOUND; DBMS_OUTPUT.PUT_LINE(v_emp_name||' : '||

v_salary); END LOOP;4) CLOSE cs_emp; END;

ตั�วิอย่�าง : แสดงเง�นเด�อนพน�กงานทุ�กคนโดย่ใช้ Explicit cursor

Page 60: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 6060

ผลล�พธี�EMP_01 :

25000EMP_02 :

78000EMP_03 :

11000EMP_04 :

8000EMP_05 :

43000

Page 61: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 6161

SQL Cursor AttributesSQL Cursor Attributes

Delete rows that have the specified order number from the ITEM table. Print the number of rows deleted.

ExampleVARIABLE rows_deleted VARCHAR2(30)DECLARE v_ordid NUMBER := 605;BEGIN DELETE FROM item WHERE ordid = v_ordid; :rows_deleted := (SQL%ROWCOUNT || ' rows deleted.');END;/PRINT rows_deleted

Page 62: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 6262

Handling Exceptions with PLHandling Exceptions with PL//SQLSQL

• What is an exception?– Identifier in PL/SQL that is raised

during execution• How is it raised?

– An Oracle error occurs.– You raise it explicitly.

• How do you handle it?– Trap it with a handler.– Propagate it to the calling

environment.

Page 63: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 6363

Handling ExceptionsHandling Exceptions

Trap the exceptionTrap the exception

DECLAREDECLARE

BEGINBEGIN

END;END;

Exception Exception is raisedis raised

EXCEPTIONEXCEPTION

Exception Exception is trappedis trapped

Propagate the exceptionPropagate the exception

DECLAREDECLARE

BEGINBEGIN

END;END;

Exception Exception is raisedis raised

EXCEPTIONEXCEPTION

Exception isException isnot trappednot trapped

Exception Exception propagates to calling propagates to calling

environmentenvironment

Page 64: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 6464

Trapping ExceptionsTrapping Exceptions

Syntax

EXCEPTION WHEN exception1 [OR exception2 . . .] THEN statement1; statement2; . . . [WHEN exception3 [OR exception4 . . .] THEN statement1; statement2; . . .] [WHEN OTHERS THEN statement1; statement2; . . .]

EXCEPTION WHEN exception1 [OR exception2 . . .] THEN statement1; statement2; . . . [WHEN exception3 [OR exception4 . . .] THEN statement1; statement2; . . .] [WHEN OTHERS THEN statement1; statement2; . . .]

Page 65: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 6565

Trapping Predefined Trapping Predefined Oracle Server ErrorsOracle Server Errors

• Reference the standard name in the exception-handling routine.

• Sample predefined exceptions :– NO_DATA_FOUND– TOO_MANY_ROWS– INVALID_CURSOR– ZERO_DIVIDE– DUP_VAL_ON_INDEX

Page 66: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 6666

Predefined ExceptionPredefined Exception

BEGIN SELECT ... COMMIT;EXCEPTION WHEN NO_DATA_FOUND THEN statement1; statement2;

WHEN TOO_MANY_ROWS THEN statement1; WHEN OTHERS THEN statement1; statement2; statement3;END;

Page 67: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 6767

แบบฝึ"กหั�ดแบบฝึ"กหั�ด

1. สร�าง anonymous block เพื่$%อแสดง “My PL/SQL Block Works” บูน์ screen.

My PL/SQL Block Works

2. สร�าง anonymous block เพื่$%อค'าน์วัณ์ x:=2; y:=4; z= x+y; แสดงผลล�พื่ธี� z3.จากขั้�อ 3 ใช้� IF ก'าหน์ดเง$%อน์ไขั้ถ้�า z มากกวั,า 3

ให�แสดงขั้�อควัาม z > 3 ถ้�าไม,ให�แสดง z <=3

Page 68: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 6868

4. จงสร�างโปรแกรม PL/SQL ที่ %ที่'าการพื่/มพื่� employee_id ,first_name และsalary ขั้องพื่น์�กงาน์ที่ %ม รห�สพื่น์�กงาน์ 100 จาก Table ช้$%อ EMPLOYEES [ด�ต�วัอย่,างที่ % 9]

5. จงสร�างโปรแกรม PL/SQL ที่ %ที่'าการพื่/มพื่� employee_id ,first_name และsalary ขั้องพื่น์�กงาน์ที่1กคน์จาก Table ช้$%อ EMPLOYEES [ใช้� cursor ด�ต�วัอย่,างที่ % 10]

6. จากขั้�อ 5 แสดงเฉพื่าะพื่น์�กงาน์ที่ %ม เง/น์เด$อน์มากกวั,า 8000 [ใช้� if ]7. จากขั้�อ 5. ที่'าการแก�ไขั้เง/น์เด$อน์ขั้องพื่น์�กงาน์ที่ % น์�อย่กวั,า

10000ปร�บูค,าให�เป3น์ 10000 และมากกวั,า 15000 ปร�บูให�เหล$อ

15000[ด�ต�วัอย่,างที่ % 14]

Page 69: Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ prawat237@yahoo.com Lecture by

June 25, 2006June 25, 2006Aj.Winyou Aj.Winyou NiranartlamphongNiranartlamphong

Week 9 Introduction to PL-SQL.,

Week 9 Introduction to PL-SQL., Week 9 Introduction to PL-SQL., page page 6969

แบบฝึ"กหั�ดแบบฝึ"กหั�ด

8.สร�างตารางช้$%อ Emp (empid,fname,salary)

น์'าขั้�อม�ลจากตาราง Employees เฉพื่าะพื่น์�กงาน์ที่ %ม เง/น์เด$อน์ มากกวั,า 5000 โดย่ก'าหน์ดให�

Empid= 1001 บูวักเพื่/%มที่ ละ 1Fname = first__name+last_nameSalay =salary +1000