30
© 2007 by Prentice Hall 7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

Embed Size (px)

Citation preview

Page 1: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-1

Introduction to Oracle 10gChapter 7Using PL/SQL to Your Advantage

James Perry and Gerald Post

Page 2: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-2

Chapter Outline

• Introducing PL/SQL• Understanding Anonymous Blocks• Creating Anonymous Blocks• Understanding Explicit Cursors• Introducing Named Blocks• Creating, Using, Listing, and Dropping

Functions• Creating Procedures

Page 3: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-3

Table 7.1

Oracle Error Code

Meaning

ORA-0001 Unique constraint violated.

ORA-1001 Illegal cursor operation.

ORA-1403 No data found.

ORA-1422 A SELECT INTO statement returns more than one row.

ORA-1476 Division by zero has occurred.

ORA-1722 Conversion to a number failed; (Attempt to convert a character string, for instance.)

ORA-6502 Truncation, conversion error, or arithmetic error.

ORA-6511 Attempt to open a cursor that is already open.

Page 4: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-4

Table 7.2

Exception name to test

Meaning

ACCESS_INTO_NULL Attempted to assign a value to an attribute of an uninitialized object.

CASE_NOT_FOUND None of the WHEN clauses of a CASE structure was selected and there was no ELSE default clause.

CURSOR_ALREADY_OPEN

Program tried to open a cursor that is already open.

INVALID_CURSOR Program tried to perform an illegal cursor activity such as closing an open cursor.

NO_DATA_FOUND A SELECT INTO statement returns zero rows.

ROWTYPE_MISMATCH A cursor variable and a PL/SQL variable have incompatible data types.

TOO_MANY_ROWS A SELECT INTO statement returned more than one row.

VALUE_ERROR A conversion, arithmetic, truncation, or size-constraint error occurred. (For example, an attempt to select a long character column into a shorter PL/SQL variable causes this exception.)

ZERO_DIVIDE Attempted to divide a number by zero.

Page 5: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-5

Table 7.3

Base Salary to Dependent Ratio

Base Salary Increase

$0 – $1,000 10%

$1,001 – $1,500 8%

$1,501 – $2,000 6%

$2,001 – $3,000 3%

Above $3,000 No increase

Page 6: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-6

7.1 PL/SQL processing

Page 7: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-7

7.2 Example PL/SQL block

Page 8: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-8

7.3 Initializing the Redwood Realty database

Page 9: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-9

7.4 An anonymous PL/SQL block

Page 10: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-10

7.5 Executing an anonymous PL/SQL block

Page 11: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-11

error message displayed by the exception-handling code

SQLERRM is the text of the error message

7.6 A run-time error using an implicit cursor

Page 12: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-12

two hyphens turn the SELECT statement into a comment

7.7 Exploring exception handling

Page 13: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-13

7.8 A completed PL/SQL block containing an explicit cursor

Page 14: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-14

7.9 Testing an anonymous PL/SQL block containing an explicit cursor

Page 15: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-15

cursor definition

cursor FOR loop

7.10 Using a cursor FOR loop with an explicit cursor

Page 16: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-16

TrueFalse Condition?

<statements> <statements>

enter

exit

7.11 IF statement logic

Page 17: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-17

SalaryRatio <= 1000

SalaryRatio <= 1500

SalaryRatio <= 2000

SalaryRatio <= 3000

Increment by 10%

Increment by 8%

Increment by 6%

Increment by 3% (no increment)

Enter

True

True

True

True

False

False

False

False

Exit

7.12 Business rules logic diagram

Page 18: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-18

7.13 Implementing a DML statement in a PL/SQL code block

Page 19: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-19

7.14 Messages indicate the salary increment for each agent

Page 20: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-20

7.15 Displaying compile-time error information.

Page 21: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-21

7.16 Calling a user-defined function

Page 22: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-22

7.17 Error messages indicating an invalid AgentID value

Page 23: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-23

new code line

new code lines

new code line

7.18 Modified Age function

Page 24: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-24

modified Age function

testing the Age function with good and bad data values

7.19 Testing the modified Age function

Page 25: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-25

User_Source description

Age function source code stored in the database

7.20 Displaying User_Source’s structure and information about a stored function

Page 26: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-26

7.21 Completed ForSale source code (shown in Notepad)

Page 27: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-27

7.22 Message indicating the procedure is syntax-error free

Page 28: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-28

call to ForSale without an argument and resulting error messages

7.23 Testing the ForSale procedure

Page 29: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-29

new line

new line

three new lines

7.24 Testing the newly modified ForSale procedure

Page 30: © 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post

© 2007 by Prentice Hall7-30

7.25 Displaying a procedure’s source code