© 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
© 2007 by Prentice Hall7-3 Table 7.1 Oracle Error CodeMeaning ORA-0001Unique constraint violated. ORA-1001Illegal cursor operation. ORA-1403No data found. ORA-1422A SELECT INTO statement returns more than one row. ORA-1476Division by zero has occurred. ORA-1722Conversion to a number failed; (Attempt to convert a character string, for instance.) ORA-6502Truncation, conversion error, or arithmetic error. ORA-6511Attempt to open a cursor that is already open.
© 2007 by Prentice Hall7-4 Table 7.2 Exception name to testMeaning ACCESS_INTO_NULLAttempted to assign a value to an attribute of an uninitialized object. CASE_NOT_FOUNDNone of the WHEN clauses of a CASE structure was selected and there was no ELSE default clause. CURSOR_ALREADY_OPENProgram tried to open a cursor that is already open. INVALID_CURSORProgram tried to perform an illegal cursor activity such as closing an open cursor. NO_DATA_FOUNDA SELECT INTO statement returns zero rows. ROWTYPE_MISMATCHA cursor variable and a PL/SQL variable have incompatible data types. TOO_MANY_ROWSA SELECT INTO statement returned more than one row. VALUE_ERRORA 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_DIVIDEAttempted to divide a number by zero.
© 2007 by Prentice Hall7-5 Table 7.3 Base Salary to Dependent RatioBase Salary Increase $0 – $1,00010% $1,001 – $1,5008% $1,501 – $2,0006% $2,001 – $3,0003% Above $3,000No increase
© 2007 by Prentice Hall PL/SQL processing
© 2007 by Prentice Hall Example PL/SQL block
© 2007 by Prentice Hall Initializing the Redwood Realty database
© 2007 by Prentice Hall An anonymous PL/SQL block
© 2007 by Prentice Hall Executing an anonymous PL/SQL block
© 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
© 2007 by Prentice Hall7-12 two hyphens turn the SELECT statement into a comment 7.7 Exploring exception handling
© 2007 by Prentice Hall A completed PL/SQL block containing an explicit cursor
© 2007 by Prentice Hall Testing an anonymous PL/SQL block containing an explicit cursor
© 2007 by Prentice Hall7-15 cursor definition cursor FOR loop 7.10 Using a cursor FOR loop with an explicit cursor
© 2007 by Prentice Hall7-16 TrueFalseCondition ? enter exit 7.11 IF statement logic
© 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 False Exit 7.12 Business rules logic diagram
© 2007 by Prentice Hall Implementing a DML statement in a PL/SQL code block
© 2007 by Prentice Hall Messages indicate the salary increment for each agent
© 2007 by Prentice Hall Displaying compile-time error information.
© 2007 by Prentice Hall Calling a user-defined function
© 2007 by Prentice Hall Error messages indicating an invalid AgentID value
© 2007 by Prentice Hall7-23 new code line new code lines new code line 7.18 Modified Age function
© 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
© 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
© 2007 by Prentice Hall Completed ForSale source code (shown in Notepad)
© 2007 by Prentice Hall Message indicating the procedure is syntax-error free
© 2007 by Prentice Hall7-28 call to ForSale without an argument and resulting error messages 7.23 Testing the ForSale procedure
© 2007 by Prentice Hall7-29 new line three new lines 7.24 Testing the newly modified ForSale procedure
© 2007 by Prentice Hall Displaying a procedure’s source code