6- oracle database 11g develop plsql program units vol 2

465
Oracle Database 11g: Develop PL/SQL Program Units Volume 2 Student Guide D49986GC20 Edition 2.0 September 2009 D63066

Upload: trapte

Post on 10-Mar-2015

1.469 views

Category:

Documents


182 download

TRANSCRIPT

Page 1: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program UnitsVolume 2 • Student Guide

D49986GC20

Edition 2.0

September 2009

D63066

Page 2: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Copyright © 2009, Oracle. All rights reserved.

Disclaimer

This document contains proprietary information and is protected by copyright and other intellectual property laws. You may copy and print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way. Except where your use constitutes "fair use" under copyright law, you may not use, share, download, upload, copy, print, display, perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization of Oracle.

The information contained in this document is subject to change without notice. If you find any problems in the document, please report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not warranted to be error-free.

Restricted Rights Notice

If this documentation is delivered to the United States Government or anyone using the documentation on behalf of the United States Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTSThe U.S. Government’s rights to use, modify, reproduce, release, perform, display, or disclose these training materials are restricted by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract.

Trademark Notice

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

AuthorLauran Serhal

Technical Contributors and Reviewers Anjulaponni AzhagulekshmiChristian BauwensChristoph BurandtZarko CesljasYanti ChangSalome ClementLaszlo CzinkoczkiIngrid DelaHayeSteve FriedbergLaura GarzaJoel GoodmanNancy GreenbergManish PawarBrian PottleHelen RobertsonTulika SrivastavaTed Witiuk

EditorsArijit GhoshRaj Kumar

PublishersPavithran AdkaSheryl Domingue

Page 3: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Copyright © 2009, Oracle. All rights reserved.

Using the PL/SQL Compiler

Page 4: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 2

Copyright © 2009, Oracle. All rights reserved.10 - 2

Objectives

After completing this lesson, you should be able to do the following:• Use the PL/SQL compiler initialization parameters• Use the PL/SQL compile-time warnings

Page 5: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 3

Copyright © 2009, Oracle. All rights reserved.10 - 3

Lesson Agenda

• Using the PLSQL_CODE_TYPE and PLSQL_OPTIMIZE_LEVEL PL/SQL compilation initialization parameters

• Using the PL/SQL compile-time warnings:– Using the PLSQL_WARNING initialization parameter– Using the DBMS_WARNING package subprograms

Page 6: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 4

Copyright © 2009, Oracle. All rights reserved.10 - 4

Initialization Parameters for PL/SQL Compilation

• PLSQL_CODE_TYPE• PLSQL_OPTIMIZE_LEVEL• PLSQL_CCFLAGS• PLSQL_WARNINGS

Initialization Parameters for PL/SQL Compilation In releases before Oracle Database 10g, the PL/SQL compiler translated your code to machine code without applying many changes for performance. Now, PL/SQL uses an optimizing compiler that can rearrange code for better performance. You do not need to do anything to get the benefits of this new optimizer; it is enabled by default. Note

• The PLSQL_CCFLAGS initialization parameter is covered in the lesson titled “Managing PL/SQL Code.”

• The PLSQL_WARNINGS initialization parameter is covered later in this lesson.

Page 7: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 5

Copyright © 2009, Oracle. All rights reserved.10 - 5

Using the Initialization Parameters for PL/SQL Compilation

• PLSQL_CODE_TYPE: Specifies the compilation mode for PL/SQL library units

• PLSQL_OPTIMIZE_LEVEL: Specifies the optimization level to be used to compile PL/SQL library units

PLSQL_CODE_TYPE = { INTERPRETED | NATIVE }

PLSQL_OPTIMIZE_LEVEL = { 0 | 1 | 2 | 3}

Using the Initialization Parameters for PL/SQL CompilationThe PLSQL_CODE_TYPE ParameterThis parameter specifies the compilation mode for PL/SQL library units. If you choose INTERPRETED, PL/SQL library units will be compiled to PL/SQL bytecode format. Such modules are executed by the PL/SQL interpreter engine. If you choose NATIVE, PL/SQL library units (with the possible exception of top-level anonymous PL/SQL blocks) will be compiled to native (machine) code. Such modules will be executed natively without incurring any interpreter overhead. When the value of this parameter is changed, it has no effect on PL/SQL library units that have already been compiled. The value of this parameter is stored persistently with each library unit. If a PL/SQL library unit is compiled natively, all subsequent automatic recompilations of that library unit will use native compilation. In Oracle Database 11g, native compilation is easier and more integrated, with fewer initialization parameters to set.In rare cases, if the overhead of the optimizer makes compilation of very large applications take too long, you might lower the optimization level by setting the initialization parameter PLSQL_OPTIMIZE_LEVEL to 1 instead of its default value 2. In even rarer cases, you might see a change in exception behavior—either an exception that is not raised at all or one that is raised earlier than expected. Setting PLSQL_OPTIMIZE_LEVEL to 0 prevents the code from being rearranged at all.

Page 8: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 6

Using the Initialization Parameters for PL/SQL Compilation (continued)The PLSQL_OPTIMIZE_LEVEL ParameterThis parameter specifies the optimization level that will be used to compile PL/SQL library units. The higher the setting of this parameter, the more effort the compiler makes to optimize PL/SQL library units. The available values are (0, 1, and 2 were available starting with Oracle 10g release 2): 0: Maintains the evaluation order and hence the pattern of side effects, exceptions, and package initializations of Oracle9i and earlier releases. Also removes the new semantic identity ofBINARY_INTEGER and PLS_INTEGER and restores the earlier rules for the evaluation of integer expressions. Although code will run somewhat faster than it did in Oracle9i, use of level 0 will forfeit most of the performance gains of PL/SQL starting with Oracle Database 10g.1: Applies a wide range of optimizations to PL/SQL programs including the elimination of unnecessary computations and exceptions, but generally does not move source code out of its original source order2: Applies a wide range of modern optimization techniques beyond those of level 1 including changes which may move source code relatively far from its original location3: This value is new in Oracle Database 11g. It applies a wide range of optimization techniques beyond those of level 2, automatically including techniques not specifically requested. This enables procedure inlining, which is an optimization process that replaces procedure calls with a copy of the body of the procedure to be called. The copied procedure almost always runs faster than the original call. To allow subprogram inlining, either accept the default value of the PLSQL_OPTIMIZE_LEVEL initialization parameter (which is 2) or set it to 3. With PLSQL_OPTIMIZE_LEVEL = 2, you must specify each subprogram to be inlined. With PLSQL_OPTIMIZE_LEVEL = 3, the PL/SQL compiler seeks opportunities to inline subprograms beyond those that you specify.Note: For additional information about inlining, refer to the Oracle Database PL/SQL Language Reference 11g Release 2 (11.2) guide and the Oracle Database 11g Advanced PL/SQL instructor-led course.Generally, setting this parameter to 2 pays off in terms of better execution performance. If, however, the compiler runs slowly on a particular source module or if optimization does not make sense for some reason (for example, during rapid turnaround development), then setting this parameter to 1 results in almost as good a compilation with less use of compile-time resources. The value of this parameter is stored persistently with the library unit.NoteThe PLSQL_CODE_TYPE parameter in Oracle Database 10g replaced the following obsolete parameters:• PLSQL_NATIVE_C_COMPILER• PLSQL_NATIVE_MAKE_FILE_NAME• PLSQL_NATIVE_C_COMPILER• PLSQL_NATIVE_MAKE_UTILITY• PLSQL_NATIVE_LINKER

The PLSQL_DEBUG parameter is deprecated in Oracle Database 11g. The parameter PLSQL_DEBUG no longer controls the generation of debugging information by the PL/SQL compiler; debugging information is always generated and no special parameter is needed.

Page 9: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 7

Copyright © 2009, Oracle. All rights reserved.10 - 7

The Compiler Settings

Enables or disables the reporting of warning messages by the PL/SQL compiler.

PLSQL_WARNINGS

Specifies the optimization level to be used to compile PL/SQL library units.

PLSQL_OPTIMIZE_LEVEL

PLSQL_CCFLAGS

PLSQL_CODE_TYPECompiler Option

Specifies the compilation mode for PL/SQL library units.

Controls conditional compilation of each PL/SQL library unit independently.

Description

In general, for the fastest performance, use the following setting:

PLSQL_CODE_TYPE = NATIVEPLSQL_OPTIMIZE_LEVEL = 2

The Compiler SettingsThe new compiler increases the performance of PL/SQL code and allows it to execute approximately two times faster than an Oracle8i database and 1.5 times to 1.75 times as fast as Oracle9i Database Release 2.To get the fastest performance, the compiler setting must be:

PLSQL_CODE_TYPE = NATIVEPLSQL_OPTIMIZE_LEVEL = 2

Page 10: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 8

Copyright © 2009, Oracle. All rights reserved.10 - 8

Displaying the PL/SQL Initialization Parameters

Use the USER|ALL|DBA_PLSQL_OBJECT_SETTINGS data dictionary views to display the settings for a PL/SQL object:

DESCRIBE USER_PLSQL_OBJECT_SETTINGS

Displaying the PL/SQL Initialization ParametersThe columns of the USER_PLSQL_OBJECTS_SETTINGS data dictionary view are: Owner: The owner of the object. This column is not displayed in the USER_PLSQL_OBJECTS_SETTINGS view.Name: The name of the objectType: The available choices are: PROCEDURE, FUNCTION, PACKAGE, PACKAGE BODY,TRIGGER, TYPE, or TYPE BODY.PLSQL_OPTIMIZE_LEVEL: The optimization level that was used to compile the objectPLSQL_CODE_TYPE: The compilation mode for the objectPLSQL_DEBUG: Specifies whether or not the object was compiled for debuggingPLSQL_WARNINGS: The compiler warning settings used to compile the objectNLS_LENGTH_SEMANTICS: The NLS length semantics used to compile the objectPLSQL_CCFLAGS: The conditional compilation flag used to compile the objectPLSCOPE_SETTINGS: Controls the compile-time collection, cross reference, and storage of PL/SQL source code identifier data (new in Oracle Database 11g)

Page 11: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 9

Copyright © 2009, Oracle. All rights reserved.10 - 9

Displaying and Setting the PL/SQL Initialization Parameters

• Set the compiler initialization parameter’s value using the ALTER SYSTEM or ALTER SESSION statements.

• The parameters’ values are accessed when the CREATEOR REPLACE statement is executed.

SELECT name, type, plsql_code_type AS CODE_TYPE, plsql_optimize_level AS OPT_LVL

FROM user_plsql_object_settings;

. . .

Note• For additional information about the ALTER SYSTEM or ALTER SESSION statements,

refer to the Oracle Database SQL Reference 11g Release 2 (11.2) Guide.• The DBA_STORED_SETTINGS data dictionary view family is deprecated in Oracle

Database 10g and is replaced with the DBA_PLSQL_OBJECT_SETTINGS data dictionary view family.

Page 12: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 10

Copyright © 2009, Oracle. All rights reserved.10 - 10

Changing PL/SQL Initialization Parameters: Example

ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL = 1;ALTER SESSION SET PLSQL_CODE_TYPE = 'NATIVE';

-- code displayed in the notes pageCREATE OR REPLACE PROCEDURE add_job_history. . .

. . .

@code_10_10_s.sql

Changing PL/SQL Initialization Parameters: ExampleTo change a compiled PL/SQL object from interpreted code type to native code type, you must first set the PLSQL_CODE_TYPE parameter to NATIVE (optionally set the other parameters) and then, recompile the program. To enforce native compilation to all PL/SQL code, you must recompile each one. Scripts (in the rdmbs/admin directory) are provided for you to achieve conversion to full native compilation (dbmsupgnv.sql) or full interpreted compilation (dbmsupgin.sql). The add_job_history procedure is created as follows:

CREATE OR REPLACE PROCEDURE add_job_history( p_emp_id job_history.employee_id%type, p_start_date job_history.start_date%type, p_end_date job_history.end_date%type, p_job_id job_history.job_id%type, p_department_id job_history.department_id%type )

ISBEGININSERT INTO job_history (employee_id, start_date,

end_date, job_id, department_id)VALUES(p_emp_id, p_start_date, p_end_date,

p_job_id, p_department_id);END add_job_history;

/

Page 13: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 11

Copyright © 2009, Oracle. All rights reserved.10 - 11

Lesson Agenda

• Using the PLSQL_CODE_TYPE and PLSQL_OPTIMIZE_LEVEL PL/SQL compilation initialization parameters

• Using the PL/SQL compile-time warnings:– Using the PLSQL_WARNING initialization parameter– Using the DBMS_WARNING package subprograms

Page 14: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 12

Copyright © 2009, Oracle. All rights reserved.10 - 12

Overview of PL/SQL Compile-Time Warnings for Subprograms

Starting with Oracle 10g, the PL/SQL compiler has been enhanced to produce warnings for subprograms.

Enable warnings View warningsCompile program

Overview of PL/SQL Compile-Time Warnings for SubprogramsTo make your programs more robust and avoid problems at run time, you can turn on checking for certain warning conditions. These conditions are not serious enough to produce an error and keep you from compiling a subprogram. They may point out something in the subprogram that produces an undefined result or might create a performance problem.In releases prior to Oracle Database 10g, compiling a PL/SQL program had two possible outcomes:

• Success, producing a valid compiled unit• Failure, with compilation errors indicating that the program had either syntax or semantic

errorsHowever, even when compilation of a program was successful, the program may have violated recommended best practices or could have been coded to be more efficient. Oracle Database 10gintroduced a new ease-of-use feature that allows the PL/SQL compiler to communicate warning messages in these situations. Compiler warnings allow developers to avoid common coding pitfalls, thus improving productivity.

Page 15: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 13

Overview of PL/SQL Compile-Time Warnings for Subprograms (continued)PL/SQL supports passing of IN OUT and OUT parameters by value or by reference through the NOCOPY compiler hint. Passing parameters by value is inherently less efficient because it involves making multiple copies of the data. With Oracle Database 11g, the compiler automatically detects and recommends the use of the NOCOPY hint, where the parameter types are large object, record, or collection types. With the PL/SQL compiler-warning feature, compiling a PL/SQL program could have additional possible outcomes:

• Success with compilation warnings• Failure with compilation errors and compilation warnings

Note that the compiler may issue warning messages even on a successful compile. A compilation error must be corrected to be able to use the stored procedure whereas a warning is for informational purposes.Examples of warning messagesSP2-0804: Procedure created with compilation warningsPLW-07203: Parameter 'IO_TBL' may benefit from use of the NOCOPY compiler hint

Page 16: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 14

Copyright © 2009, Oracle. All rights reserved.10 - 14

Benefits of Compiler Warnings

• Make programs more robust and avoid problems at run time

• Identify potential performance problems• Identify factors that produce undefined results

Benefits of Compiler WarningsUsing compiler warnings can help you to:

• Make your programs more robust and avoid problems at run time• Identify potential performance problems• Identify factors that produce undefined results

Note• You can enable checking for certain warning conditions when these conditions are not

serious enough to produce an error and keep you from compiling a subprogram.• Warning messages can be issued during compilation of PL/SQL subprograms; anonymous

blocks do not produce any warnings.• All PL/SQL warning messages use the prefix PLW.

Page 17: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 15

Copyright © 2009, Oracle. All rights reserved.10 - 15

Categories of PL/SQL Compile-Time Warning Messages

SEVERE PERFORMANCE

INFORMATIONAL ALL

Categories of PL/SQL Compile-Time Warning MessagesPL/SQL warning messages are divided into categories, so that you can suppress or display groups of similar warnings during compilation. The categories are:• SEVERE: Messages for conditions that may cause unexpected behavior or wrong results,

such as aliasing problems with parameters• PERFORMANCE: Messages for conditions that may cause performance problems, such as

passing a VARCHAR2 value to a NUMBER column in an INSERT statement• INFORMATIONAL: Messages for conditions that do not have an effect on performance or

correctness, but that you may want to change to make the code more maintainable, such as unreachable code that can never be executed

Page 18: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 16

Copyright © 2009, Oracle. All rights reserved.10 - 16

Setting the Warning Messages Levels

You can set warning levels using one of the following methods:• Declaratively:

– Using the PLSQL_WARNINGS initialization parameter• Programmatically:

– Using the DBMS_WARNING package

PLSQL_WARNINGSinitialization parameter

DBMS_WARNINGpackage

Setting the Warning Messages LevelsYou can set the compiler warning messages levels using one of the following methods: Using the PLSQL_WARNINGS Initialization ParameterThe PLSQL_WARNINGS setting enables or disables the reporting of warning messages by the PL/SQL compiler, and specifies which warning messages to show as errors. The settings for the PLSQL_WARNINGS parameter are stored along with each compiled subprogram. You can use the PLSQL_WARNINGS initialization parameter to do the following:

• Enable or disable the reporting of all warnings, warnings of a selected category, or specific warning messages.

• Treat all warnings, a selected category of warning, or specific warning messages as errors.• Any valid combination of the preceding

The keyword All is a shorthand way to refer to all warning messages: SEVERE, PERFORMANCE, and INFORMATIONAL.Using the DBMS_WARNING PackageThe DBMS_WARNING package provides a way to manipulate the behavior of PL/SQL warning messages, in particular by reading and changing the setting of the PLSQL_WARNINGSinitialization parameter to control what kinds of warnings are suppressed, displayed, or treated as errors. This package provides the interface to query, modify, and delete current system or session settings. This package is covered later in this lesson.

Page 19: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 17

Copyright © 2009, Oracle. All rights reserved.10 - 17

Setting Compiler Warning Levels:Using PLSQL_WARNINGS

ALTER [SESSION|SYSTEM]PLSQL_WARNINGS = 'value_clause1'[ ,'value_clause2']...

Qualifier Value = { ENABLE | DISABLE | ERROR }

Modifier Value = { ALL | SEVERE | INFORMATIONAL | PERFORMANCE | { integer | (integer [, integer ] ...) } }

value_clause = Qualifier Value : Modifier Value

Modifying Compiler Warning SettingsThe parameter value comprises a comma-separated list of quoted qualifier and modifier keywords, where the keywords are separated by colons. The qualifier values are: ENABLE, DISABLE, and ERROR. The modifier value ALL applies to all warning messages. SEVERE, INFORMATIONAL, and PERFORMANCE apply to messages in their own category, and an integer list for specific warning messages. Possible values for ENABLE, DISABLE, and ERROR: • ALL• SEVERE• INFORMATIONAL• PERFORMANCE• numeric_value

Values for numeric_value are in:• Range 5000-5999 for severe • Range 6000-6249 for informational• Range 7000-7249 for performance

Page 20: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 18

Copyright © 2009, Oracle. All rights reserved.10 - 18

Setting Compiler Warning Levels: Using PLSQL_WARNINGS, Examples

ALTER SESSION SET plsql_warnings = 'enable:severe',

'enable:performance', 'disable:informational';

ALTER SESSION SET plsql_warnings = 'enable:severe';

ALTER SESSION SET PLSQL_WARNINGS='ENABLE:SEVERE', 'DISABLE:PERFORMANCE' , 'ERROR:05003';

Setting Compiler Warning Levels: Using PLSQL_WARNINGS, ExamplesYou can use the ALTER SESSION or ALTER SYSTEM command to change the PLSQL_WARNINGS initialization parameter. The graphic in the slide shows the various examples of enabling and disabling compiler warnings.Example 1In this example, you are enabling SEVERE and PERFORMANCE warnings and disabling INFORMATIONAL warnings.Example 2In the second example, you are enabling only SEVERE warnings.Example 3You can also treat particular messages as errors instead of warnings. In this example, if you know that the warning message PLW-05003 represents a serious problem in your code, including 'ERROR:05003' in the PLSQL_WARNINGS setting makes that condition trigger an error message (PLS_05003) instead of a warning message. An error message causes the compilation to fail. In this example, you are also disabling PERFORMANCE warnings.

Page 21: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 19

Copyright © 2009, Oracle. All rights reserved.10 - 19

Setting Compiler Warning Levels: Using PLSQL_WARNINGS in SQL Developer

Setting Compiler Warning Levels: Using PLSQL_WARNINGS in SQL DeveloperThe PL/SQL Compiler pane specifies options for compilation of PL/SQL subprograms. If the Generate PL/SQL Debug Information check box is selected, PL/SQL debug information is included in the compiled code; if this option is not selected, this debug information is not included. The ability to stop on individual code lines and debugger access to variables are allowed only in code compiled with debug information generated. Setting and Viewing the PL/SQL Compile-Time Warning Messages Categories in SQL DeveloperYou can control the display of informational, severe, and performance-related messages. The ALL type overrides any individual specifications for the other types of messages. For each type of message, you can specify any of the following:

• No entry (blank): Use any value specified for ALL; and if no value is specified, use the Oracle default.

• Enable: Enable the display of all messages of this category. • Disable: Disable the display of all messages of this category. • Error: Enable the display of only error messages of this category.

Page 22: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 20

Copyright © 2009, Oracle. All rights reserved.10 - 20

Viewing the Current Setting of PLSQL_WARNINGS

V$PARAMETER view

DBMS_WARNING package function

USER|DBA|ALL_PLSQL_OBJECT_SETTINGS views

PLSQL_WARNINGSinitialization parameter

SQL Developer

Viewing the Current Value of the PLSQL_WARNINGS ParameterYou can examine the current setting for the PLSQL_WARNINGS parameter by issuing a SELECT statement on the V$PARAMETER view. For example:

ALTER SESSION SET plsql_warnings = 'enable:severe', 'enable:performance','enable:informational';

Session altered.SELECT value FROM v$parameter WHERE name='plsql_warnings';VALUE---------------------------------------------------------ENABLE:ALL

Alternatively, you can use the DBMS_WARNING.GET_WARNING_SETTING_STRINGpackage and procedure to retrieve the current settings for the PLSQL_WARNINGS parameter:

DECLARE s VARCHAR2(1000);BEGIN

s := dbms_warning.get_warning_setting_string();dbms_output.put_line (s);

END;/

Page 23: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 21

Copyright © 2009, Oracle. All rights reserved.10 - 21

Viewing the Compiler Warnings: Using SQL Developer, SQL*Plus, or Data Dictionary Views

Enable warningsand compile program

Use SHOW ERRORScommand in SQL*Plus

Use USER/ALL/DBA_ERRORS views

View errors/warnings in SQL Developer

View compilerwarnings/errors

Compilerwarnings/errors?

Viewing the Compiler WarningsYou can use SQL*Plus to see any warnings raised as a result of the compilation of a PL/SQL block. SQL*Plus indicates that a compilation warning has occurred. The “SP2-08xx: <object> created with compilation warnings.” message is displayed for objects compiled with the PERFORMANCE, INFORMATIONAL, or SEVERE modifiers. There is no differentiation between the three. You must enable the compiler warnings before compiling the program. You can display the compiler warning messages using one of the following methods:Using the SQL*Plus SHOW ERRORS CommandThis command displays any compiler errors including the new compiler warnings and informational messages. This command is invoked immediately after a CREATE[PROGEDURE|FUNCTION|PACKAGE] command is used. The SHOW ERRORS command displays warnings and compiler errors. New compiler warnings and informational messages are “interleaved” with compiler errors when SHOW ERRORS is invoked.Using the Data Dictionary ViewsYou can select from the USER_|ALL_|DBA_ERRORS data dictionary views to display PL/SQL compiler warnings. The ATTRIBUTES column of these views has a new attribute called WARNING and the warning message displays in the TEXT column.

Page 24: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 22

Copyright © 2009, Oracle. All rights reserved.10 - 22

SQL*Plus Warning Messages: Example

CREATE OR REPLACE PROCEDURE bad_proc(p_out …) IS BEGIN . . .; END;/

SP2-0804: Procedure created with compilation warnings.

SHOW ERRORS;Errors for PROCEDURE BAD_PROC:

LINE/COL ERROR-------- --------------------------------------6/24 PLW-07203: parameter 'p_out' may benefit

from use of the NOCOPY compiler hint

SQL*Plus Warning Messages: ExampleUse the SHOW ERRORS command in SQL*Plus to display the compilation errors of a stored procedure. When you specify this option with no arguments, SQL*Plus displays the compilation errors for the most recently created or altered stored procedure. If SQL*Plus displays a compilation warnings message after you create or alter a stored procedure, you can use SHOW ERRORS commands to obtain more information. With the introduction of the support for PL/SQL warnings, the range of feedback messages is expanded to include a third message as follows:

SP2-08xx: <object> created with compilation warnings. This enables you to differentiate between the occurrence of a compilation warning and a compilation error. You must correct an error if you want to use the stored procedure, whereas a warning is for informational purposes only.The SP2 prefix is included with the warning message, because this provides you with the ability to look up the corresponding message number in the SQL*Plus User’s Guide and Reference to determine the cause and action for the particular message.Note: The SHOW SQL*Plus command is not supported in the SQL Developer 1.5.4 version that is used in this class. You can view the compiler errors and warnings using the USER_|ALL_|DBA_ERRORS data dictionary views.

Page 25: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 23

Copyright © 2009, Oracle. All rights reserved.10 - 23

Guidelines for Using PLSQL_WARNINGS

• The settings for the PLSQL_WARNINGS parameter are stored along with each compiled subprogram.

• If you recompile the subprogram using one of the following statements, the current settings for that session are used:– CREATE OR REPLACE – ALTER ... COMPILE

• If you recompile the subprogram using the ALTER ... COMPILE statement with the REUSE SETTINGS clause, the original setting stored with the program is used.

Guidelines for Using PLSQL_WARNINGSAs already stated, the PLSQL_WARNINGS parameter can be set at the session level or the system level. The settings for the PLSQL_WARNINGS parameter are stored along with each compiled subprogram. If you recompile the subprogram with a CREATE OR REPLACE statement, the current settings for that session are used. If you recompile the subprogram with an ALTER...COMPILE statement, then the current session setting is used unless you specify the REUSE SETTINGS clause in the statement, which uses the original setting that is stored with the subprogram.

Page 26: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 24

Copyright © 2009, Oracle. All rights reserved.10 - 24

Lesson Agenda

• Using the PLSQL_CODE_TYPE and PLSQL_OPTIMIZE_LEVEL PL/SQL Compilation Initialization Parameters

• Using the PL/SQL Compile-Time Warnings:– Using the PLSQL_WARNING Initialization Parameter– Using the DBMS_WARNING Package subprograms

Page 27: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 25

Copyright © 2009, Oracle. All rights reserved.10 - 25

Setting Compiler Warning Levels: Using the DBMS_WARNING Package

DBMS_WARNINGS package subprograms

Query settings Modify settings

Delete settingsRestore settings

Setting Compiler Warning Levels: Using the DBMS_WARNING PackageUse the DBMS_WARNING package to programmatically manipulate the behavior of current system or session PL/SQL warning settings. The DBMS_WARNING package provides a way to manipulate the behavior of PL/SQL warning messages, in particular by reading and changing the setting of the PLSQL_WARNINGS initialization parameter to control what kinds of warnings are suppressed, displayed, or treated as errors. This package provides the interface to query, modify, and delete current system or session settings.The DBMS_WARNING package is valuable if you are writing a development environment that compiles PL/SQL subprograms. Using the package interface routines, you can control PL/SQL warning messages programmatically to suit your requirements.

Page 28: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 26

Setting Compiler Warning Levels: Using the DBMS_WARNING Package (continued)Overview of PL/SQL Compile-Time Warnings for Subprograms: ExampleAssume that you write some code to compile PL/SQL code. You know that the compiler issues performance warnings when passing collection variables as OUT or IN OUT parameters without specifying the NOCOPY hint. The general environment that calls your compilation utility may or may not have appropriate warning-level settings. In any case, your business rules indicate that the calling environment set must be preserved and that your compilation process should suppress the warnings. By calling subprograms in the DBMS_WARNING package, you can detect the current warning settings, change the settings to suit your business requirements, and restore the original settings when your processing has completed.When you use the ALTER SESSION or ALTER SYSTEM command to set the PLSQL_WARNINGS parameter, the new value specified completely replaces the previous value. A new package, DBMS_WARNING is available in Oracle Database 10g that has interfaces to query and incrementally change the setting for the PLSQL_WARNINGS parameter and make it more specific to your requirements. The DBMS.WARNING package can be used to change the PLSQL_WARNINGS parameter incrementally, so that you can set the warnings that you want to set, without having to work out how to preserve the values of any warnings not of direct interest to you. For example, the DBA may only enable severe warnings for the entire database in the initialization parameter file, but a developer who is testing new code may want to view specific performance and informational messages. The developer can then use the DBMS_WARNING package to incrementally add the specific warnings that he or she wishes to see. This allows the developer to view the messages that he or she wants to see without replacing the DBA’s settings.

Page 29: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 27

Copyright © 2009, Oracle. All rights reserved.10 - 27

Using the DBMS_WARNING Package Subprograms

GET_CATEGORY (function)Get the warnings’categories names

Replace warnings

Query warnings

Set warnings

Scenario

ADD_WARNING_SETTING_CAT (procedure)ADD_WARNING_SETTING_NUM (procedure)

SET_WARNING_SETTING_STRING (procedure)

GET_WARNING_SETTING_CAT (function)GET_WARNING_SETTING_NUM (function)GET_WARNING_SETTING_STRING (function)

Subprograms to Use

Using the DBMS_WARNING SubprogramsThe following is a list of the DBMS_WARNING subprograms:• ADD_WARNING_SETTING_CAT: Modifies the current session or system warning

settings of the warning_category previously supplied • ADD_WARNING_SETTING_NUM: Modifies the current session or system warning

settings of the warning_number previously supplied• GET_CATEGORY: Returns the category name, given the message number • GET_WARNING_SETTING_CAT: Returns the specific warning category in the session• GET_WARNING_SETTING_NUM: Returns the specific warning number in the session• GET_WARNING_SETTING_STRING: Returns the entire warning string for the current

session• SET_WARNING_SETTING_STRING: Replaces previous settings with the new value

Note: For additional information about the above subprograms, refer to Oracle Database PL/SQL Packages and Types Reference 11g Release 2 (11.2).

Page 30: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 28

Copyright © 2009, Oracle. All rights reserved.10 - 28

The DBMS_WARNING Procedures: Syntax, Parameters, and Allowed Values

EXECUTE DBMS_WARNING.ADD_WARNING_SETTING_NUM (-warning_number IN NUMBER, warning_value IN VARCHAR2, scope IN VARCAHR2);

EXECUTE DBMS_WARNING.ADD_WARNING_SETTING_CAT (-warning_category IN VARCHAR2, warning_value IN VARCHAR2,scope IN VARCAHR2);

EXECUTE DBMS_WARNING.SET_WARNING_SETTING_STRING (-warning_value IN VARCHAR2,scope IN VARCHAR2);

The DBMS_WARNING Procedures: Syntax, Parameters, and Allowed ValuesThe warning_category is the name of the category. The allowed values are: • ALL• INFORMATIONAL• SEVERE• PERFORMANCE.

The warning_value is the value for the category. The allowed values are: • ENABLE• DISABLE• ERROR

The warning_number is the warning message number. The allowed values are all validwarning numbers. The scope specifies whether the changes are being performed in the session context or the system context. The allowed values are SESSION or SYSTEM. Using SYSTEM requires the ALTER SYSTEM privilege.

Page 31: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 29

Copyright © 2009, Oracle. All rights reserved.10 - 29

The DBMS_WARNING Procedures: Example

-- Establish the following warning setting string in the-- current session:-- ENABLE:INFORMATIONAL,-- DISABLE:PERFORMANCE,-- ENABLE:SEVERE

EXECUTE DBMS_WARNING.SET_WARNING_SETTING_STRING(-'ENABLE:ALL', 'SESSION');

EXECUTE DBMS_WARNING.ADD_WARNING_SETTING_CAT(-'PERFORMANCE','DISABLE', 'SESSION');

Using DBMS_WARNING Procedures: ExampleUsing the SET_WARNING_SETTING_STRING procedure, you can set one warning setting. If you have multiple warning settings, you should perform the following steps:

1. Call SET_WARNING_SETTING_STRING to set the initial warning setting string.2. Call ADD_WARNING_SETTING_CAT (or ADD_WARNING_SETTING_NUM) repeatedly

to add more settings to the initial string.The example in the slide establishes the following warning setting string in the current session:

ENABLE:INFORMATIONAL,DISABLE:PERFORMANCE,ENABLE:SEVERE

Page 32: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 30

Copyright © 2009, Oracle. All rights reserved.10 - 30

The DBMS_WARNING Functions:Syntax, Parameters, and Allowed Values

DBMS_WARNING.GET_CATEGORY (-warning_number IN pls_integer) RETURN VARCHAR2;

DBMS_WARNING.GET_WARNING_SETTING_NUM (-warning_number IN NUMBER) RETURN warning_value;

DBMS_WARNING.GET_WARNING_SETTING_CAT (-warning_category IN VARCHAR2) RETURN warning_value;

DBMS_WARNING.GET_WARNING_SETTING_STRINGRETURN pls_integer;

The DBMS_WARNING Functions: Syntax, Parameters, and Allowed ValuesThe warning_category is the name of the category. The allowed values are: • ALL• INFORMATIONAL• SEVERE• PERFORMANCE

The warning_number is the warning message number. The allowed values are all validwarning numbers. The scope specifies whether the changes are being performed in the session context or the system context. The allowed values are SESSION or SYSTEM. Using SYSTEM requires the ALTER SYSTEM privilege.Note: Use the GET_WARNING_SETTING_STRING function when you do not have the SELECT privilege on the v$parameter or v$parameter2 fixed tables, or if you want to parse the warning string yourself and then modify and set the new value using SET_WARNING_SETTING_STRING.

Page 33: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 31

Copyright © 2009, Oracle. All rights reserved.10 - 31

The DBMS_WARNING Functions: Example

-- Determine the category for warning message number -- PLW-07203SET SERVEROUTPUT ONEXECUTE DBMS_OUTPUT.PUT_LINE( -DBMS_WARNING.GET_CATEGORY(7203));

-- Determine the current session warning settingsSET SERVEROUTPUT ONEXECUTE DBMS_OUTPUT.PUT_LINE( -DBMS_WARNING.GET_WARNING_SETTING_STRING);

Note The message numbers must be specified as positive integers, because the data type for the GET_CATEGORY parameter is PLS_INTEGER (allowing positive integer values).

Page 34: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 32

Copyright © 2009, Oracle. All rights reserved.10 - 32

Using DBMS_WARNING: Example

CREATE OR REPLACE PROCEDURE compile_code(p_pkg_name VARCHAR2) ISv_warn_value VARCHAR2(200);v_compile_stmt VARCHAR2(200) :=

'ALTER PACKAGE '|| p_pkg_name ||' COMPILE';

BEGINv_warn_value := DBMS_WARNING.GET_WARNING_SETTING_STRING; DBMS_OUTPUT.PUT_LINE('Current warning settings: '||

v_warn_value); DBMS_WARNING.ADD_WARNING_SETTING_CAT(

'PERFORMANCE', 'DISABLE', 'SESSION');DBMS_OUTPUT.PUT_LINE('Modified warning settings: '||

DBMS_WARNING.GET_WARNING_SETTING_STRING); EXECUTE IMMEDIATE v_compile_stmt;DBMS_WARNING.SET_WARNING_SETTING_STRING(v_warn_value,

'SESSION'); DBMS_OUTPUT.PUT_LINE('Restored warning settings: '||

DBMS_WARNING.GET_WARNING_SETTING_STRING);END;/

Using DBMS_WARNING: ExampleNote: Before you run the code provided in the example in the slide, you must create the MY_PKG script found in demo_10_33.sql. This demo script creates the MY_PKG package.In the example in the slide, the compile_code procedure is designed to compile a named PL/SQL package. The code suppresses the PERFORMANCE category warnings. The calling environment’s warning settings must be restored after the compilation is performed. The code does not know what the calling environment warning settings are; it uses the GET_WARNING_SETTING_STRING function to save the current setting. This value is used to restore the calling environment setting using the DBMS_WARNING.SET_WARNING_SETTING_STRING procedure in the last line of the example code. Before compiling the package using Native Dynamic SQL, the compile_codeprocedure alters the current session-warning level by disabling warnings for the PERFORMANCEcategory. The code also prints the original, modified, and the restored warning settings.

Page 35: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 33

Copyright © 2009, Oracle. All rights reserved.10 - 33

Using DBMS_WARNING: Example

EXECUTE DBMS_WARNING.SET_WARNING_SETTING_STRING(-'ENABLE:ALL', 'SESSION');

@code_10_32_s.sql

@code_10_33_cs.sql –- compiles the DEPT_PKG package

Using DBMS_WARNING: Example (continued)In the example in the slide, the example provided in the previous slide is tested. First, enable all compiler warnings. Next, run the script on the previous page. Finally, call the compile_codeprocedure and pass it an existing package name, DEPT_PKG, as a parameter.

Page 36: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 34

Copyright © 2009, Oracle. All rights reserved.10 - 34

Using the PLW 06009 Warning Message

• The PLW warning indicates that the OTHERS handler of your PL/SQL subroutine can exit without executing:– Some form of RAISE, or – A call to the standard procedure

RAISE_APPLICATION_ERROR• A good programming practice suggests that OTHERS

handlers must always pass an exception upward.

Using the New PLW 06009 WarningAs a good programming practice, you should have your OTHERS exception handler pass the exception upward to the calling subroutine. If you fail to add this functionality, you run the risk of having exceptions go unnoticed. To avoid this flaw in your code, you can turn on warnings for your session and recompile the code that you want to verify. If the OTHERS handler does not handle the exception, the PLW 06009 warning will inform you.Note: PLW 06009 is not the only new warning message in Oracle Database 11g. For a complete list of all PLW warnings, see the Oracle Database Error Messages11g Release 2 (11.2) guide.

Page 37: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 35

Copyright © 2009, Oracle. All rights reserved.10 - 35

The PLW 06009 Warning: ExampleCREATE OR REPLACE PROCEDURE p(i IN VARCHAR2)

ISBEGIN

INSERT INTO t(col_a) VALUES (i);EXCEPTION

WHEN OTHERS THEN null;END p;

/ALTER PROCEDURE P COMPILE

PLSQL_warnings = 'enable:all' REUSE SETTINGS;

SELECT * FROM user_errorsWHERE name = 'P'

The PLW 06009 Warning: ExampleAfter running the first code example in the slide and after compiling the procedure using the Object Navigation tree, the Compiler – Log tab displays the PLW-06009 warning. You can also use the user_error data dictionary view to display the error. The definition of table t that is used in the slide example is as follows:

CREATE TABLE t (col_a NUMBER);

Page 38: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 36

Copyright © 2009, Oracle. All rights reserved.10 - 36

Quiz

The categories of PL/SQL compile-time warning messages are:1. SEVERE2. PERFORMANCE3. INFORMATIONAL4. All5. CRITICAL

Answers: 1, 2, 3, 4PL/SQL warning messages are divided into categories, so that you can suppress or display groups of similar warnings during compilation. The categories are:• SEVERE: Messages for conditions that may cause unexpected behavior or wrong results,

such as aliasing problems with parameters.• PERFORMANCE: Messages for conditions that may cause performance problems, such as

passing a VARCHAR2 value to a NUMBER column in an INSERT statement.• INFORMATIONAL: Messages for conditions that do not have an effect on performance or

correctness, but that you may want to change to make the code more maintainable, such as unreachable code that can never be executed.

• ALL: Displays all categories.

Page 39: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 37

Copyright © 2009, Oracle. All rights reserved.10 - 37

Summary

In this lesson, you should have learned how to:• Use the PL/SQL compiler initialization parameters• Use the PL/SQL compile-time warnings

Page 40: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 10 - 38

Copyright © 2009, Oracle. All rights reserved.10 - 38

Practice 10: Overview

This practice covers the following topics:• Displaying the compiler initialization parameters• Enabling native compilation for your session and compiling

a procedure• Disabling the compiler warnings, and then restoring the

original session-warning settings• Identifying the categories for some compiler-warning

message numbers

Practice 10: OverviewIn this practice, you display the compiler initialization parameters. You then enable native compilation for your session and compile a procedure. You then suppress all compiler warnings categories and then restore the original session-warning settings. Finally, you identify the categories for some compiler-warning message numbers.

Page 41: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Copyright © 2009, Oracle. All rights reserved.

Managing PL/SQL Code

Page 42: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 2

Copyright © 2009, Oracle. All rights reserved.11 - 2

Objectives

After completing this lesson, you should be able to do the following:• Describe and use conditional compilation• Hide PL/SQL source code using dynamic obfuscation and

the Wrap utility

Lesson AimThis lesson introduces the conditional compilation and obfuscating or wrapping PL/SQL code.

Page 43: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 3

Copyright © 2009, Oracle. All rights reserved.11 - 3

Lesson Agenda

• Using conditional compilation• Obfuscating PL/SQL code

Page 44: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 4

Copyright © 2009, Oracle. All rights reserved.11 - 4

What Is Conditional Compilation?

Enables you to customize the functionality in a PL/SQL application without removing any source code:• Utilize the latest functionality with the latest database

release or disable the new features to run the application against an older release of the database.

• Activate debugging or tracing functionality in the development environment and hide that functionality in the application while it runs at a production site.

Reserved preprocessor control tokens

$IF, $THEN, $ELSE, $ELSIF, $END, $$, $ERROR

What Is Conditional Compilation?Conditional compilation enables you to selectively include code, depending on the values of the conditions evaluated during compilation. For example, conditional compilation enables you to determine which PL/SQL features in a PL/SQL application are used for specific database releases. The latest PL/SQL features in an application can be run on a new database release and at the same time those features can be conditional so that the same application is compatible with a previous database release. Conditional compilation is also useful when you want to execute debugging procedures in a development environment, but want to turn off the debugging routines in a production environment. Benefits of Conditional Compilation

• Support for multiple versions of the same program in one source code• Easy maintenance and debugging of code• Easy migration of code to a different release of the database

Page 45: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 5

Copyright © 2009, Oracle. All rights reserved.11 - 5

How Does Conditional Compilation Work?

Inquiry directives:Use the $$ token.

Selection directives:Use the $IF token.

Error directives:Use the $ERROR token.

DBMS_DB_VERSION package

DBMS_PREPROCESSOR package

How Does Conditional Compilation Work?You can use conditional compilation by embedding directives in your PL/SQL source programs. When the PL/SQL program is submitted for compilation, a preprocessor evaluates these directives and selects parts of the program to be compiled. The selected program source is then handed off to the compiler for compilation.Inquiry directives use the $$ token to make inquiries about the compilation environment such as the value of a PL/SQL compiler initialization parameters PLSQL_CCFLAGS or PLSQL_OPTIMIZE_LEVEL for the unit being compiled. This directive can be used in conjunction with the conditional selection directive to select the parts of the program to compile. Selection directives can test inquiry directives or static package constants by using the $IFconstruct to branch sections of code for possible compilation if a condition is satisfied.Error directives issue a compilation error if an unexpected condition is encountered during conditional compilation using the $ERROR token. The DBMS_DB_VERSION package provides database version and release constants that can be used for conditional compilation.The DBMS_PREPROCESSOR package provides subprograms for accessing the post-processed source text that is selected by conditional compilation directives in a PL/SQL unit.

Page 46: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 6

Copyright © 2009, Oracle. All rights reserved.11 - 6

Using Selection Directives

DECLARECURSOR cur IS SELECT employee_id FROMemployees WHERE$IF myapp_tax_package.new_tax_code $THEN

salary > 20000;$ELSE

salary > 50000;$ENDBEGIN

OPEN cur;. . .END;

$IF <Boolean-expression> $THEN Text$ELSEIF <Boolean-expression> $THEN Text. . .$ELSE Text$END

Using Selection DirectivesThe conditional selection directive looks like and operates like the IF-THEN-ELSE mechanism in PL/SQL proper. When the preprocessor encounters $THEN, it verifies that the text between $IF and $THEN is a static expression. If the check succeeds and the result of the evaluation is TRUE, then the PL/SQL program text between $THEN and $ELSE (or $ELSIF) is selected for compilation.The selection condition (the expression between $IF and $THEN) can be constructed by referring to constants defined in another package or an inquiry directive or some combination of the two.In the example in the slide, conditional selection directive chooses between two versions of the cursor, cur, on the basis of the value of MYAPP_TAX_PACKAGE.NEW_TAX_CODE. If the value is TRUE, then employees with salary > 20000 are selected, else employees with salary > 50000 are selected.

Page 47: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 7

Copyright © 2009, Oracle. All rights reserved.11 - 7

PLSQL_CCFLAGSPLSQL_CODE_TYPEPLSQL_OPTIMIZE_LEVELPLSQL_WARNINGS NLS_LENGTH_SEMANTICSPLSQL_LINEPLSQL_UNIT

Using Predefined and User-Defined Inquiry Directives

PLSQL_CCFLAGS = 'plsql_ccflags:true,debug:true,debug:0';

Predefined inquiry directives

User-defined inquiry directives

Using Predefined and User-Defined Inquiry DirectivesAn inquiry directive can be predefined or user-defined. The following describes the order of the processing flow when conditional compilation attempts to resolve an inquiry directive:

1. The ID is used as an inquiry directive in the form $$id for the search key.2. The two-pass algorithm proceeds as follows:

a. The string in the PLSQL_CCFLAGS initialization parameter is scanned from right to left, searching with ID for a matching name (not case sensitive); done if found.

b. The predefined inquiry directives are searched; done if found.3. If the $$ID cannot be resolved to a value, then the PLW-6003 warning message is

reported if the source text is not wrapped. The literal NULL is substituted as the value for undefined inquiry directives. Note that if the PL/SQL code is wrapped, then the warning message is disabled so that the undefined inquiry directive is not revealed.

In the example in the slide, the value of $$debug is 0 and the value of $$plsql_ccflagsis TRUE. Note that the value of $$plsql_ccflags resolves to the user-definedplsql_ccflags inside the value of the PLSQL_CCFLAGS compiler parameter. This occurs because a user-defined directive overrides the predefined one.

Page 48: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 8

Copyright © 2009, Oracle. All rights reserved.11 - 8

The PLSQL_CCFLAGS Parameter and the Inquiry Directive

Use the PLSQL_CCFLAGS parameter to control conditional compilation of each PL/SQL library unit independently.

PLSQL_CCFLAGS = '<v1>:<c1>,<v2>:<c2>,...,<vn>:<cn>'

ALTER SESSION SETPLSQL_CCFLAGS = 'plsql_ccflags:true, debug:true, debug:0';

PLSQL_CCFLAGSinitialization parameter Inquiry directive

The PLSQL_CCFLAGS Parameter and the Inquiry DirectiveOracle Database 10g Release 2 introduced a new Oracle initialization parameter PLSQL_CCFLAGS for use with conditional compilation. This dynamic parameter enables you to set up name-value pairs. The names (called flag names) can then be referenced in inquiry directives. PLSQL_CCFLAGS provides a mechanism that allows PL/SQL programmers to control conditional compilation of each PL/SQL library unit independently. Values• vi: Has the form of an unquoted PL/SQL identifier. It is unrestricted and can be a reserved

word or a keyword. The text is not case sensitive. Each one is known as a flag or flag name. Each vi can occur more than once in the string, each occurrence can have a different flag value, and the flag values can be of different kinds.

• ci: Can be any of the following: - A PL/SQL Boolean literal- A PLS_INTEGER literal- The literal NULL (default). The text is not case sensitive. Each one is known as a flag

value and corresponds to a flag name.

Page 49: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 9

Copyright © 2009, Oracle. All rights reserved.11 - 9

Displaying the PLSQL_CCFLAGS Initialization Parameter Setting

SELECT name, type, plsql_ccflagsFROM user_plsql_object_settings

. . .

Displaying the PLSQL_CCFLAGS Initialization Parameter SettingUse the USER|ALL|DBA_PLSQL_OBJECT_SETTINGS data dictionary views to display the settings of a PL/SQL object. You can define any allowable value for PLSQL_CCFLAGS. However, Oracle recommends that this parameter be used for controlling the conditional compilation of debugging or tracing code.The flag names can be set to any identifier, including reserved words and keywords. The values must be the literals TRUE, FALSE, or NULL, or a PLS_INTEGER literal. The flag names and values are not case sensitive. The PLSQL_CCFLAGS parameter is a PL/SQL compiler parameter (like other compiler parameters) and is stored with the PL/SQL program unit. Consequently, if the PL/SQL program gets recompiled later with the REUSE SETTINGSclause (example, ALTER PACKAGE …REUSE SETTINGS), then the same value of PLSQL_CCFLAGS is used for the recompilation. Because the PLSQL_CCFLAGS parameter can be set to a different value for each PL/SQL unit, it provides a convenient method for controlling conditional compilation on a per unit basis.

Page 50: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 10

Copyright © 2009, Oracle. All rights reserved.11 - 10

ALTER SESSION SET PLSQL_CCFLAGS = 'Tracing:true';CREATE OR REPLACE PROCEDURE P ISBEGIN

$IF $$tracing $THENDBMS_OUTPUT.PUT_LINE ('TRACING');

$ENDEND P;

The PLSQL_CCFLAGS Parameter and the Inquiry Directive: Example

SELECT name, plsql_ccflags FROM USER_PLSQL_OBJECT_SETTINGSWHERE name = 'P';

The PLSQL_CCFLAGS Parameter and the Inquiry Directive: ExampleIn the example in the slide, the parameter is set and then the procedure is created. The setting is stored with each PL/SQL unit.

Page 51: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 11

Copyright © 2009, Oracle. All rights reserved.11 - 11

Using Conditional Compilation Error Directives to Raise User-Defined Errors

ALTER SESSION SET Plsql_CCFlags = ' Trace_Level:3 '/ CREATE PROCEDURE P ISBEGIN

$IF $$Trace_Level = 0 $THEN ...;$ELSIF $$Trace_Level = 1 $THEN ...;$ELSIF $$Trace_Level = 2 $THEN ...;$else $error 'Bad: '||$$Trace_Level $END –- error

-- directive$END -- selection directive ends

END P;

$ERROR varchar2_static_expression $END

SHOW ERRORSErrors for PROCEDURE P:LINE/COL ERROR-------- ------------------------------------6/9 PLS-00179: $ERROR: Bad: 3

Using Conditional Compilation Error Directives to Raise User-Defined ErrorsThe $ERROR error directive raises a user-defined error and is of the form:

$ERROR varchar2_static_expression $ENDNote: varchar2_static_expression must be a VARCHAR2 static expression.``

Page 52: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 12

Copyright © 2009, Oracle. All rights reserved.11 - 12

Using Static Expressions with Conditional Compilation

• Boolean static expressions:– TRUE, FALSE, NULL, IS NULL, IS NOT NULL– > , < , >= , <= , = , <>, NOT, AND, OR

• PLS_INTEGER static expressions:– -2147483648 to 2147483647, NULL

• VARCHAR2 static expressions include:– ||, NULL, TO_CHAR

• Static constants:

static_constant CONSTANT datatype := static_expression;

Using Static Expressions with Conditional CompilationAs described earlier, a preprocessor processes conditional directives before proper compilation begins. Consequently, only expressions that can be fully evaluated at compile time are permitted in conditional compilation directives. Any expression that contains references to variables or functions that require the execution of the PL/SQL are not available during compilation and cannot be evaluated. This subset of PL/SQL expressions allowed in conditional compilation directives is referred to as static expressions. Static expressions are carefully defined to guarantee that if a unit is automatically recompiled without any changes to the values it depends on, the expressions evaluate in the same way and the same source is compiled.Generally, static expressions are composed of three sources:

• Inquiry directives marked with $$• Constants defined in PL/SQL packages such as DBMS_DB_VERSION. These values can

be combined and compared using the ordinary operations of PL/SQL.• Literals such as TRUE, FALSE, 'CA', 123, NULL

Static expressions can also contain operations that include comparisons, logical Boolean operations (such as OR and AND), or concatenations of static character expression.

Page 53: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 13

Copyright © 2009, Oracle. All rights reserved.11 - 13

The DBMS_DB_VERSION Package: Boolean Constants

VER_LE_9VER_LE_9_1VER_LE_9_2VER_LE_10VER_LE_10_1VER_LE_10_2VER_LE_11VER_LE_11_1

DBMS_DB_VERSIONPackage

DBMS_DB_VERSIONBoolean constants

Oracle 11g Release 1

TRUE?

The DBMS_DB_VERSION PackageOracle Database 10g Release 2 introduced the DBMS_DB_VERSION package. This package specifies the Oracle database version and release numbers that are useful when making simple selections for conditional compilation.The constants represent a Boolean condition that evaluates to less than or equal to the version and the release, if present. ExampleVER_LE_11 indicates that the database version <= 11. The values of the constants are either TRUE or FALSE. For example, in an Oracle Database 11g Release 1 database, VER_LE_11 and VER_LE_11_1 are TRUE and all other constants are FALSE.

Page 54: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 14

Copyright © 2009, Oracle. All rights reserved.11 - 14

The DBMS_DB_VERSION Package Constants

Version <=11 and release <= 1.TRUEVER_LE_11_1Version <= 11.FALSEVER_LE_11Version <=10 and release <= 2.TRUEVER_LE_10_2Version <= 10 and release <= 1.FALSEVER_LE_10_1Version <= 10.

Version <= 9 and release <= 2.

Version <= 9 and release <= 1.

Version <= 9.

Description

FALSEVER_LE_9_2

FALSEVER_LE_9_1

FALSEVER_LE_9

TRUEVER_LE_10

Name Value

The DBMS_DB_VERSION PackageThe package for the Oracle Database 11g Release 1 version is shown below:

PACKAGE DBMS_DB_VERSION ISVERSION CONSTANT PLS_INTEGER := 11; -- RDBMS version

-- numberRELEASE CONSTANT PLS_INTEGER := 1; -- RDBMS release

-- numberver_le_9_1 CONSTANT BOOLEAN := FALSE;ver_le_9_2 CONSTANT BOOLEAN := FALSE;ver_le_9 CONSTANT BOOLEAN := FALSE;ver_le_10_1 CONSTANT BOOLEAN := FALSE;ver_le_10_2 CONSTANT BOOLEAN := FALSE;ver_le_10 CONSTANT BOOLEAN := FALSE;ver_le_11_1 CONSTANT BOOLEAN := TRUE;ver_le_11 CONSTANT BOOLEAN := TRUE;

END DBMS_DB_VERSION;The DBMS_DB_VERSION package contains different constants for different Oracle Database releases. The Oracle Database 11g Release 1 version of the DBMS_DB_VERSION package uses the constants shown in the slide.

Page 55: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 15

Copyright © 2009, Oracle. All rights reserved.11 - 15

Using Conditional Compilation with Database Versions: Example

ALTER SESSION SET PLSQL_CCFLAGS = 'my_debug:FALSE, my_tracing:FALSE';CREATE PACKAGE my_pkg AS

SUBTYPE my_real IS-- Check the database version, if >= 10g, use BINARY_DOUBLE data type, -- else use NUMBER data type

$IF DBMS_DB_VERSION.VERSION < 10 $THEN NUMBER; $ELSE BINARY_DOUBLE;$END

my_pi my_real; my_e my_real;END my_pkg;/CREATE PACKAGE BODY my_pkg ASBEGIN

$IF DBMS_DB_VERSION.VERSION < 10 $THENmy_pi := 3.14016408289008292431940027343666863227;my_e := 2.71828182845904523536028747135266249775;

$ELSEmy_pi := 3.14016408289008292431940027343666863227d;my_e := 2.71828182845904523536028747135266249775d;

$ENDEND my_pkg;/

Using Conditional Compilation with Database Versions: ExampleThis example also shows the use of the PLSQL_CCFLAGS parameter. First, you set the PLSQL_CCFLAGS parameter flag for displaying debugging code and tracing information. In the example in the slide on this page and the next page, conditional compilation is used to specify code for database versions. Conditional compilation is used to determine whether the BINARY_DOUBLE data type can be utilized in the calculations for PL/SQL units in the database. The BINARY_DOUBLE data type can only be used in Oracle Database 10g or later. If you are using Oracle Database 10g, then the data type for my_real is BINARY_DOUBLE; otherwise, the data type for my_real is NUMBER.In the specification of the new package, my_pkg, conditional compilation is used to check for the database version. In the body definition of the package, conditional compilation is used again to set the values of my_pi and my_e for future calculations based on the database version.The result of the slide example code is as follows:

Page 56: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 16

Copyright © 2009, Oracle. All rights reserved.11 - 16

Using Conditional Compilation with Database Versions: Example

CREATE OR REPLACE PROCEDURE circle_area(p_radius my_pkg.my_real) ISv_my_area my_pkg.my_real;v_my_datatype VARCHAR2(30);

BEGINv_my_area := my_pkg.my_pi * p_radius * p_radius;DBMS_OUTPUT.PUT_LINE('Radius: ' || TO_CHAR(p_radius)

|| ' Area: ' || TO_CHAR(v_my_area) );$IF $$my_debug $THEN -- if my_debug is TRUE, run some debugging code

SELECT DATA_TYPE INTO v_my_datatype FROM USER_ARGUMENTS WHERE OBJECT_NAME = 'CIRCLE_AREA' AND ARGUMENT_NAME = 'P_RADIUS';

DBMS_OUTPUT.PUT_LINE('Datatype of the RADIUS argument is: ' || v_my_datatype);

$ENDEND; /

CALL circle_area(50); -- Using Oracle Database 11g Release 2

Using Conditional Compilation with Database Versions: Example (continued)In the example in the slide, a new procedure called circle_area is defined. This procedure calculates the area of a circle based on the values of the variables in the my_pkg package defined on the previous page. The procedure has one IN formal parameter, radius. The procedure declares a couple of variables: my_area, which is the same data type as my_real in my_pkg, and my_datatype, which is a VARCHAR2(30). In the procedure’s body, my_area becomes equal to the value of my_pi set in my_pkgmultiplied by the value that is passed to the procedure as a radius. A message is printed displaying the radius and the area of the circle as shown in the second code example in the slide. Note: If you want to set my_debug to TRUE, you can make this change only for the circle_area procedure with the REUSE SETTINGS clause as follows:

ALTER PROCEDURE circle_area COMPILE PLSQL_CCFLAGS = 'my_debug:TRUE' REUSE SETTINGS;

Page 57: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 17

Copyright © 2009, Oracle. All rights reserved.11 - 17

Using DBMS_PREPROCESSOR Procedures to Print or Retrieve Source Text

CALL DBMS_PREPROCESSOR.PRINT_POST_PROCESSED_SOURCE('PACKAGE', 'ORA61', 'MY_PKG');

Using DBMS_PREPROCESSOR Procedures to Print or Retrieve Source TextDBMS_PREPROCESSOR subprograms print or retrieve the postprocessed source text of a PL/SQL unit after processing the conditional compilation directives. This postprocessed text is the actual source used to compile a valid PL/SQL unit. The example in the slide shows how to print the postprocessed form of my_pkg using the PRINT_POST_PROCESSED_SOURCEprocedure.When my_pkg is compiled on an Oracle Database 10g release or later database using the HR account, the resulting output is shown in the slide above. The PRINT_POST_PROCESSED_SOURCE removes unselected text. The lines of code that are not included in the postprocessed text are removed. The arguments for the PRINT_POST_PROCESSED_SOURCE procedure are: object type, schema name (using student account ORA61), and object name. Note: For additional information about the DBMS_PREPROCESSOR package, refer to the Oracle Database PL/SQL Packages and Types Reference 11g Release 2 (11.2) guide.

Page 58: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 18

Copyright © 2009, Oracle. All rights reserved.11 - 18

Lesson Agenda

• Using conditional compilation• Obfuscating PL/SQL code

Page 59: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 19

Copyright © 2009, Oracle. All rights reserved.11 - 19

What Is Obfuscation?

• Obfuscation (or wrapping) of a PL/SQL unit is the process of hiding the PL/SQL source code.

• Wrapping can be done with the wrap utility and DBMS_DDLsubprograms.

• The Wrap utility is run from the command line and it processes an input SQL file, such as a SQL*Plus installation script.

• The DBMS_DDL subprograms wrap a single PL/SQL unit, such as a single CREATE PROCEDURE command, that has been generated dynamically.

NoteFor additional information about obfuscation, refer to the Oracle Database PL/SQL Language Reference 11g Release 2 (11.2) guide.

Page 60: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 20

Copyright © 2009, Oracle. All rights reserved.11 - 20

Benefits of Obfuscating

• It prevents others from seeing your source code.• Your source code is not visible through the USER_SOURCE,

ALL_SOURCE, or DBA_SOURCE data dictionary views.• SQL*Plus can process the obfuscated source files.• The Import and Export utilities accept wrapped files.

Page 61: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 21

Copyright © 2009, Oracle. All rights reserved.11 - 21

What’s New in Dynamic Obfuscating Since Oracle 10g?

DBMS_DDL package

DBMS_DDL.WRAP function

CREATE_WRAPPED procedure

Wraps the text and creates the PL/SQL unit

Provides the same functionality as the CREATE_WRAPPED procedure

but allows for larger inputs

What’s New in Dynamic Obfuscating Since Oracle 10g?The CREATE_WRAPPED ProcedureIt takes as input a single CREATE OR REPLACE statement that specifies creation of a PL/SQL package specification, package body, function, procedure, type specification, or type body, generates a CREATE OR REPLACE statement with the PL/SQL source text obfuscated, and executes the generated statement.The WRAP FunctionIt takes as input a CREATE OR REPLACE statement that specifies the creation of a PL/SQL package specification, package body, function, procedure, type specification, or type body and returns a CREATE OR REPLACE statement, where the text of the PL/SQL unit has been obfuscated.

Page 62: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 22

Copyright © 2009, Oracle. All rights reserved.11 - 22

Nonobfuscated PL/SQL Code: ExampleSET SERVEROUTPUT ONBEGIN -- The ALL_SOURCE view family shows source code

EXECUTE IMMEDIATE 'CREATE OR REPLACE PROCEDURE P1 ISBEGIN

DBMS_OUTPUT.PUT_LINE (''I am not wrapped'');END P1;

';END;/CALL p1();

SELECT text FROM user_source WHERE name = 'P1' ORDER BY line;

Nonobfuscated PL/SQL Code: ExampleIn the first example in the slide, the EXECUTE IMMEDIATE statement is used to create the procedure P1. The code in the created procedure is not wrapped. The code is not hidden when you use any of the views from the ALL_SOURCE view family to display the procedure’s code as shown in the slide.

Page 63: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 23

Copyright © 2009, Oracle. All rights reserved.11 - 23

Obfuscated PL/SQL Code: Example

BEGIN -- ALL_SOURCE view family obfuscates source codeDBMS_DDL.CREATE_WRAPPED ( '

CREATE OR REPLACE PROCEDURE P1 ISBEGIN

DBMS_OUTPUT.PUT_LINE (''I am wrapped now'');END P1;

' );END;/CALL p1();

SELECT text FROM user_source WHERE name = 'P1' ORDER BY line;

. . .

Obfuscated PL/SQL Code: ExampleIn the example in the slide, the DBMS_DDL.CREATE_WRAPPED package procedure is used to create the procedure P1. The code is obfuscated when you use any of the views from the ALL_SOURCE view family to display the procedure’s code as shown on the next page. When you check the *_SOURCE views, the source is wrapped, or hidden, so that others cannot view the code details as shown in the output of the command in the slide.

Page 64: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 24

Copyright © 2009, Oracle. All rights reserved.11 - 24

Dynamic Obfuscation: Example

SET SERVEROUTPUT ON

DECLAREc_code CONSTANT VARCHAR2(32767) :=' CREATE OR REPLACE PROCEDURE new_proc AS

v_VDATE DATE; BEGIN

v_VDATE := SYSDATE;DBMS_OUTPUT.PUT_LINE(v_VDATE) ;

END; ' ;BEGIN

DBMS_DDL.CREATE_WRAPPED (c_CODE);END;/

Dynamic Obfuscation: ExampleThe example in the slide displays the creation of a dynamically obfuscated procedure calledNEW_PROC. To verify that the code for NEW_PROC is obfuscated, you can query from theDBA|ALL|USER_SOURCE dictionary views as shown below:

SELECT text FROM user_sourceWHERE name = 'NEW_PROC';

. . .

Page 65: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 25

Copyright © 2009, Oracle. All rights reserved.11 - 25

The PL/SQL Wrapper Utility

• The PL/SQL wrapper is a stand-alone utility that hides application internals by converting PL/SQL source code into portable object code.

• Wrapping has the following features:– Platform independence– Dynamic loading– Dynamic binding– Dependency checking– Normal importing and exporting when invoked

PL/SQL WrapperThe PL/SQL wrapper is a stand-alone utility that converts PL/SQL source code into portable object code. Using it, you can deliver PL/SQL applications without exposing your source code, which may contain proprietary algorithms and data structures. The wrapper converts the readable source code into unreadable code. By hiding application internals, it prevents misuse of your application.Wrapped code, such as PL/SQL stored programs, has several features:

• It is platform independent, so you do not need to deliver multiple versions of the same compilation unit.

• It permits dynamic loading, so users need not shut down and restart to add a new feature. • It permits dynamic binding, so external references are resolved at load time. • It offers strict dependency checking, so that invalidated program units are recompiled

automatically when they are invoked. • It supports normal importing and exporting, so the import/export utility can process

wrapped files.

Page 66: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 26

Copyright © 2009, Oracle. All rights reserved.11 - 26

Running the Wrapper Utility

• Do not use spaces around the equal signs.• The INAME argument is required.• The default extension for the input file is .sql, unless it is

specified with the name.• The ONAME argument is optional.• The default extension for output file is .plb, unless

specified with the ONAME argument.

WRAP INAME=input_file_name [ONAME=output_file_name]

WRAP INAME=demo_04_hello.sqlWRAP INAME=demo_04_helloWRAP INAME=demo_04_hello.sql ONAME=demo_04_hello.plb

Examples

Running the WrapperThe wrapper is an operating system executable called WRAP. To run the wrapper, enter the following command at your operating system prompt:

WRAP INAME=input_file_name [ONAME=output_file_name]Each of the examples shown in the slide takes a file called demo_04_hello.sql as input and creates an output file called demo_04_hello.plb.After the wrapped file is created, execute the .plb file from SQL*Plus to compile and store the wrapped version of the source code, as you would execute SQL script files.Note

• Only the INAME argument is required. If the ONAME argument is not specified, then the output file acquires the same name as the input file with an extension of .plb.

• The input file can have any extension, but the default is .sql.• Case sensitivity of the INAME and ONAME values depends on the operating system.• Generally, the output file is much larger than the input file.• Do not put spaces around the equal signs in the INAME and ONAME arguments and values.

Page 67: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 27

Copyright © 2009, Oracle. All rights reserved.11 - 27

Results of Wrapping

-- Original PL/SQL source code in input file:

CREATE PACKAGE banking ISmin_bal := 100;no_funds EXCEPTION;...END banking;/

-- Wrapped code in output file:CREATE PACKAGE banking

wrapped012abc463e .../

Results of WrappingWhen it is wrapped, an object type, package, or subprogram has the following form: header, followed by the word wrapped, followed by the encrypted body.The input file can contain any combination of SQL statements. However, the PL/SQL wrapper wraps only the following CREATE statements: • CREATE [OR REPLACE] TYPE• CREATE [OR REPLACE] TYPE BODY• CREATE [OR REPLACE] PACKAGE• CREATE [OR REPLACE] PACKAGE BODY• CREATE [OR REPLACE] FUNCTION• CREATE [OR REPLACE] PROCEDURE

All other SQL CREATE statements are passed intact to the output file.

Page 68: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 28

Copyright © 2009, Oracle. All rights reserved.11 - 28

Guidelines for Wrapping

• You must wrap only the package body, not the package specification.

• The wrapper can detect syntactic errors but cannot detect semantic errors.

• The output file should not be edited. You maintain the original source code and wrap again as required.

• To ensure that all the important parts of your source code are obfuscated, view the wrapped file in a text editor before distributing it.

Guidelines for WrappingGuidelines include the following:

• When wrapping a package or object type, wrap only the body, not the specification. Thus, you give other developers the information that they need to use the package without exposing its implementation.

• If your input file contains syntactic errors, the PL/SQL wrapper detects and reports them. However, the wrapper cannot detect semantic errors because it does not resolve external references. For example, the wrapper does not report an error if the table or view amp does not exist:

CREATE PROCEDURE raise_salary (emp_id INTEGER, amount NUMBER) ASBEGIN

UPDATE amp -- should be empSET sal = sal + amount WHERE empno = emp_id;

END;However, the PL/SQL compiler resolves external references. Therefore, semantic errors are reported when the wrapper output file (.plb file) is compiled.

• Because its contents are not readable, the output file should not be edited. To change a wrapped object, you need to modify the original source code and wrap the code again.

Page 69: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 29

Copyright © 2009, Oracle. All rights reserved.11 - 29

DBMS_DDL Package Versus the Wrap Utility

YesNoObfuscate multipleprograms at a time

Dynamic Obfuscation

Code obfuscation

Functionality

YesYes

No

Wrap Utility

Yes

DBMS_DDL

DBMS_DDL Versus the Wrap UtilityBoth the Wrap utility and the DBMS_DDL package have distinct uses:The Wrap utility is useful for obfuscating multiple programs with one execution of the utility. In essence, a complete application may be wrapped. However, the Wrap utility cannot be used to obfuscate dynamically generated code at run time. The Wrap utility processes an input SQL file and obfuscates only the PL/SQL units in the file, such as:

• Package specification and body• Function and procedure• Type specification and body

The Wrap utility does not obfuscate PL/SQL content in:• Anonymous blocks • Triggers • Non-PL/SQL code

The DBMS_DDL package is intended to obfuscate a dynamically generated program unit from within another program unit. The DBMS_DDL package methods cannot obfuscate multiple program units at one execution. Each execution of these methods accepts only one CREATE OR REPLACE statement at a time as argument.

Page 70: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 30

Copyright © 2009, Oracle. All rights reserved.11 - 30

Quiz

Conditional compilation enables you to customize the functionality in a PL/SQL application without removing any source code.1. True2. False

Answer: 1

Conditional CompilationConditional compilation enables you to customize the functionality in a PL/SQL application without removing any source code.Utilize the latest functionality with the latest database release or disable the new features to run the application against an older release of the database.Activate debugging or tracing functionality in the development environment and hide that functionality in the application while it runs at a production site.

Page 71: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 31

Copyright © 2009, Oracle. All rights reserved.11 - 31

Summary

In this lesson, you should have learned how to: • Describe and use conditional compilation• Hide PL/SQL source code using dynamic obfuscation and

the Wrap utility

SummaryThis lesson introduced the conditional compilation and obfuscating (or wrapping) of PL/SQL code.

Page 72: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 11 - 32

Copyright © 2009, Oracle. All rights reserved.11 - 32

Practice 11: Overview

This practice covers the following topics:• Creating a package and a procedure that uses conditional

compilation• Using the appropriate package to retrieve the

postprocessed source text of the PL/SQL unit• Obfuscating some PL/SQL code

Practice 11: OverviewIn this practice, you create a package and a procedure that use conditional compilation. In addition, you use the appropriate package to retrieve the postprocessed source text of the PL/SQL unit. You also obfuscate some PL/SQL code.

Page 73: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Copyright © 2009, Oracle. All rights reserved.

Managing Dependencies

Page 74: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 2

Copyright © 2009, Oracle. All rights reserved.12 - 2

Objectives

After completing this lesson, you should be able to do the following:• Track procedural dependencies• Predict the effect of changing a database object on

procedures and functions• Manage procedural dependencies

Lesson AimThis lesson introduces you to object dependencies and implicit and explicit recompilation of invalid objects.

Page 75: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 3

Copyright © 2009, Oracle. All rights reserved.12 - 3

Overview of Schema Object Dependencies

BothView

BothUser-defined object

BothUser-defined collection

BothTrigger

BothTable

BothSynonym

BothSubprogram

Referenced onlySequence

Dependent onlyPackage body

Package specification

Object Type

Both

Can Be Dependent or Referenced

Dependent and Referenced Objects Some types of schema objects can reference other objects in their definitions. For example, a view is defined by a query that references tables or other views, and the body of a subprogram can include SQL statements that reference other objects. If the definition of object A references object B, then A is a dependent object (with respect to B) and B is a referenced object (with respect to A). Dependency Issues

• If you alter the definition of a referenced object, dependent objects may or may not continue to work properly. For example, if the table definition is changed, the procedure may or may not continue to work without error.

• The Oracle server automatically records dependencies among objects. To manage dependencies, all schema objects have a status (valid or invalid) that is recorded in the data dictionary, and you can view the status in the USER_OBJECTS data dictionary view.

• If the status of a schema object is VALID, then the object has been compiled and can be immediately used when referenced.

• If the status of a schema object is INVALID, then the schema object must be compiled before it can be used.

Page 76: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 4

Copyright © 2009, Oracle. All rights reserved.12 - 4

Dependencies

Direct dependency

Indirect dependency

Direct dependency

View or procedure:Referenced/dependent

Referencedtable

Dependentprocedure

Dependent and Referenced Objects (continued)A procedure or function can directly or indirectly (through an intermediate view, procedure, function, or packaged procedure or function) reference the following objects:

• Tables• Views• Sequences• Procedures• Functions• Packaged procedures or functions

Page 77: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 5

Copyright © 2009, Oracle. All rights reserved.12 - 5

Direct Local Dependencies

View A Table AProcedure A Procedure B

“depends-on” table

Managing Local DependenciesIn the case of local dependencies, the objects are on the same node in the same database. The Oracle server automatically manages all local dependencies, using the database’s internal “depends-on” table. When a referenced object is modified, the dependent objects are sometimes invalidated. The next time an invalidated object is called, the Oracle server automatically recompiles it.If you alter the definition of a referenced object, dependent objects might or might not continue to function without error, depending on the type of alteration. For example, if you drop a table, no view based on the dropped table is usable.Starting with Oracle Database 10g, the CREATE OR REPLACE SYNONYM command has been enhanced to minimize the invalidations to dependent PL/SQL program units and views that reference it. This is covered later in this lesson. Starting with Oracle Database 11g, dependencies are tracked at the level of element within unit. This is referred to as fine-grained dependency. Fine-grained dependencies are covered later in this lesson.

Page 78: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 6

Copyright © 2009, Oracle. All rights reserved.12 - 6

Querying Direct Object Dependencies:Using the USER_DEPENDENCIES View

SELECT name, type, referenced_name, referenced_typeFROM user_dependenciesWHERE referenced_name IN ('EMPLOYEES','EMP_VW' );

. . .

Querying Direct Object Dependencies: Using the USER_DEPENDENCIES ViewYou can determine which database objects to recompile manually by displaying direct dependencies from the USER_DEPENDENCIES data dictionary view. The ALL_DEPENDENCIES and DBA_DEPENDENCIES views contain the additional OWNERcolumn, which references the owner of the object.The USER_DEPENDENCIES Data Dictionary View ColumnsThe columns of the USER_DEPENDENCIES data dictionary view are as follows: • NAME: The name of the dependent object• TYPE: The type of the dependent object (PROCEDURE, FUNCTION, PACKAGE,

PACKAGE BODY, TRIGGER, or VIEW)• REFERENCED_OWNER: The schema of the referenced object• REFERENCED_NAME: The name of the referenced object• REFERENCED_TYPE: The type of the referenced object• REFERENCED_LINK_NAME: The database link used to access the referenced object

Page 79: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 7

Copyright © 2009, Oracle. All rights reserved.12 - 7

Querying an Object’s Status

Every database object has one of the following status values:

An access privilege on a referenced object was revoked. (Only a dependent object can be unauthorized.)

UNAUTHORIZED

The object is marked invalid because an object that it references has changed. (Only a dependent object can be invalid.)

INVALID

The object was successfully compiled, using the current definition in the data dictionary.

VALID

COMPILED WITH ERRORS

Status

The most recent attempt to compile the object produced errors.

Description

Querying an Object’s StatusEvery database object has one of the status values shown in the table in the slide.Note: The USER_OBJECTS, ALL_OBJECTS, and DBA_OBJECTS static data dictionary viewsdo not distinguish between Compiled with errors, Invalid, and Unauthorized; instead, they describe all these as INVALID.

Page 80: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 8

Copyright © 2009, Oracle. All rights reserved.12 - 8

Invalidation of Dependent Objects

• Procedure A is a direct dependent of View B. View B is a direct dependent of Table C. Procedure A is an indirect dependent of Table C.

• Direct dependents are invalidated only by changes to the referenced object that affect them.

• Indirect dependents can be invalidated by changes to the reference object that do not affect them.

View B Table CProcedure A

Invalidation of Dependent ObjectsIf object A depends on object B, which depends on object C, then A is a direct dependent of B, B is a direct dependent of C, and A is an indirect dependent of C.In Oracle Database 11g, direct dependents are invalidated only by changes to the referenced object that affect them (changes to the signature of the referenced object).Indirect dependents can be invalidated by changes to the reference object that do not affect them: If a change to Table C invalidates View B, it invalidates Procedure A (and all other direct and indirect dependents of View B). This is called cascading invalidation.Assume that the structure of the table on which a view is based is modified. When you describe the view by using the SQL*Plus DESCRIBE command, you get an error message that states that the object is invalid to describe. This is because the command is not a SQL command; at this stage, the view is invalid because the structure of its base table is changed. If you query the view now, then the view is recompiled automatically and you can see the result if it is successfully recompiled.

Page 81: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 9

Copyright © 2009, Oracle. All rights reserved.12 - 9

Schema Object Change That Invalidates Some Dependents: Example

CREATE VIEW commissioned ASSELECT first_name, last_name, commission_pct FROM employeesWHERE commission_pct > 0.00;

CREATE VIEW six_figure_salary ASSELECT * FROM employeesWHERE salary >= 100000;

SELECT object_name, status FROM user_objects WHERE object_type = 'VIEW';

Schema Object Change That Invalidates Some Dependents: ExampleThe example in the slide demonstrates an example of a schema object change that invalidates some dependents but not others. The two newly created views are based on the EMPLOYEEStable in the HR schema. The status of the newly created views is VALID.

Page 82: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 10

Copyright © 2009, Oracle. All rights reserved.12 - 10

Schema Object Change That Invalidates Some Dependents: Example

ALTER TABLE employees MODIFY email VARCHAR2(50);

SELECT object_name, status FROM user_objects WHERE object_type = 'VIEW';

Schema Object Change That Invalidates Some Dependents: Example (continued)Suppose you determine that the EMAIL column in the EMPLOYEES table needs to be lengthened from 25 to 50, you alter the table as shown in the slide above. Because the COMMISSIONED view does not include the EMAIL column in its select list, it is not invalidated. However, the SIXFIGURES view is invalidated because all columns in the table are selected.

Page 83: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 11

Copyright © 2009, Oracle. All rights reserved.12 - 11

Displaying Direct and Indirect Dependencies

1. Run the utldtree.sql script that creates the objects that enable you to display the direct and indirect dependencies.

2. Execute the DEPTREE_FILL procedure.

EXECUTE deptree_fill('TABLE', 'ORA61', 'EMPLOYEES')

@/home/oracle/labs/plpu/labs/utldtree.sql

Displaying Direct and Indirect Dependencies by Using Views Provided by OracleDisplay direct and indirect dependencies from additional user views called DEPTREE and IDEPTREE; these views are provided by Oracle.Example

1. Make sure that the utldtree.sql script has been executed. This script is located in the $ORACLE_HOME/labs/plpu/labs folder. You can run the script as follows:

@?/labs/plpu/labs/utldtree.sql Note: In this class, this script is supplied in the labs folder of your class files. The code example above uses the student account ORA61.

2. Populate the DEPTREE_TEMPTAB table with information for a particular referenced object by invoking the DEPTREE_FILL procedure. There are three parameters for this procedure:

object_type Type of the referenced object

object_owner Schema of the referenced object

object_name Name of the referenced object

Page 84: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 12

Copyright © 2009, Oracle. All rights reserved.12 - 12

Displaying DependenciesUsing the DEPTREE View

SELECT nested_level, type, nameFROM deptreeORDER BY seq#;

. . .

Displaying Dependencies Using the DEPTREE ViewYou can display a tabular representation of all dependent objects by querying the DEPTREEview. You can display an indented representation of the same information by querying the IDEPTREE view, which consists of a single column named DEPENDENCIES as follows:

SELECT * FROM ideptree;

. . .

Page 85: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 13

Copyright © 2009, Oracle. All rights reserved.12 - 13

More Precise Dependency Metadatain Oracle Database 11g

• Before 11g, adding column D to table T invalidated the dependent objects.

• Oracle Database 11g records additional, finer-grained dependency management:– Adding column D to table T does not impact view V and does

not invalidate the dependent objects

Procedure P Function FView VColumns: A,B

Table TColumns: A,B

Add column D

Fine-Grained DependenciesStarting with Oracle Database 11g, you have access to records that describe more precise dependency metadata. This is called fine-grained dependency and it enables you to see when the dependent objects are not invalidated without logical requirement.Earlier Oracle Database releases record dependency metadata—for example, PL/SQL unit Pdepends on PL/SQL unit F, or that view V depends on table T—with the precision of the whole object. This means that dependent objects are sometimes invalidated without logical requirement. For example, if view V depends only on columns A and B in table T, and column Dis added to table T, the validity of view V is not logically affected. Nevertheless, before Oracle Database Release 11.1, view V is invalidated by the addition of column D to table T. With Oracle Database Release 11.1, adding column D to table T does not invalidate view V. Similarly, if procedure P depends only on elements E1 and E2 within a package, adding element E99 to the package does not invalidate procedure P.Reducing the invalidation of dependent objects in response to changes to the objects on which they depend increases application availability, both in the development environment and during online application upgrade.For more information about this topic, refer to the 11g: Infrastructure Grid – High Availability P1 eStudy.

Page 86: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 14

Copyright © 2009, Oracle. All rights reserved.12 - 14

Fine-Grained Dependency Management

• In Oracle Database 11g, dependencies are now tracked at the level of element within unit.

• Element-based dependency tracking covers the following:– Dependency of a single-table view on its base table– Dependency of a PL/SQL program unit (package

specification, package body, or subprogram) on the following:

— Other PL/SQL program units— Tables— Views

Page 87: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 15

Copyright © 2009, Oracle. All rights reserved.12 - 15

Fine-Grained Dependency Management: Example 1

CREATE TABLE t2 (col_a NUMBER, col_b NUMBER, col_c NUMBER);CREATE VIEW v AS SELECT col_a, col_b FROM t2;

ALTER TABLE t2 ADD (col_d VARCHAR2(20));

SELECT ud.name, ud.type, ud.referenced_name, ud.referenced_type, uo.status

FROM user_dependencies ud, user_objects uoWHERE ud.name = uo.object_name AND ud.name = 'V';

SELECT ud.name, ud.type, ud.referenced_name,ud.referenced_type, uo.status

FROM user_dependencies ud, user_objects uoWHERE ud.name = uo.object_name AND ud.name = 'V';

Fine-Grained Dependency Management: Example 1Example of Dependency of a Single-Table View on Its Base TableIn the first example in the slide, table T2 is created with three columns: COL_A, COL_B, and COL_C. A view named V is created based on columns COL_A and COL_B of table T2. The dictionary views are queried and the view V is dependent on table T and its status is valid.In the third example, table T2 is altered. A new column named COL_D is added. The dictionary views still report that the view V is dependent because element-based dependency tracking realizes that the columns COL_A and COL_B are not modified and, therefore, the view does not need to be invalidated.

Page 88: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 16

Copyright © 2009, Oracle. All rights reserved.12 - 16

Fine-Grained Dependency Management: Example 1

ALTER TABLE t2 MODIFY (col_a VARCHAR2(20));SELECT ud.name, ud.referenced_name, ud.referenced_type,

uo.statusFROM user_dependencies ud, user_objects uoWHERE ud.name = uo.object_name AND ud.name = 'V';

Fine-Grained Dependency Management: Example 1 (continued)In the example in the slide, the view is invalidated because its element (COL_A) is modified in the table on which the view is dependent.

Page 89: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 17

Copyright © 2009, Oracle. All rights reserved.12 - 17

Fine-Grained Dependency Management:Example 2

CREATE PACKAGE pkg ISPROCEDURE proc_1;

END pkg; /CREATE OR REPLACE PROCEDURE p IS BEGIN

pkg.proc_1(); END p;/CREATE OR REPLACE PACKAGE pkg IS

PROCEDURE proc_1;PROCEDURE unheard_of;

END pkg; /

Fine-Grained Dependency Management: Example 2In the example in the slide, you create a package named PKG that has procedure PROC_1declared. A procedure named P invokes PKG.PROC_1. The definition of the PKG package is modified and another subroutine is added to the package declaration.When you query the USER_OBJECTS dictionary view for the status of the P procedure, it is still valid as shown because the element you added to the definition of PKG is not referenced through procedure P.

SELECT status FROM user_objectsWHERE object_name = 'P';

Page 90: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 18

Copyright © 2009, Oracle. All rights reserved.12 - 18

Changes to Synonym Dependencies

VALID status

VALID status

CREATE OR REPLACE SYNONYM

Changes to Synonym DependenciesThe Oracle Database minimizes down time during code upgrades or schema merges.When certain conditions on columns, privileges, partitions, and so on are met, a table or object type is considered equivalent and dependent objects are no longer invalidated. In Oracle Database 10g, the CREATE OR REPLACE SYNONYM command has been enhanced to minimize the invalidations to dependent PL/SQL program units and views that reference it. This eliminates the need for time-consuming recompilation of the program units after redefinition of the synonyms or during execution. You do not have to set any parameters or issue any special commands to enable this functionality; invalidations are minimized automatically.Note: This enhancement applies only to synonyms pointing to tables.

Page 91: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 19

Copyright © 2009, Oracle. All rights reserved.12 - 19

Maintaining Valid PL/SQL Program Units and Views

EMPLOYEESCREATE OR REPLACESYNONYM emp_hist

EMPLOYEES_HIST

VALID status

Maintaining Valid PL/SQL Program UnitsStarting with Oracle Database 10g Release 2, you can change the definition of a synonym, and the dependent PL/SQL program units are not invalidated under the following conditions:

• The column order, column names, and column data types of the tables are identical.• The privileges on the newly referenced table and its columns are a superset of the set of

privileges on the original table. These privileges must not be derived through roles alone.• The names and types of partitions and subpartitions are identical.• The tables are of the same organization type.• Object type columns are of the same type.

Maintaining Valid ViewsAs with dependent PL/SQL program units, you can change the definition of a synonym, and the dependent views are not invalidated under the conditions listed in the preceding paragraph. In addition, the following must be true to preserve the VALID status of dependent views, but not of dependent PL/SQL program units, when you redefine a synonym:

• Columns and order of columns defined for primary key and unique indexes, NOT NULLconstraints, and primary key and unique constraints must be identical.

• The dependent view cannot have any referential constraints.

Page 92: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 20

Copyright © 2009, Oracle. All rights reserved.12 - 20

EMPLOYEES table

REDUCE_SALprocedure

RAISE_SALprocedure

Another Scenario of Local Dependencies

Another Scenario of Local DependenciesThe example in the slide illustrates the effect that a change in the definition of a procedure has on the recompilation of a dependent procedure. Assume that the RAISE_SAL procedure updates the EMPLOYEES table directly, and that the REDUCE_SAL procedure updates the EMPLOYEEStable indirectly by way of RAISE_SAL.

• If the internal logic of the RAISE_SAL procedure is modified, REDUCE_SAL will successfully recompile if RAISE_SAL has successfully compiled.

• If the formal parameters for the RAISE_SAL procedure are eliminated, REDUCE_SALwill not successfully recompile.

Page 93: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 21

Copyright © 2009, Oracle. All rights reserved.12 - 21

Guidelines for Reducing Invalidation

To reduce invalidation of dependent objects:

Add new items to theend of the package

Reference each tablethrough a view

Guidelines for Reducing InvalidationAdd New Items to End of PackageWhen adding new items to a package, add them to the end of the package. This preserves the slot numbers and entry-point numbers of existing top-level package items, preventing their invalidation. For example, consider the following package:

CREATE OR REPLACE PACKAGE pkg1 ISFUNCTION get_var RETURN VARCHAR2;PROCEDURE set_var (v VARCHAR2);END;

Adding an item to the end of pkg1 does not invalidate dependents that reference get_var. Inserting an item between the get_var function and the set_var procedure invalidates dependents that reference the set_var function.Reference Each Table Through a ViewReference tables indirectly, using views. This allows you to do the following:

• Add columns to the table without invalidating dependent views or dependent PL/SQL objects

• Modify or delete columns not referenced by the view without invalidating dependent objects

Page 94: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 22

Copyright © 2009, Oracle. All rights reserved.12 - 22

Object Revalidation

• An object that is not valid when it is referenced must be validated before it can be used.

• Validation occurs automatically when an object is referenced; it does not require explicit user action.

• If an object is not valid, its status is either COMPILED WITH ERRORS, UNAUTHORIZED, or INVALID.

Object RevalidationThe compiler cannot automatically revalidate an object that compiled with errors. The compiler recompiles the object, and if it recompiles without errors, it is revalidated; otherwise, it remains invalid.The compiler checks whether the unauthorized object has access privileges to all of its referenced objects. If so, the compiler revalidates the unauthorized object without recompiling it. If not, the compiler issues appropriate error messages.The SQL compiler recompiles the invalid object. If the object recompiles without errors, it is revalidated; otherwise, it remains invalid.For an invalid PL/SQL program unit (procedure, function, or package), the PL/SQL compiler checks whether any referenced object changed in a way that affects the invalid object.

• If so, the compiler recompiles the invalid object. If the object recompiles without errors, it is revalidated; otherwise, it remains invalid. If not, the compiler revalidates the invalid object without recompiling it.

• If not, the compiler revalidates the invalid object without recompiling it. Fast revalidation is usually performed on objects that were invalidated due to cascading invalidation.

Page 95: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 23

Copyright © 2009, Oracle. All rights reserved.12 - 23

Remote Dependencies

View Table

Procedure

ProcedureRemote

dependency

ViewINVALID

TableProcedureVALID

ProcedureINVALID

Remotedependency

Local and remote references

Remote DependenciesIn the case of remote dependencies, the objects are on separate nodes. The Oracle server does not manage dependencies among remote schema objects other than local-procedure-to-remote-procedure dependencies (including functions, packages, and triggers). The local stored procedure and all its dependent objects are invalidated but do not automatically recompile when called for the first time.Recompilation of Dependent Objects: Local and Remote

• Verify successful explicit recompilation of the dependent remote procedures and implicit recompilation of the dependent local procedures by checking the status of these procedures within the USER_OBJECTS view.

• If an automatic implicit recompilation of the dependent local procedures fails, the status remains invalid and the Oracle server issues a run-time error. Therefore, to avoid disrupting production, it is strongly recommended that you recompile local dependent objects manually, rather than relying on an automatic mechanism.

Page 96: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 24

Copyright © 2009, Oracle. All rights reserved.12 - 24

Concepts of Remote Dependencies

Remote dependencies are governed by the mode that is chosen by the user:

TIMESTAMP checking SIGNATURE checking

Concepts of Remote DependenciesTIMESTAMP CheckingEach PL/SQL program unit carries a time stamp that is set when it is created or recompiled. Whenever you alter a PL/SQL program unit or a relevant schema object, all its dependent program units are marked as invalid and must be recompiled before they can execute. The actual time stamp comparison occurs when a statement in the body of a local procedure calls a remote procedure.SIGNATURE CheckingFor each PL/SQL program unit, both the time stamp and the signature are recorded. The signature of a PL/SQL construct contains information about the following:

• The name of the construct (procedure, function, or package)• The base types of the parameters of the construct• The modes of the parameters (IN, OUT, or IN OUT)• The number of the parameters

The recorded time stamp in the calling program unit is compared with the current time stamp in the called remote program unit. If the time stamps match, the call proceeds. If they do not match, the remote procedure call (RPC) layer performs a simple comparison of the signature to determine whether the call is safe or not. If the signature has not been changed in an incompatible manner, execution continues; otherwise, an error is returned.

Page 97: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 25

Copyright © 2009, Oracle. All rights reserved.12 - 25

Setting the REMOTE_DEPENDENCIES_MODEParameter

• As an init.ora parameter:REMOTE_DEPENDENCIES_MODE = TIMESTAMP |SIGNATURE

• At the system level:ALTER SYSTEM SET REMOTE_DEPENDENCIES_MODE = TIMESTAMP | SIGNATURE

• At the session level:ALTER SESSION SET REMOTE_DEPENDENCIES_MODE = TIMESTAMP | SIGNATURE

REMOTE_DEPENDENCIES_MODE ParameterSetting the REMOTE_DEPENDENCIES_MODEvalue TIMESTAMP

SIGNATURESpecify the value of the REMOTE_DEPENDENCIES_MODE parameter using one of the three methods described in the slide.Note: The calling site determines the dependency model.

Page 98: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 26

Copyright © 2009, Oracle. All rights reserved.12 - 26

Remote Procedure BCompiles at 8:00 AM

Remote procedure B:Compiles and is VALID

at 8:00 AM

Local Procedures Referencing Remote ProceduresA local procedure that references a remote procedure is invalidated by the Oracle server if the remote procedure is recompiled after the local procedure is compiled. Automatic Remote Dependency MechanismWhen a procedure compiles, the Oracle server records the time stamp of that compilation within the P code of the procedure. In the slide, when the remote procedure B is successfully compiled at 8:00 AM, this time is recorded as its time stamp.

Page 99: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 27

Copyright © 2009, Oracle. All rights reserved.12 - 27

Local Procedure A Compiles at 9:00 AM

Remote procedure B: VALID

Local procedure A: VALID

TIMESTAMP of B TIMESTAMP of A RecordTIMESTAMP of B

Local Procedures Referencing Remote Procedures (continued)Automatic Remote Dependency Mechanism (continued)When a local procedure referencing a remote procedure compiles, the Oracle server also records the time stamp of the remote procedure in the P code of the local procedure.In the slide, local procedure A (which is dependent on remote procedure B) is compiled at 9:00 AM. The time stamps of both procedure A and remote procedure B are recorded in the P code of procedure A.

Page 100: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 28

Copyright © 2009, Oracle. All rights reserved.12 - 28

Execute Procedure A

Remote procedure B: VALID

Local procedure A: VALID

TIMESTAMP of B TIMESTAMP of A TIMESTAMP of B

TIMESTAMP comparison

Automatic Remote DependencyWhen the local procedure is invoked at run time, the Oracle server compares the two time stamps of the referenced remote procedure. If the time stamps are equal (indicating that the remote procedure has not recompiled), then the Oracle server executes the local procedure.In the example in the slide, the time stamp recorded with the P code of remote procedure B is the same as that recorded with local procedure A. Therefore, local procedure A is valid.

Page 101: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 29

Copyright © 2009, Oracle. All rights reserved.12 - 29

Remote Procedure B Recompiled at 11:00 AM

Remote procedure B:Recompiles and is VALID

at 11:00 AM

Local Procedures Referencing Remote ProceduresAssume that remote procedure B is successfully recompiled at 11:00 AM. The new time stamp is recorded along with its P code.

Page 102: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 30

Copyright © 2009, Oracle. All rights reserved.12 - 30

Execute Procedure A

Remote procedure B: VALID

Local procedure A: INVALID

TIMESTAMP of B TIMESTAMP of A TIMESTAMP of B

TIMESTAMP comparison

Saved TIMESTAMP of B != COMPILE TIME of B

Automatic Remote DependencyIf the time stamps are not equal (indicating that the remote procedure has recompiled), then the Oracle server invalidates the local procedure and returns a run-time error. If the local procedure (which is now tagged as invalid) is invoked a second time, then the Oracle server recompiles it before executing, in accordance with the automatic local dependency mechanism.Note: If a local procedure returns a run-time error the first time it is invoked (indicating that the remote procedure’s time stamp has changed), then you should develop a strategy to reinvoke the local procedure.In the example in the slide, the remote procedure is recompiled at 11:00 AM and this time is recorded as its time stamp in the P code. The P code of local procedure A still has 8:00 AM as the time stamp for remote procedure B. Because the time stamp recorded with the P code of local procedure A is different from that recorded with the remote procedure B, the local procedure is marked invalid. When the local procedure is invoked for the second time, it can be successfully compiled and marked valid.A disadvantage of time stamp mode is that it is unnecessarily restrictive. Recompilation of dependent objects across the network is often performed when not strictly necessary, leading to performance degradation.

Page 103: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 31

Copyright © 2009, Oracle. All rights reserved.12 - 31

Signature Mode

• The signature of a procedure is:– The name of the procedure– The data types of the parameters– The modes of the parameters

• The signature of the remote procedure is saved in the local procedure.

• When executing a dependent procedure, the signature of the referenced remote procedure is compared.

SignaturesTo alleviate some of the problems with the time stamp–only dependency model, you can use the signature model. This allows the remote procedure to be recompiled without affecting the local procedures. This is important if the database is distributed.The signature of a subprogram contains the following information:

• The name of the subprogram• The data types of the parameters• The modes of the parameters• The number of parameters• The data type of the return value for a function

If a remote program is changed and recompiled but the signature does not change, then the local procedure can execute the remote procedure. With the time stamp method, an error would have been raised because the time stamps would not have matched.

Page 104: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 32

Copyright © 2009, Oracle. All rights reserved.12 - 32

Recompiling a PL/SQL Program Unit

Recompilation:• Is handled automatically through implicit run-time

recompilation• Is handled through explicit recompilation with the ALTER

statement

ALTER PROCEDURE [SCHEMA.]procedure_name COMPILE;

ALTER FUNCTION [SCHEMA.]function_name COMPILE;

ALTER PACKAGE [SCHEMA.]package_nameCOMPILE [PACKAGE | SPECIFICATION | BODY];

ALTER TRIGGER trigger_name [COMPILE[DEBUG]];

Recompiling PL/SQL ObjectsIf the recompilation is successful, the object becomes valid. If not, the Oracle server returns an error and the object remains invalid. When you recompile a PL/SQL object, the Oracle server first recompiles any invalid object on which it depends.Procedure: Any local objects that depend on a procedure (such as procedures that call the recompiled procedure or package bodies that define the procedures that call the recompiled procedure) are also invalidated.Packages: The COMPILE PACKAGE option recompiles both the package specification and the body, regardless of whether it is invalid. The COMPILE SPECIFICATION option recompiles the package specification. Recompiling a package specification invalidates any local objects that depend on the specification, such as subprograms that use the package. Note that the body of a package also depends on its specification. The COMPILE BODY option recompiles only the package body. Triggers: Explicit recompilation eliminates the need for implicit run-time recompilation and prevents associated run-time compilation errors and performance overhead.The DEBUG option instructs the PL/SQL compiler to generate and store the code for use by the PL/SQL debugger.

Page 105: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 33

Copyright © 2009, Oracle. All rights reserved.12 - 33

Unsuccessful Recompilation

Recompiling dependent procedures and functions is unsuccessful when:• The referenced object is dropped or renamed• The data type of the referenced column is changed• The referenced column is dropped• A referenced view is replaced by a view with different

columns• The parameter list of a referenced procedure is modified

Unsuccessful RecompilationSometimes a recompilation of dependent procedures is unsuccessful (for example, when a referenced table is dropped or renamed).The success of any recompilation is based on the exact dependency. If a referenced view is re-created, any object that is dependent on the view needs to be recompiled. The success of the recompilation depends on the columns that the view now contains, as well as the columns that the dependent objects require for their execution. If the required columns are not part of the new view, then the object remains invalid.

Page 106: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 34

Copyright © 2009, Oracle. All rights reserved.12 - 34

Successful Recompilation

Recompiling dependent procedures and functions is successful if:• The referenced table has new columns• The data type of referenced columns has not changed• A private table is dropped, but a public table that has the

same name and structure exists• The PL/SQL body of a referenced procedure has been

modified and recompiled successfully

Successful RecompilationThe recompilation of dependent objects is successful if:

• New columns are added to a referenced table• All INSERT statements include a column list• No new column is defined as NOT NULL

When a private table is referenced by a dependent procedure and the private table is dropped, the status of the dependent procedure becomes invalid. When the procedure is recompiled (either explicitly or implicitly) and a public table exists, the procedure can recompile successfully but is now dependent on the public table. The recompilation is successful only if the public table contains the columns that the procedure requires; otherwise, the status of the procedure remains invalid.

Page 107: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 35

Copyright © 2009, Oracle. All rights reserved.12 - 35

Recompiling Procedures

Minimize dependency failures by:• Declaring records with the %ROWTYPE attribute• Declaring variables with the %TYPE attribute• Querying with the SELECT * notation• Including a column list with INSERT statements

Recompilation of ProceduresYou can minimize recompilation failure by following the guidelines that are shown in the slide.

Page 108: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 36

Copyright © 2009, Oracle. All rights reserved.12 - 36

Packages and Dependencies: Subprogram References the Package

Packagespecification

Packagebody

Stand-alone procedure

Procedure A declaration

Procedure A declaration

.

.

.

.

Packagedefinitionchanged

Packages and Dependencies: Subprogram References the PackageYou can simplify dependency management with packages when referencing a package procedure or function from a stand-alone procedure or function.

• If the package body changes and the package specification does not change, then the stand-alone procedure that references a package construct remains valid.

• If the package specification changes, then the outside procedure referencing a package construct is invalidated, as is the package body.

Page 109: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 37

Copyright © 2009, Oracle. All rights reserved.12 - 37

Packages and Dependencies:Package Subprogram References Procedure

Packagespecification

Packagebody

Stand-alone procedure definition

changed

Procedure A declaration

Procedure A declaration

.

.

.

.

Packages and Dependencies: Package Subprogram References ProcedureIf a stand-alone procedure that is referenced within the package changes, then the entire package body is invalidated, but the package specification remains valid. Therefore, it is recommended that you bring the procedure into the package.

Page 110: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 38

Copyright © 2009, Oracle. All rights reserved.12 - 38

Quiz

You can display direct and indirect dependencies by running the utldtree.sql script, populating the DEPTREE_TEMPTABtable with information for a particular referenced object, and querying the DEPTREE or IDEPTREE views.1. True2. False

Answer: 1Displaying Direct and Indirect DependenciesYou can display direct and indirect dependencies as follows:

1. Run the utldtree.sql script, which creates the objects that enable you to display the direct and indirect dependencies.

2. Populate the DEPTREE_TEMPTAB table with information for a particular referenced object by executing the DEPTREE_FILL procedure.

3. Query the DEPTREE or IDEPTREE views.

Page 111: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 39

Copyright © 2009, Oracle. All rights reserved.12 - 39

Summary

In this lesson, you should have learned how to:• Track procedural dependencies• Predict the effect of changing a database object on

procedures and functions• Manage procedural dependencies

SummaryAvoid disrupting production by keeping track of dependent procedures and recompiling them manually as soon as possible after the definition of a database object changes.

Page 112: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units 12 - 40

Copyright © 2009, Oracle. All rights reserved.12 - 40

Practice 12 Overview: Managing Dependencies in Your Schema

This practice covers the following topics:• Using DEPTREE_FILL and IDEPTREE to view

dependencies• Recompiling procedures, functions, and packages

Practice 12: OverviewIn this practice, you use the DEPTREE_FILL procedure and the IDEPTREE view to investigate dependencies in your schema. In addition, you recompile invalid procedures, functions, packages, and views.

Page 113: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Appendix APractices and Solutions

Page 114: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 2

Table of Contents

Practices and Solutions for Lesson I................................................................................... 4 Practice I-1: Identifying the Available SQL Developer Resources ................................ 5 Practice I-2: Creating and Using a New SQL Developer Database Connection ............ 6 Practice I-3: Browsing Your Schema Tables and Creating and Executing a Simple Anonymous Block .......................................................................................................... 7 Practice I-4: Setting Some SQL Developer Preferences................................................. 8 Practice I-5: Accessing the Oracle Database 11g Release 2 Online Documentation Library............................................................................................................................. 9 Practice Solutions I-1: Identifying the Available SQL Developer Resources.............. 10 Practice Solutions I-2: Creating and Using a New SQL Developer Database Connection .................................................................................................................... 12 Practice Solutions I-3: Browsing Your Schema Tables and Creating and Executing a Simple Anonymous Block ............................................................................................ 15 Practice Solutions I-4: Setting Some SQL Developer Preferences .............................. 19 Practice Solutions I-5: Accessing the Oracle Database 11g Release 2 Online Documentation Library................................................................................................. 23

Practices and Solutions for Lesson 1 ................................................................................ 24 Practice 1-1: Creating, Compiling, and Calling Procedures ......................................... 25 Practice Solutions 1-1: Creating, Compiling, and Calling Procedures......................... 27

Practices and Solutions for Lesson 2 ................................................................................ 38 Practice 2-1: Creating Functions................................................................................... 39 Practice 2-2: Introduction to the SQL Developer Debugger ........................................ 41 Practice Solutions 2-1: Creating Functions................................................................... 42 Practice Solutions 2-2: Introduction to the SQL Developer Debugger ........................ 48

Practices and Solutions for Lesson 3 ................................................................................ 58 Practice 3-1: Creating and Using Packages .................................................................. 59 Practice Solutions 3-1: Creating and Using Packages .................................................. 61

Practices and Solutions for Lesson 4 ................................................................................ 68 Practice 4-1: Working with Packages ........................................................................... 69 Practice Solutions 4-1: Working with Packages........................................................... 73

Practices and Solutions for Lesson 5 .............................................................................. 102 Practice 5-1: Using the UTL_FILE Package .............................................................. 103 Practice Solutions 5-1: Using the UTL_FILE Package .............................................. 104

Practices and Solutions for Lesson 6 .............................................................................. 108 Practice 6-1: Using Native Dynamic SQL.................................................................. 109 Practice Solutions 6-1: Using Native Dynamic SQL.................................................. 111

Practices and Solutions for Lesson 7 .............................................................................. 121 Practice 7-1: Using Bulk Binding and Autonomous Transactions ............................. 122 Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions ............. 124

Practices and Solutions for Lesson 8 .............................................................................. 145 Practice 8-1: Creating Statement and Row Triggers................................................... 146 Practice Solutions 8-1: Creating Statement and Row Triggers .................................. 148

Practices and Solutions for Lesson 9 .............................................................................. 157 Practice 9-1: Managing Data Integrity Rules and Mutating Table Exceptions .......... 158

Page 115: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 3

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions..................................................................................................................................... 161

Practices and Solutions for Lesson 10 ............................................................................ 174 Practice 10-1: Using the PL/SQL Compiler Parameters and Warnings ..................... 175 Practice Solutions 10-1: Using the PL/SQL Compiler Parameters and Warnings ..... 176

Practices and Solutions for Lesson 11 ............................................................................ 185 Practice 11-1: Using Conditional Compilation........................................................... 186 Practice Solutions 11-1: Using Conditional Compilation........................................... 188

Practices and Solutions for Lesson 12 ............................................................................ 195 Practice 12-1: Managing Dependencies in Your Schema........................................... 196 Practice Solutions 12-1: Managing Dependencies in Your Schema .......................... 197

Page 116: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 4

Practices and Solutions for Lesson I

This is the first of many practices in this course. The solutions (if you require them) can be found in �Appendix A: Practices and Solutions.� Practices are intended to cover most of the topics that are presented in the corresponding lesson. Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

In this practice, you review the available SQL Developer resources. You also learn about your user account that you will use in this course. You then start SQL Developer, create a new database connection, browse your schema tables, and create and execute a simple anonymous block. You also set some SQL Developer preferences, execute SQL statements, and execute an anonymous PL/SQL block using SQL Worksheet. Finally, you access and bookmark the Oracle Database 11g documentation and other useful Web sites that you can use in this course.

Page 117: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 5

Practice I-1: Identifying the Available SQL Developer Resources

In this practice, you review the available SQL Developer resources.

1) Familiarize yourself with Oracle SQL Developer as needed using Appendix C: Using SQL Developer.

2) Access the SQL Developer Home Page available online at: http://www.oracle.com/technology/products/database/sql_developer/index.html

3) Bookmark the page for easier future access. 4) Access the SQL Developer tutorial available online at:

http://st-curriculum.oracle.com/tutorial/SQLDeveloper/index.htm 5) Preview and experiment with the available links and demos in the tutorial as needed,

especially the �Creating a Database Connection� and �Accessing Data� links.

Page 118: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 6

Practice I-2: Creating and Using a New SQL Developer Database Connection

In this practice, you start SQL Developer using your connection information and create a new database connection. 1) Start up SQL Developer using the user ID and password that are provided to you by

the instructor such as ±®¿êï.

2) Create a database connection using the following information:

a) Connection Name: Ó§ÜÞݱ²²»½¬·±²

b) Username: ±®¿êï

c) Password: ±®¿êï

d) Hostname: Enter the host name for your PC e) Port: 1521

f) SID: ÑÎÝÔ

3) Test the new connection. If the Status is Success, connect to the database using this new connection: a) Double-click the MyDBConnection icon on the Connections tabbed page.

b) Click the Test button in the New/Select Database Connection window. If the status is Success, click the Connect button.

Page 119: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 7

Practice I-3: Browsing Your Schema Tables and Creating and Executing a Simple Anonymous Block

In this practice, you browse your schema tables and create and execute a simple anonymous block

1) Browse the structure of the ÛÓÐÔÑÇÛÛÍ table and display its data.

a) Expand the MyDBConnection connection by clicking the plus sign next to it. b) Expand the Tables icon by clicking the plus sign next to it.

c) Display the structure of the ÛÓÐÔÑÇÛÛÍ table.

2) Browse the ÛÓÐÔÑÇÛÛÍ table and display its data.

3) Use the SQL Worksheet to select the last names and salaries of all employees whose annual salary is greater than $10,000. Use both the Execute Statement (F9) and the Run Script icon (F5) icons to execute the ÍÛÔÛÝÌ statement. Review the results of both methods of executing the ÍÛÔÛÝÌ statements in the appropriate tabs.

Note: Take a few minutes to familiarize yourself with the data, or consult Appendix B, which provides the description and data for all the tables in the ØÎ schema that you will use in this course.

4) Create and execute a simple anonymous block that outputs �Hello World.�

a) Enable ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ to display the output of the ÜÞÓÍÁÑËÌÐËÌ package statements.

b) Use the SQL Worksheet area to enter the code for your anonymous block. c) Click the Run Script (F5) icon to run the anonymous block.

Page 120: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 8

Practice I-4: Setting Some SQL Developer Preferences

In this practice, you set some SQL Developer preferences. 1) In the SQL Developer menu, navigate to Tools > Preferences. The Preferences

window is displayed. 2) Expand the Code Editor option, and then click the Display option to display the

�Code Editor: Display� section. The �Code Editor: Display� section contains general options for the appearance and behavior of the code editor.

a) Enter ïðð in the Right Margin Column text box in the Show Visible Right Margin section. This renders a right margin that you can set to control the length of lines of code.

b) Click the Line Gutter option. The Line Gutter option specifies options for the line gutter (left margin of the code editor). Select the Show Line Numbers check box to display the code line numbers.

3) Click the Worksheet Parameters option under the Database option. In the �Select default path to look for scripts� text box, specify the /¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°« folder. This folder contains the solutions scripts, code examples scripts, and any labs or demos used in this course.

4) Click OK to accept your changes and to exit the Preferences window.

5) Familiarize yourself with the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°« folder.

a) Click the Files tab (next to the Connections tab).

b) Navigate to the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°« folder. How many subfolders do you see in the labs folder?

c) Navigate through the folders, and open a script file without executing the code.

d) Clear the displayed code in the SQL Worksheet area.

Page 121: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 9

Practice I-5: Accessing the Oracle Database 11g Release 2 Online Documentation Library

In this practice, you access and bookmark some of the Oracle Database 11g Release 2 documentation references that you will use in this course. 1) Access the Oracle Database 11g Release 2 documentation Web page at:

http://www.oracle.com/pls/db111/homepage. 2) Bookmark the page for easier future access.

3) Display the complete list of books available for Oracle Database 11g Release 2. 4) Make a note of the following documentation references that you will use in this

course as needed:

a) Advanced Application Developer�s Guide b) New Features Guide c) PL/SQL Language Reference d) Oracle Database Reference e) Oracle Database Concepts f) SQL Developer User�s Guide g) SQL Language Reference Guide

h) SQL*Plus User�s Guide and Reference

Page 122: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 10

Practice Solutions I-1: Identifying the Available SQL Developer Resources In this practice, you review the available SQL Developer resources.

1) Familiarize yourself with Oracle SQL Developer as needed using Appendix C: Using SQL Developer.

2) Access the online SQL Developer Home Page available online at: http://www.oracle.com/technology/products/database/sql_developer/index.html

The SQL Developer Home page is displayed as follows:

3) Bookmark the page for easier future access.

No formal solution. The link is added to your Links toolbar as follows: 4) Access the SQL Developer tutorial available online at:

http://st-curriculum.oracle.com/tutorial/SQLDeveloper/index.htm

Access the SQL Developer tutorial using the preceding URL. The following page is displayed:

Page 123: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions I-1: Identifying the Available SQL Developer Resources (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 11

5) Preview and experiment with the available links and demos in the tutorial as needed, especially the �Creating a Database Connection� and �Accessing Data� links. To review the section on creating a database connection, click the plus �+� sign next to the �What to Do First� link to display the �Creating a Database Connection� link. To review the Creating a Database Connection topic, click the topic�s link. To review the section on accessing data, click the plus �+� sign next to the �Accessing Data� link to display the list of available topics. To review any of the topics, click the topic�s link.

Page 124: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 12

Practice Solutions I-2: Creating and Using a New SQL Developer Database Connection

In this practice, you start SQL Developer using your connection information and create a new database connection. 1) Start up SQL Developer using the user ID and password that are provided to you by

the instructor such as ±®¿êï.

Click the SQL Developer icon on your desktop.

2) Create a database connection using the following information:

a) Connection Name: Ó§ÜÞݱ²²»½¬·±²

b) Username: ±®¿êï

c) Password: ±®¿êï

d) Hostname: Enter the host name for your PC

e) Port: 1521

f) SID: ÑÎÝÔ

Page 125: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions I-2: Creating and Using a New SQL Developer Database Connection (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 13

Right-click the Connections icon on the Connections tabbed page, and then select the New Connection option from the shortcut menu. The New/Select Database Connection window is displayed. Use the preceding information provided to create the new database connection. Note: To display the properties of the newly created connection, right-click the connection name, and then select Properties from the shortcut menu. Substitute the username, password, host name, and service name with the appropriate information as provided by your instructor. The following is a sample of the newly created database connection for student ora61:

3) Test the new connection. If the Status is Success, connect to the database using this

new connection: a) Double-click the MyDBConnection icon on the Connections tabbed page.

b) Click the Test button in the New/Select Database Connection window. If the status is Success, click the Connect button.

Page 126: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions I-2: Creating and Using a New SQL Developer Database Connection (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 14

Page 127: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 15

Practice Solutions I-3: Browsing Your Schema Tables and Creating and Executing a Simple Anonymous Block

In this practice, you browse your schema tables and create and execute a simple anonymous block.

1) Browse the structure of the ÛÓÐÔÑÇÛÛÍ table and display its data.

a) Expand the Ó§ÜÞݱ²²»½¬·±² connection by clicking the plus sign next to it.

b) Expand the Tables icon by clicking the plus sign next to it.

c) Display the structure of the ÛÓÐÔÑÇÛÛÍ table.

Double-click the ÛÓÐÔÑÇÛÛÍ table. The ݱ´«³²­ tab displays the columns in the ÛÓÐÔÑÇÛÛÍ table as follows:

2) Browse the ÛÓÐÔÑÇÛÛÍ table and display its data.

To display the employees� data, click the Data tab. The ÛÓÐÔÑÇÛÛÍ table data is displayed as follows:

Page 128: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions I-3: Browsing Your Schema Tables and Creating and Executing a Simple Anonymous Block (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 16

3) Use the SQL Worksheet to select the last names and salaries of all employees whose

annual salary is greater than $10,000. Use both the Execute Statement (F9) and the Run Script icon (F5) icons to execute the ÍÛÔÛÝÌ statement. Review the results of both methods of executing the ÍÛÔÛÝÌ statements in the appropriate tabs.

Note: Take a few minutes to familiarize yourself with the data, or consult Appendix B, which provides the description and data for all the tables in the ØÎ schema that you will use in this course. Display the SQL Worksheet using one of the following two methods:

1. Select Tools > SQL Worksheet or click the Open SQL Worksheet icon. The Select Connection window is displayed.

2. Select the new Ó§ÜÞݱ²²»½¬·±² from the Connection drop-down list (if not already selected), and then click OK.

Open the ­±´Á×Áðíò­¯´ file in the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ folder as follows using one of the following two methods:

1. In the Files tab, select (or navigate to) the script file that you want to

open. 2. Double-click the file name to open. The code of the script file is displayed

in the SQL Worksheet area.

Page 129: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions I-3: Browsing Your Schema Tables and Creating and Executing a Simple Anonymous Block (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 17

3. To run the code, click the Run Script (F5) icon on the SQL Worksheet toolbar.

Alternatively you can also: 1. Select Open from the File menu. The Open dialog box is displayed. 2. In the Open dialog box, select (or navigate to) the script file that you want

to open. 3. Click Open. The code of the script file is displayed in the SQL Worksheet

area. 4. To run the code, click the Run Script (F5) icon on the SQL Worksheet

toolbar.

To run a single ÍÛÔÛÝÌ statement, click the Execute Statement (F9) icon (while making sure the cursor is on any of the ÍÛÔÛÝÌ statement lines) on the SQL Worksheet toolbar to execute the statement. The code and the result are displayed as follows:

ÍÛÔÛÝÌ ÔßÍÌÁÒßÓÛô ÍßÔßÎÇ ÚÎÑÓ ÛÓÐÔÑÇÛÛÍ ÉØÛÎÛ ÍßÔßÎÇ â ïððððå

4) Create and execute a simple anonymous block that outputs �Hello World.�

a) Enable ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ to display the output of the ÜÞÓÍÁÑËÌÐËÌ package statements.

Enter the following command in the SQL Worksheet area, and then click the Run Script (F5) icon.

Page 130: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions I-3: Browsing Your Schema Tables and Creating and Executing a Simple Anonymous Block (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 18

ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ

b) Use the SQL Worksheet area to enter the code for your anonymous block.

Enter the following code in the SQL Worksheet area as shown below. Alternatively, open the ­±´Á×Áðìò­¯´ file in the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ folder. The code is displayed as follows:

c) Click the Run Script (F5) icon to run the anonymous block.

The Script Output tab displays the output of the anonymous block as follows:

Page 131: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 19

Practice Solutions I-4: Setting Some SQL Developer Preferences In this practice, you set some SQL Developer preferences.

1) In the SQL Developer menu, navigate to Tools > Preferences. The Preferences window is displayed.

2) Expand the Code Editor option, and then click the Display option to display the

�Code Editor: Display� section. The �Code Editor: Display� section contains general options for the appearance and behavior of the code editor.

a) Enter ïðð in the Right Margin Column text box in the Show Visible Right Margin section. This renders a right margin that you can set to control the length of lines of code.

Page 132: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions I-4: Setting Some SQL Developer Preferences (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 20

b) Click the Line Gutter option. The Line Gutter option specifies options for the line gutter (left margin of the code editor). Select the Show Line Numbers check box to display the code line numbers.

Page 133: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions I-4: Setting Some SQL Developer Preferences (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 21

3) Click the Worksheet Parameters option under the Database option. In the �Select default path to look for scripts� text box, specify the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°« folder. This folder contains the solutions scripts, code examples scripts, and any labs or demos used in this course.

Page 134: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions I-4: Setting Some SQL Developer Preferences (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 22

4) Click OK to accept your changes and to exit the Preferences window.

5) Familiarize yourself with the labs folder on the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°« folder. a) Click the Files tab (next to the Connections tab).

b) Navigate to the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°« folder.

c) How many subfolders do you see in the labs folder? d) Navigate through the folders, and open a script file without executing the code.

e) Clear the displayed code in the SQL Worksheet area. In the SQL Developer menu, navigate to Tools > Preferences.

Page 135: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 23

Practice Solutions I-5: Accessing the Oracle Database 11g Release 2 Online Documentation Library

In this practice, you access and bookmark some of the Oracle Database 11g Release 2 documentation references that you will use in this course. 1) Access the Oracle Database 11g Release 2 documentation Web page at:

http://www.oracle.com/pls/db111/homepage 2) Bookmark the page for easier future access.

3) Display the complete list of books available for Oracle Database 11g Release 2. 4) Make a note of the following documentation references that you will use in this

course as needed:

a) Advanced Application Developer�s Guide b) New Features Guide c) PL/SQL Language Reference d) Oracle Database Reference e) Oracle Database Concepts f) SQL Developer User�s Guide g) SQL Language Reference Guide

h) SQL*Plus User�s Guide and Reference

Page 136: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 24

Practices and Solutions for Lesson 1

In this practice, you create, compile, and invoke procedures that issue DML and query commands. You also learn how to handle exceptions in procedures. Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

Page 137: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 25

Practice 1-1: Creating, Compiling, and Calling Procedures

In this practice, you create and invoke the ßÜÜÁÖÑÞ procedure and review the results. You also create and invoke a procedure called ËÐÜÁÖÑÞ to modify a job in the ÖÑÞÍ table and create and invoke a procedure called ÜÛÔÁÖÑÞ to delete a job from the ÖÑÞÍ table. Finally, you create a procedure called ÙÛÌÁÛÓÐÔÑÇÛÛ to query the ÛÓÐÔÑÇÛÛÍ table, retrieving the salary and job ID for an employee when provided with the employee ID.

1) Create, compile, and invoke the ßÜÜÁÖÑÞ procedure and review the results.

a) Create a procedure called ßÜÜÁÖÑÞ to insert a new job into the ÖÑÞÍ table. Provide the ID and job title using two parameters. Note: You can create the procedure (and other objects) by entering the code in the SQL Worksheet area, and then click the Run Script (F5) icon. This creates and compiles the procedure. To find out whether or not the procedure has any errors, click the procedure name in the procedure node, and then select Compile from the pop-up menu.

b) Invoke the procedure with ×ÌÁÜÞß as the job ID and Ü¿¬¿¾¿­» ß¼³·²·­¬®¿¬±® as the job title. Query the ÖÑÞÍ table and view the results.

c) Invoke your procedure again, passing a job ID of ÍÌÁÓßÒ and a job title of ͬ±½µ Ó¿²¿¹»®. What happens and why?

2) Create a procedure called ËÐÜÁÖÑÞ to modify a job in the ÖÑÞÍ table.

a) Create a procedure called ËÐÜÁÖÑÞ to update the job title. Provide the job ID and a new title using two parameters. Include the necessary exception handling if no update occurs.

b) Invoke the procedure to change the job title of the job ID ×ÌÁÜÞß to Ü¿¬¿ ß¼³·²·­¬®¿¬±®. Query the ÖÑÞÍ table and view the results.

c) Test the exception-handling section of the procedure by trying to update a job that does not exist. You can use the job ID ×ÌÁÉÛÞ and the job title É»¾ Ó¿­¬»®.

Page 138: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 1-1: Creating, Compiling, and Calling Procedures (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 26

3) Create a procedure called ÜÛÔÁÖÑÞ to delete a job from the ÖÑÞÍ table.

a) Create a procedure called ÜÛÔÁÖÑÞ to delete a job. Include the necessary exception-handling code if no job is deleted.

b) Invoke the procedure using the job ID ×ÌÁÜÞß. Query the ÖÑÞÍ table and view the results.

c) Test the exception-handling section of the procedure by trying to delete a job that does not exist. Use ×ÌÁÉÛÞ as the job ID. You should get the message that you included in the exception-handling section of the procedure as the output.

4) Create a procedure called ÙÛÌÁÛÓÐÔÑÇÛÛ to query the ÛÓÐÔÑÇÛÛÍ table, retrieving the salary and job ID for an employee when provided with the employee ID.

a) Create a procedure that returns a value from the ÍßÔßÎÇ and ÖÑÞÁ×Ü columns for a specified employee ID. Remove syntax errors, if any, and then recompile the code.

b) Execute the procedure using host variables for the two ÑËÌ parameters�one for the salary and the other for the job ID. Display the salary and job ID for employee ID 120.

c) Invoke the procedure again, passing an ÛÓÐÔÑÇÛÛÁ×Ü of íðð. What happens and why?

Page 139: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 27

Practice Solutions 1-1: Creating, Compiling, and Calling Procedures

In this practice, you create and invoke the ßÜÜÁÖÑÞ procedure and review the results. You also create and invoke a procedure called ËÐÜÁÖÑÞ to modify a job in the ÖÑÞÍ table and create and invoke a procedure called ÜÛÔÁÖÑÞ to delete a job from the ÖÑÞÍ table. Finally, you create a procedure called ÙÛÌÁÛÓÐÔÑÇÛÛ to query the ÛÓÐÔÑÇÛÛÍ table, retrieving the salary and job ID for an employee when provided with the employee ID.

1) Create, compile, and invoke the ßÜÜÁÖÑÞ procedure and review the results.

a) Create a procedure called ßÜÜÁÖÑÞ to insert a new job into the ÖÑÞÍ table. Provide the ID and job title using two parameters. Note: You can create the procedure (and other objects) by entering the code in the SQL Worksheet area, and then click the Run Script (F5) icon. This creates and compiles the procedure. If the procedure generates an error message when you create it, click the procedure name in the procedure node, edit the procedure, and then select Compile from the pop-up menu.

Open the ­±´ÁðïÁðïÁ¿ò­¯´ file in the ñ¸±³»ñ±®¿½´» ´¿¾­ñ°´°«ñ­±´²­ folder. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the procedure. The code and the result are displayed as follows:

To view the newly created procedure, click the Procedures node in the Object Navigator. If the newly created procedure is not displayed, right-click

Page 140: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-1: Creating, Compiling, and Calling Procedures (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 28

the Procedures node, and then select Refresh from the shortcut menu. The new procedure is displayed as follows:

b) Invoke the procedure with ×ÌÁÜÞß as the job ID and Ü¿¬¿¾¿­» ß¼³·²·­¬®¿¬±® as the job title. Query the ÖÑÞÍ table and view the results.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²ñ­±´ÁðïÁðïÁ¾ò­¯´ script. The code and the result are displayed as follows:

c) Invoke your procedure again, passing a job ID of ÍÌÁÓßÒ and a job title of ͬ±½µ Ó¿²¿¹»®. What happens and why?

Page 141: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-1: Creating, Compiling, and Calling Procedures (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 29

An exception occurs because there is a Unique key integrity constraint on the ÖÑÞÁ×Ü column.

2) Create a procedure called ËÐÜÁÖÑÞ to modify a job in the ÖÑÞÍ table.

a) Create a procedure called ËÐÜÁÖÑÞ to update the job title. Provide the job ID and a new title using two parameters. Include the necessary exception handling if no update occurs.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²ñ­±´ÁðïÁðîÁ¿ò­¯´ script. The code and the result are displayed as follows:

Page 142: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-1: Creating, Compiling, and Calling Procedures (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 30

b) Invoke the procedure to change the job title of the job ID ×ÌÁÜÞß to Ü¿¬¿ ß¼³·²·­¬®¿¬±®. Query the ÖÑÞÍ table and view the results.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²ñ­±´ÁðïÁðîÁ¾ò­¯´ script. The code and the result are displayed as follows:

c) Test the exception-handling section of the procedure by trying to update a job that does not exist. You can use the job ID ×ÌÁÉÛÞ and the job title É»¾ Ó¿­¬»®.

Page 143: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-1: Creating, Compiling, and Calling Procedures (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 31

3) Create a procedure called ÜÛÔÁÖÑÞ to delete a job from the ÖÑÞÍ table.

a) Create a procedure called ÜÛÔÁÖÑÞ to delete a job. Include the necessary exception-handling code if no job is deleted.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²ñ­±´ÁðïÁðíÁ¿ò­¯´ script. The code and the result are displayed as follows:

Page 144: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-1: Creating, Compiling, and Calling Procedures (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 32

b) To invoke the procedure and then query the ÖÑÞÍ table, load the ­±´ÁðïÁðíÁ¾ò­¯´ file in the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ folder. Click the Run Script (F5) icon on the SQL Worksheet toolbar to invoke the procedure. The code and the result are displayed as follows:

Page 145: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-1: Creating, Compiling, and Calling Procedures (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 33

c) Test the exception-handling section of the procedure by trying to delete a job that does not exist. Use ×ÌÁÉÛÞ as the job ID. You should get the message that you included in the exception-handling section of the procedure as the output.

To invoke the procedure and then query the ÖÑÞÍ table, load the ­±´ÁðïÁðíÁ½ò­¯´ file from the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ folder. Click the Run Script (F5) icon on the SQL Worksheet toolbar to invoke the procedure. The code and the result are displayed as follows:

Page 146: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-1: Creating, Compiling, and Calling Procedures (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 34

4) Create a procedure called ÙÛÌÁÛÓÐÔÑÇÛÛ to query the ÛÓÐÔÑÇÛÛÍ table, retrieving the salary and job ID for an employee when provided with the employee ID.

a) Create a procedure that returns a value from the ÍßÔßÎÇ and ÖÑÞÁ×Ü columns for a specified employee ID. Remove syntax errors, if any, and then recompile the code.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðïÁðìÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the procedure. The code and the result are displayed as follows:

Page 147: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-1: Creating, Compiling, and Calling Procedures (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 35

Note If the newly created procedure is not displayed in the Object Navigator, right-click the Procedures node in the Object Navigator, and then select Refresh from the shortcut menu. Right-click the procedure�s name in the Object Navigator, and then select Compile from the shortcut menu. The procedure is compiled.

b) Execute the procedure using host variables for the two ÑËÌ parameters�one for the salary and the other for the job ID. Display the salary and job ID for employee ID 120.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðïÁðìÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to invoke the procedure. The code and the result are displayed as follows:

Page 148: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-1: Creating, Compiling, and Calling Procedures (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 36

c) Invoke the procedure again, passing an ÛÓÐÔÑÇÛÛÁ×Ü of íðð. What happens and why?

There is no employee in the ÛÓÐÔÑÇÛÛÍ table with an ÛÓÐÔÑÇÛÛÁ×Ü of 300. The ÍÛÔÛÝÌ statement retrieved no data from the database, resulting in a fatal PL/SQL error: ÒÑÁÜßÌßÁÚÑËÒÜ as follows:

Page 149: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-1: Creating, Compiling, and Calling Procedures (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 37

Page 150: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 38

Practices and Solutions for Lesson 2

In practice 2-1, you create, compile, and use the following:

  A function called ÙÛÌÁÖÑÞ to return a job title.   A function called ÙÛÌÁßÒÒËßÔÁÝÑÓÐ to return the annual salary computed from an

employee�s monthly salary and commission passed as parameters.   A procedure called ßÜÜÁÛÓÐÔÑÇÛÛ to insert a new employee into the ÛÓÐÔÑÇÛÛÍ

table. In practice 2-2, you are introduced to the basic functionality of the SQL Developer debugger:   Create a procedure and a function.   Insert breakpoints in the newly created procedure.   Compile the procedure and function for debug mode.   Debug the procedure and step into the code.   Display and modify the subprograms� variables.

Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

Page 151: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 39

Practice 2-1: Creating Functions

In this practice, you create, compile, and use stored functions and a procedure.

1) Create and invoke the ÙÛÌÁÖÑÞ function to return a job title.

a) Create and compile a function called ÙÛÌÁÖÑÞ to return a job title.

b) Create a ÊßÎÝØßÎî host variable called ¾Á¬·¬´», allowing a length of 35 characters. Invoke the function with job ID ÍßÁÎÛÐ to return the value in the host variable, and then print the host variable to view the result.

2) Create a function called ÙÛÌÁßÒÒËßÔÁÝÑÓÐ to return the annual salary computed from an employee�s monthly salary and commission passed as parameters.

a) Create the ÙÛÌÁßÒÒËßÔÁÝÑÓÐ function, which accepts parameter values for the monthly salary and commission. Either or both values passed can be ÒËÔÔ, but the function should still return a non-ÒËÔÔ annual salary. Use the following basic formula to calculate the annual salary: ø­¿´¿®§öïî÷ õ ø½±³³·­­·±²Á°½¬ö­¿´¿®§öïî÷

b) Use the function in a ÍÛÔÛÝÌ statement against the ÛÓÐÔÑÇÛÛÍ table for employees in department 30.

3) Create a procedure, ßÜÜÁÛÓÐÔÑÇÛÛ, to insert a new employee into the ÛÓÐÔÑÇÛÛÍ table. The procedure should call a ÊßÔ×ÜÁÜÛÐÌ×Ü function to check whether the department ×Ü specified for the new employee exists in the ÜÛÐßÎÌÓÛÒÌÍ table.

Page 152: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 40

a) Create a function called ÊßÔ×ÜÁÜÛÐÌ×Ü to validate a specified department ID and return a ÞÑÑÔÛßÒ value of ÌÎËÛ if the department exists.

b) Create the ßÜÜÁÛÓÐÔÑÇÛÛ procedure to add an employee to the ÛÓÐÔÑÇÛÛÍ table. The row should be added to the ÛÓÐÔÑÇÛÛÍ table if the ÊßÔ×ÜÁÜÛÐÌ×Ü function returns ÌÎËÛ; otherwise, alert the user with an appropriate message. Provide the following parameters:

- º·®­¬Á²¿³»

- ´¿­¬Á²¿³»

- »³¿·´

- ¶±¾: Use ùÍßÁÎÛÐù as the default.

- ³¹®: Use 145 as the default.

- ­¿´: Use 1000 as the default.

- ½±³³: Use 0 as the default.

- ¼»°¬·¼: Use 30 as the default.

- Use the ÛÓÐÔÑÇÛÛÍÁÍÛÏ sequence to set the »³°´±§»»Á·¼ column.

- Set the ¸·®»Á¼¿¬» column to ÌÎËÒÝøÍÇÍÜßÌÛ÷.

c) Call ßÜÜÁÛÓÐÔÑÇÛÛ for the name ùÖ¿²» Ø¿®®·­ù in department ïë, leaving other parameters with their default values. What is the result?

d) Add another employee named Joe Harris in department 80, leaving the remaining parameters with their default values. What is the result?

Page 153: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 41

Practice 2-2: Introduction to the SQL Developer Debugger

In this practice, you experiment with the basic functionality of the SQL Developer debugger.

1) Enable ÍÛÎÊÛÎÑËÌÐËÌ.

2) Run the ­±´ÁðîÁðîÁðîò­¯´ script to create the »³°Á´·­¬ procedure. Examine the code of the procedure and compile the procedure. Why do you get the compiler error?

3) Run the ­±´ÁðîÁðîÁðíò­¯´ script to create the ¹»¬Á´±½¿¬·±² function. Examine the code of the function, compile the function, and then correct any errors, if any.

4) Re-compile the »³°Á´·­¬ procedure. The procedure should compile successfully.

5) Edit the »³°Á´·­¬ procedure and the ¹»¬Á´±½¿¬·±² function.

6) Add four breakpoints to the »³°Á´·­¬ procedure to the following lines of code: ¿÷ ÑÐÛÒ »³°Á½«®­±®å

¾÷ ÉØ×ÔÛ ø»³°Á½«®­±®ûÚÑËÒÜ÷ ßÒÜ ø· äã °Ó¿¨Î±©­÷ ÔÑÑÐ

½÷ ªÁ½·¬§ æã ¹»¬Á´±½¿¬·±² ø»³°Á®»½±®¼ò¼»°¿®¬³»²¬Á²¿³»÷å

¼÷ ÝÔÑÍÛ »³°Á½«®­±®å

7) Compile the »³°Á´·­¬ procedure for debugging.

8) Debug the procedure.

9) Enter 100 as the value of the ÐÓßÈÎÑÉÍ parameter.

10) Examine the value of the variables in the Data tab. What are the values assigned to ÎÛÝÁÛÓÐ and ÛÓÐÁÌßÞ? Why?

11) Use the Step Into debug option to step into each line of code in »³°Á´·­¬ and go through the while loop once only.

12) Examine the value of the variables in the Data tab. What are the values assigned to ÎÛÝÁÛÓÐ?

13) Continue pressing F7 until the »³°Á¬¿¾ø·÷ æã ®»½Á»³°å line is executed. Examine the value of the variables in the Data tab. What are the values assigned to ÛÓÐÁÌßÞ?

14) Use the Data tab to modify the value of the counter i to 98.

15) Continue pressing F7 until you observe the list of employees displayed in the Debugging � Log tab. How many employees are displayed?

16) If you use the Step Over debugger option to step through the code, do you step through the ¹»¬Á´±½¿¬·±² function? Why or why not?

Page 154: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 42

Practice Solutions 2-1: Creating Functions

In this practice, you create, compile, and use stored functions and a procedure.

1) Create and invoke the ÙÛÌÁÖÑÞ function to return a job title.

a) Create and compile a function called ÙÛÌÁÖÑÞ to return a job title.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðîÁðïÁðïÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the function. The code and the result are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÚËÒÝÌ×ÑÒ ¹»¬Á¶±¾ ø°Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼û¬§°»÷ ÎÛÌËÎÒ ¶±¾­ò¶±¾Á¬·¬´»û¬§°» ×Í ªÁ¬·¬´» ¶±¾­ò¶±¾Á¬·¬´»û¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ¶±¾Á¬·¬´» ×ÒÌÑ ªÁ¬·¬´» ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã °Á¶±¾·¼å ÎÛÌËÎÒ ªÁ¬·¬´»å ÛÒÜ ¹»¬Á¶±¾å ñ

b) Create a ÊßÎÝØßÎî host variable called ¾Á¬·¬´», allowing a length of 35 characters. Invoke the function with job ID ÍßÁÎÛÐ to return the value in the host variable, and then print the host variable to view the result.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðîÁðïÁðïÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the function. The code and the result are displayed as follows:

ÊßÎ×ßÞÔÛ ¾Á¬·¬´» ÊßÎÝØßÎîøíë÷ ÛÈÛÝËÌÛ æ¾Á¬·¬´» æã ¹»¬Á¶±¾ øùÍßÁÎÛÐù÷å ÐÎ×ÒÌ ¾Á¬·¬´»

Page 155: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating Functions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 43

2) Create a function called ÙÛÌÁßÒÒËßÔÁÝÑÓÐ to return the annual salary computed from an employee�s monthly salary and commission passed as parameters.

a) Create the ÙÛÌÁßÒÒËßÔÁÝÑÓÐ function, which accepts parameter values for the monthly salary and commission. Either or both values passed can be ÒËÔÔ, but the function should still return a non-ÒËÔÔ annual salary. Use the following basic formula to calculate the annual salary: ø­¿´¿®§öïî÷ õ ø½±³³·­­·±²Á°½¬ö­¿´¿®§öïî÷

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ /­±´ÁðîÁðïÁðîÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the function. The code and the result are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÚËÒÝÌ×ÑÒ ¹»¬Á¿²²«¿´Á½±³°ø °Á­¿´ ×Ò »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á½±³³ ×Ò »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ÷ ÎÛÌËÎÒ ÒËÓÞÛÎ ×Í ÞÛÙ×Ò ÎÛÌËÎÒ øÒÊÔø°Á­¿´ôð÷ ö ïî õ øÒÊÔø°Á½±³³ôð÷ ö ²ª´ø°Á­¿´ôð÷ ö ïî÷÷å ÛÒÜ ¹»¬Á¿²²«¿´Á½±³°å ñ

b) Use the function in a ÍÛÔÛÝÌ statement against the ÛÓÐÔÑÇÛÛÍ table for employees in department 30.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðîÁðïÁðîÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the function. The code and the result are displayed as follows:

Page 156: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating Functions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 44

ÍÛÔÛÝÌ »³°´±§»»Á·¼ô ´¿­¬Á²¿³»ô ¹»¬Á¿²²«¿´Á½±³°ø­¿´¿®§ô½±³³·­­·±²Á°½¬÷ þß²²«¿´ ݱ³°»²­¿¬·±²þ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ãíð ñ

3) Create a procedure, ßÜÜÁÛÓÐÔÑÇÛÛ, to insert a new employee into the ÛÓÐÔÑÇÛÛÍ table. The procedure should call a ÊßÔ×ÜÁÜÛÐÌ×Ü function to check whether the department ×Ü specified for the new employee exists in the ÜÛÐßÎÌÓÛÒÌÍ table.

a) Create a function called ÊßÔ×ÜÁÜÛÐÌ×Ü to validate a specified department ID and return a ÞÑÑÔÛßÒ value of ÌÎËÛ if the department exists.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðîÁðïÁðíÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create the function. The code and the result are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø °Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå

ÞÛÙ×Ò ÍÛÔÛÝÌ ï ×ÒÌÑ ªÁ¼«³³§ ÚÎÑÓ ¼»°¿®¬³»²¬­ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ ã °Á¼»°¬·¼å ÎÛÌËÎÒ ÌÎËÛå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å ñ

Page 157: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating Functions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 45

b) Create the ßÜÜÁÛÓÐÔÑÇÛÛ procedure to add an employee to the ÛÓÐÔÑÇÛÛÍ table. The row should be added to the ÛÓÐÔÑÇÛÛÍ table if the ÊßÔ×ÜÁÜÛÐÌ×Ü function returns ÌÎËÛ; otherwise, alert the user with an appropriate message. Provide the following parameters:

- º·®­¬Á²¿³»

- ´¿­¬Á²¿³»

- »³¿·´

- ¶±¾: Use ùÍßÁÎÛÐù as the default.

- ³¹®: Use 145 as the default.

- ­¿´: Use 1000 as the default.

- ½±³³: Use 0 as the default.

- ¼»°¬·¼: Use 30 as the default.

- Use the ÛÓÐÔÑÇÛÛÍÁÍÛÏ sequence to set the »³°´±§»»Á·¼ column.

- Set the ¸·®»Á¼¿¬» column to ÌÎËÒÝøÍÇÍÜßÌÛ÷.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðîÁðïÁðíÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the procedure. The code and the result are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í ÞÛÙ×Ò ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô

Page 158: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating Functions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 46

¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷ ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å ñ

c) Call ßÜÜÁÛÓÐÔÑÇÛÛ for the name ùÖ¿²» Ø¿®®·­ù in department ïë, leaving other parameters with their default values. What is the result?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðîÁðïÁðíÁ½ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to invoke the procedure. The code and the result are displayed as follows: EXECUTE add_employee('Jane', 'Harris', 'JAHARRIS', p_deptid=> 15)

d) Add another employee named Joe Harris in department 80, leaving the remaining

parameters with their default values. What is the result?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðîÁðïÁðíÁ¼ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to invoke the procedure. The code and the result are displayed as follows:

Page 159: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating Functions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 47

ÛÈÛÝËÌÛ ¿¼¼Á»³°´±§»»øùÖ±»ùô ùØ¿®®·­ùô ùÖßØßÎÎ×Íùô °Á¼»°¬·¼ãâ èð÷

Page 160: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 48

Practice Solutions 2-2: Introduction to the SQL Developer Debugger

In this practice, you experiment with the basic functionality of the SQL Developer debugger.

1) Enable ÍÛÎÊÛÎÑËÌÐËÌ.

Enter the following command in the SQL Worksheet area, and then click the Run Script (F5) Click the icon on the SQL Worksheet toolbar. ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ

2) Run the ­±´ÁðîÁðîÁðîò­¯´ script to create the »³°Á´·­¬ procedure. Examine the code of the procedure and compile the procedure. Why do you get the compiler error?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðîÁðîÁðîò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the procedure. The codex and the result are displayed as follows: ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ »³°Á´·­¬ ø°Á³¿¨®±©­ ×Ò ÒËÓÞÛÎ÷ ×Í ÝËÎÍÑÎ ½«®Á»³° ×Í ÍÛÔÛÝÌ ¼ò¼»°¿®¬³»²¬Á²¿³»ô »ò»³°´±§»»Á·¼ô »ò´¿­¬Á²¿³»ô »ò­¿´¿®§ô »ò½±³³·­­·±²Á°½¬ ÚÎÑÓ ¼»°¿®¬³»²¬­ ¼ô »³°´±§»»­ » ÉØÛÎÛ ¼ò¼»°¿®¬³»²¬Á·¼ ã »ò¼»°¿®¬³»²¬Á·¼å ®»½Á»³° ½«®Á»³°ûÎÑÉÌÇÐÛå ÌÇÐÛ »³°Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ½«®Á»³°ûÎÑÉÌÇÐÛ ×ÒÜÛÈ ÞÇ Þ×ÒßÎÇÁ×ÒÌÛÙÛÎå »³°Á¬¿¾ »³°Á¬¿¾Á¬§°»å · ÒËÓÞÛÎ æã ïå ªÁ½·¬§ ÊßÎÝØßÎîøíð÷å ÞÛÙ×Ò ÑÐÛÒ ½«®Á»³°å ÚÛÌÝØ ½«®Á»³° ×ÒÌÑ ®»½Á»³°å »³°Á¬¿¾ø·÷ æã ®»½Á»³°å ÉØ×ÔÛ ø½«®Á»³°ûÚÑËÒÜ÷ ßÒÜ ø· äã °Á³¿¨®±©­÷ ÔÑÑÐ · æã · õ ïå ÚÛÌÝØ ½«®Á»³° ×ÒÌÑ ®»½Á»³°å »³°Á¬¿¾ø·÷ æã ®»½Á»³°å ªÁ½·¬§ æã ¹»¬Á´±½¿¬·±² ø®»½Á»³°ò¼»°¿®¬³»²¬Á²¿³»÷å ¼¾³­Á±«¬°«¬ò°«¬Á´·²»øùÛ³°´±§»» ù ¤¤ ®»½Á»³°ò´¿­¬Á²¿³» ¤¤ ù ©±®µ­ ·² ù ¤¤ ªÁ½·¬§ ÷å ÛÒÜ ÔÑÑÐå ÝÔÑÍÛ ½«®Á»³°å ÚÑÎ ¶ ×Ò ÎÛÊÛÎÍÛ ïòò· ÔÑÑÐ

Page 161: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-2: Introduction to the SQL Developer Debugger (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 49

ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø»³°Á¬¿¾ø¶÷ò´¿­¬Á²¿³»÷å ÛÒÜ ÔÑÑÐå ÛÒÜ »³°Á´·­¬å ñ

The compilation warning is because the ¹»¬Á´±½¿¬·±² function is not yet declared. To display the compile error in more detail, right-click the ÛÓÐÁÔ×ÍÌ procedure in the Procedures node (you might need to refresh the procedures list in order to view the newly created ÛÓÐÁÔ×ÍÌ procedure), and then select Compile from the pop-up menu. The detailed warning message is displayed in the Compiler-Log tab as follows:

3) Run the ­±´ÁðîÁðîÁðíò­¯´ script to create the ¹»¬Á´±½¿¬·±² function. Examine the code of the function, compile the function, and then correct any errors, if any.

Page 162: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-2: Introduction to the SQL Developer Debugger (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 50

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðîÁðîÁðíò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the procedure. The codex and the result are displayed as follows: ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÚËÒÝÌ×ÑÒ ¹»¬Á´±½¿¬·±² ø °Á¼»°¬²¿³» ×Ò ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÊßÎÝØßÎî ßÍ ªÁ´±½Á·¼ ÒËÓÞÛÎå ªÁ½·¬§ ÊßÎÝØßÎîøíð÷å ÞÛÙ×Ò ÍÛÔÛÝÌ ¼ò´±½¿¬·±²Á·¼ô ´ò½·¬§ ×ÒÌÑ ªÁ´±½Á·¼ô ªÁ½·¬§ ÚÎÑÓ ¼»°¿®¬³»²¬­ ¼ô ´±½¿¬·±²­ ´ ÉØÛÎÛ «°°»®ø¼»°¿®¬³»²¬Á²¿³»÷ ã «°°»®ø°Á¼»°¬²¿³»÷ ¿²¼ ¼ò´±½¿¬·±²Á·¼ ã ´ò´±½¿¬·±²Á·¼å ÎÛÌËÎÒ ªÁ½·¬§å ÛÒÜ ÙÛÌÁÔÑÝßÌ×ÑÒå ñ

4) Recompile the »³°Á´·­¬ procedure. The procedure should compile successfully.

To recompile the procedure, right-click the procedure�s name, and then select Compile from the shortcut menu.

5) Edit the »³°Á´·­¬ procedure and the ¹»¬Á´±½¿¬·±² function.

Right-click the »³°Á´·­¬ procedure name in the Object Navigator, and then select Edit. The »³°Á´·­¬ procedure is opened in edit mode. If the procedure is already displayed in the SQL Worksheet area, but is in read-only mode, click the Edit icon (pencil icon) in the Code tab. Right-click the ¹»¬Á´±½¿¬·±² function name in the Object Navigator, and then select Edit. The ¹»¬Á´±½¿¬·±² function is opened in edit mode. If the function is already displayed in the SQL Worksheet area, but is in read-only mode, click the Edit icon (pencil icon) in the Code tab.

6) Add four breakpoints to the »³°Á´·­¬ procedure to the following lines of code:

Page 163: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-2: Introduction to the SQL Developer Debugger (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 51

¿÷ ÑÐÛÒ »³°Á½«®­±®å

¾÷ ÉØ×ÔÛ ø»³°Á½«®­±®ûÚÑËÒÜ÷ ßÒÜ ø· äã °Ó¿¨Î±©­÷ ÔÑÑÐ

½÷ ªÁ½·¬§ æã ¹»¬Á´±½¿¬·±² ø»³°Á®»½±®¼ò¼»°¿®¬³»²¬Á²¿³»÷å

¼÷ ÝÔÑÍÛ »³°Á½«®­±®å

To add a breakpoint, click the line gutter next to each of the lines listed above as shown below:

7) Compile the »³°Á´·­¬ procedure for debugging.

Click the �Compile for Debug� icon on the procedure�s toolbar as shown below:

Page 164: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-2: Introduction to the SQL Developer Debugger (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 52

Note: If you get the above warnings, it is expected. The first two warnings are because the ÐÔÍÏÔÁÜÛÞËÙ parameter was deprecated in Oracle Database 11g, while SQL Developer is still using that parameter. The last warning is in regards to using the ßËÌØ×Ü clause with a procedure. This clause will be discussed in a later lesson.

8) Debug the procedure.

Click the Debug icon on the procedure�s toolbar as shown below:

The Debug PL/SQL window is displayed as follows:

Page 165: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-2: Introduction to the SQL Developer Debugger (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 53

9) Enter 100 as the value of the ÐÓßÈÎÑÉÍ parameter.

Replace the second ÐÁÓßÈÎÑÉÍ with 100, and then click OK. Notice how the program control stops at the first breakpoint in the procedure as indicated by the blue highlight color and the red arrow pointing to that line of code. The additional debugging tabs are displayed at the bottom of the page.

Page 166: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-2: Introduction to the SQL Developer Debugger (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 54

10) Examine the value of the variables in the Data tab. What are the values assigned to ÎÛÝÁÛÓÐ and ÛÓÐÁÌßÞ? Why?

Both are set to ÒËÔÔ because the data is not yet fetched into the cursor.

Page 167: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-2: Introduction to the SQL Developer Debugger (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 55

11) Use the Step Into debug option to step in to each line of code in »³°Á´·­¬ and go through the while loop only once.

Press [F7] to step into the code only once. 12) Examine the value of the variables in the Data tab. What are the values assigned to

ÎÛÝÁÛÓÐ and ÛÓÐÁÌßÞ?

Note that when the line ÚÛÌÝØ ½«®Á»³° ×ÒÌÑ ®»½Á»³°; is executed, ®»½Á»³° is initialized as shown below:

13) Continue pressing F7 until the »³°Á¬¿¾ø·÷ æã ®»½Á»³°å line is executed. Examine the value of the variables in the Data tab. What are the values assigned to ÛÓÐÁÌßÞ?

When the line »³°Á¬¿¾ø·÷ æã ®»½Á»³°å is executed, »³°Á¬¿¾ is initialized to ®»½Á»³° as shown below:

Page 168: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-2: Introduction to the SQL Developer Debugger (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 56

14) Use the Data tab to modify the value of the counter i to 98.

In the Data tab, right-click I, and then select Modify Value from the shortcut menu. The Modify Value window is displayed. Replace the value ï with çè in the text box, and then click OK as shown below:

1

2

Page 169: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-2: Introduction to the SQL Developer Debugger (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 57

15) Continue pressing F7 until you observe the list of employees displayed in the Debugging � Log tab. How many employees are displayed?

The output at the end of the debugging session is shown below where it displays three employees:

16) If you use the Step Over debugger option to step through the code, do you step through the ¹»¬Á´±½¿¬·±² function? Why or why not?

Although the line of code where the third breakpoint is set contains a call to the ¹»¬Á´±½¿¬·±² function, the Step Over (F8) executes the line of code and retrieves the returned value of the function (same as [F7]); however, control is not transferred to the ¹»¬Á´±½¿¬·±² function.

Page 170: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 58

Practices and Solutions for Lesson 3

In this practice, you create a package specification and body called ÖÑÞÁÐÕÙ, containing a copy of your ßÜÜÁÖÑÞ, ËÐÜÁÖÑÞ, and ÜÛÔÁÖÑÞ procedures as well as your ÙÛÌÁÖÑÞ function. You also create and invoke a package that contains private and public constructs by using sample data.

Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

Page 171: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 59

Practice 3-1: Creating and Using Packages

In this practice, you create package specifications and package bodies. You then invoke the constructs in the packages by using sample data.

1) Create a package specification and body called ÖÑÞÁÐÕÙ, containing a copy of your ßÜÜÁÖÑÞ, ËÐÜÁÖÑÞ, and ÜÛÔÁÖÑÞ procedures as well as your ÙÛÌÁÖÑÞ function.

Note: Use the code from your previously saved procedures and functions when creating the package. You can copy the code in a procedure or function, and then paste the code into the appropriate section of the package.

a) Create the package specification including the procedures and function headings as public constructs.

b) Create the package body with the implementations for each of the subprograms. c) Delete the following stand-alone procedures and function you just packaged using

the Procedures and Functions nodes in the Object Navigation tree:

i) The ßÜÜÁÖÑÞ, ËÐÜÁÖÑÞ, and ÜÛÔÁÖÑÞ procedures

ii) The ÙÛÌÁÖÑÞ function

d) Invoke your ßÜÜÁÖÑÞ package procedure by passing the values ×ÌÁÍÇÍßÒ and ÍÇÍÌÛÓÍ ßÒßÔÇÍÌ as parameters.

e) Query the ÖÑÞÍ table to see the result.

2) Create and invoke a package that contains private and public constructs.

a) Create a package specification and a package body called ÛÓÐÁÐÕÙ that contains the following procedures and function that you created earlier:

i) ßÜÜÁÛÓÐÔÑÇÛÛ procedure as a public construct

ii) ÙÛÌÁÛÓÐÔÑÇÛÛ procedure as a public construct

iii) ÊßÔ×ÜÁÜÛÐÌ×Ü function as a private construct

b) Invoke the ÛÓÐÁÐÕÙòßÜÜÁÛÓÐÔÑÇÛÛ procedure, using department ID 15 for employee Jane Harris with the email ID ÖßØßÎÎ×Í. Because department ID 15 does not exist, you should get an error message as specified in the exception handler of your procedure.

Page 172: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 3-1: Creating and Using Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 60

c) Invoke the ßÜÜÁÛÓÐÔÑÇÛÛ package procedure by using department ID 80 for employee David Smith with the email ID ÜßÍÓ×ÌØ.

d) Query the ÛÓÐÔÑÇÛÛÍ table to verify that the new employee was added.

Page 173: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 61

Practice Solutions 3-1: Creating and Using Packages

In this practice, you create package specifications and package bodies. You then invoke the constructs in the packages by using sample data.

1) Create a package specification and body called ÖÑÞÁÐÕÙ, containing a copy of your ßÜÜÁÖÑÞ, ËÐÜÁÖÑÞ, and ÜÛÔÁÖÑÞ procedures as well as your ÙÛÌÁÖÑÞ function.

Note: Use the code from your previously saved procedures and functions when creating the package. You can copy the code in a procedure or function, and then paste the code into the appropriate section of the package.

a) Create the package specification including the procedures and function headings as public constructs.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðíÁðïÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the package specification. The code and the result are displayed as follows: ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ¶±¾Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ¿¼¼Á¶±¾ ø°Á¶±¾·¼ ¶±¾­ò¶±¾Á·¼ûÌÇÐÛô °Á¶±¾¬·¬´» ¶±¾­ò¶±¾Á¬·¬´»ûÌÇÐÛ÷å ÐÎÑÝÛÜËÎÛ ¼»´Á¶±¾ ø°Á¶±¾·¼ ¶±¾­ò¶±¾Á·¼ûÌÇÐÛ÷å ÚËÒÝÌ×ÑÒ ¹»¬Á¶±¾ ø°Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼û¬§°»÷ ÎÛÌËÎÒ ¶±¾­ò¶±¾Á¬·¬´»û¬§°»å ÐÎÑÝÛÜËÎÛ «°¼Á¶±¾ø°Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼ûÌÇÐÛô °Á¶±¾¬·¬´» ×Ò ¶±¾­ò¶±¾Á¬·¬´»ûÌÇÐÛ÷å ÛÒÜ ¶±¾Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

b) Create the package body with the implementations for each of the subprograms.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðíÁðïÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the package body. The code and the result are displayed as follows: ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ ¶±¾Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ¿¼¼Á¶±¾ ø °Á¶±¾·¼ ¶±¾­ò¶±¾Á·¼ûÌÇÐÛô °Á¶±¾¬·¬´» ¶±¾­ò¶±¾Á¬·¬´»ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò

Page 174: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 3-1: Creating and Using Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 62

×ÒÍÛÎÌ ×ÒÌÑ ¶±¾­ ø¶±¾Á·¼ô ¶±¾Á¬·¬´»÷ ÊßÔËÛÍ ø°Á¶±¾·¼ô °Á¶±¾¬·¬´»÷å ÝÑÓÓ×Ìå ÛÒÜ ¿¼¼Á¶±¾å

ÐÎÑÝÛÜËÎÛ ¼»´Á¶±¾ ø°Á¶±¾·¼ ¶±¾­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÜÛÔÛÌÛ ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã °Á¶±¾·¼å ×Ú ÍÏÔûÒÑÌÚÑËÒÜ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðîðíô ùÒ± ¶±¾­ ¼»´»¬»¼òù÷å ÛÒÜ ×Úå ÛÒÜ ÜÛÔÁÖÑÞå

ÚËÒÝÌ×ÑÒ ¹»¬Á¶±¾ ø°Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼û¬§°»÷ ÎÛÌËÎÒ ¶±¾­ò¶±¾Á¬·¬´»û¬§°» ×Í ªÁ¬·¬´» ¶±¾­ò¶±¾Á¬·¬´»û¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ¶±¾Á¬·¬´» ×ÒÌÑ ªÁ¬·¬´» ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã °Á¶±¾·¼å ÎÛÌËÎÒ ªÁ¬·¬´»å ÛÒÜ ¹»¬Á¶±¾å

ÐÎÑÝÛÜËÎÛ «°¼Á¶±¾ø °Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼ûÌÇÐÛô °Á¶±¾¬·¬´» ×Ò ¶±¾­ò¶±¾Á¬·¬´»ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ËÐÜßÌÛ ¶±¾­ ÍÛÌ ¶±¾Á¬·¬´» ã °Á¶±¾¬·¬´» ÉØÛÎÛ ¶±¾Á·¼ ã °Á¶±¾·¼å ×Ú ÍÏÔûÒÑÌÚÑËÒÜ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðîðîô ùÒ± ¶±¾ «°¼¿¬»¼òù÷å ÛÒÜ ×Úå ÛÒÜ «°¼Á¶±¾å

ÛÒÜ ¶±¾Á°µ¹å ñ

ÍØÑÉ ÛÎÎÑÎÍ

Page 175: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 3-1: Creating and Using Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 63

c) Delete the following stand-alone procedures and function you just packaged using the Procedures and Functions nodes in the Object Navigation tree:

i) The ßÜÜÁÖÑÞ, ËÐÜÁÖÑÞ, and ÜÛÔÁÖÑÞ procedures

ii) The ÙÛÌÁÖÑÞ function

To delete a procedure or a function, right-click the procedure�s name or function�s name in the Object Navigation tree, and then select Drop from the pop-up menu. The Drop window is displayed. Click Apply to drop the procedure or function. A confirmation window is displayed; click OK.

d) Invoke your ßÜÜÁÖÑÞ package procedure by passing the values ×ÌÁÍÇÍßÒ and ÍÇÍÌÛÓÍ ßÒßÔÇÍÌ as parameters.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­/­±´ÁðíÁðïÁ¼ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to invoke the package�s procedure. The code and the result are displayed as follows: ÛÈÛÝËÌÛ ¶±¾Á°µ¹ò¿¼¼Á¶±¾øù×ÌÁÍÇÍßÒùô ùͧ­¬»³­ ß²¿´§­¬ù÷

e) Query the ÖÑÞÍ table to see the result.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­/­±´ÁðíÁðïÁ»ò­¯´ script. Click the Run Script (F5) icon or the Execute Statement (F9) on the SQL Worksheet toolbar to query the ÖÑÞÍ table. The code and the result (using the Run Script icon) are displayed as follows:

ÍÛÔÛÝÌ ö ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã ù×ÌÁÍÇÍßÒùå

Page 176: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 3-1: Creating and Using Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 64

2) Create and invoke a package that contains private and public constructs.

a) Create a package specification and a package body called ÛÓÐÁÐÕÙ that contains the following procedures and function that you created earlier:

i) ßÜÜÁÛÓÐÔÑÇÛÛ procedure as a public construct

ii) ÙÛÌÁÛÓÐÔÑÇÛÛ procedure as a public construct

iii) ÊßÔ×ÜÁÜÛÐÌ×Ü function as a private construct

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðíÁðîÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to invoke the package�s procedure. The code and the result are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÍÛÔÛÝÌ ï ×ÒÌÑ ªÁ¼«³³§ ÚÎÑÓ ¼»°¿®¬³»²¬­ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ ã °Á¼»°¬·¼å ÎÛÌËÎÒ ÌÎËÛå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå

ÛÒÜ ª¿´·¼Á¼»°¬·¼å

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô

Page 177: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 3-1: Creating and Using Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 65

°Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í ÞÛÙ×Ò ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷ ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å

ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼ ×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

b) Invoke the ÛÓÐÁÐÕÙòßÜÜÁÛÓÐÔÑÇÛÛ procedure, using department ID 15 for employee Jane Harris with the email ID ÖßØßÎÎ×Í. Because department ID 15 does not exist, you should get an error message as specified in the exception handler of your procedure.

Page 178: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 3-1: Creating and Using Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 66

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðíÁðîÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to invoke the package�s procedure. The code and the result are displayed as follows:

Note: You must complete step 3-2-a before performing this step. If you didn�t complete step 3-2-a, run the ­±´ÁðíÁðîÁ¿ò­¯´ script first.

ÛÈÛÝËÌÛ »³°Á°µ¹ò¿¼¼Á»³°´±§»»øùÖ¿²»ùô ùØ¿®®·­ùôùÖßØßÎÎ×Íùô °Á¼»°¬·¼ ãâ ïë÷

c) Invoke the ßÜÜÁÛÓÐÔÑÇÛÛ package procedure by using department ID 80 for employee David Smith with the email ID ÜßÍÓ×ÌØ.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­/­±´ÁðíÁðîÁ½ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to invoke the package�s procedure. The code and the result are displayed as follows: ÛÈÛÝËÌÛ »³°Á°µ¹ò¿¼¼Á»³°´±§»»øùÜ¿ª·¼ùô ùͳ·¬¸ùôùÜßÍÓ×ÌØùô °Á¼»°¬·¼ ãâ èð÷

d) Query the ÛÓÐÔÑÇÛÛÍ table to verify that the new employee was added.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ /­±´ÁðíÁðîÁ¼ò­¯´ script. Click the Run Script (F5) icon or the Execute Statement (F9) icon (while making sure the cursor is on any of the ÍÛÔÛÝÌ statement code) on the SQL Worksheet toolbar to query the ÛÓÐÔÑÇÛÛÍ table. The code and the result (Execute Statement icon) are displayed as follows:

Page 179: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 3-1: Creating and Using Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 67

ÍÛÔÛÝÌ ö ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã ùͳ·¬¸ùå

The following output is displayed in the Results tab because we executed the code using the F9 icon.

Page 180: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 68

Practices and Solutions for Lesson 4

In this practice, you modify an existing package to contain overloaded subprograms and you use forward declarations. You also create a package initialization block within a package body to populate a PL/SQL table. Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

Page 181: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 69

Practice 4-1: Working with Packages

In this practice, you modify the code for the ÛÓÐÁÐÕÙ package that you created earlier, and then overload the ßÜÜÁÛÓÐÔÑÇÛÛ procedure. Next, you create two overloaded functions called ÙÛÌÁÛÓÐÔÑÇÛÛ in the ÛÓÐÁÐÕÙ package. You also add a public procedure to ÛÓÐÁÐÕÙ to populate a private PL/SQL table of valid department IDs and modify the ÊßÔ×ÜÁÜÛÐÌ×Ü function to use the private PL/SQL table contents to validate department ID values. You also change the ÊßÔ×ÜÁÜÛÐÌ×Ü validation processing function to use the private PL/SQL table of department IDs. Finally, you reorganize the subprograms in the package specification and the body so that they are in alphabetical sequence.

1) Modify the code for the ÛÓÐÁÐÕÙ package that you created in Practice 4 step 2, and overload the ßÜÜÁÛÓÐÔÑÇÛÛ procedure.

a) In the package specification, add a new procedure called ßÜÜÁÛÓÐÔÑÇÛÛ that accepts the following three parameters: i) First name

ii) Last name iii) Department ID

b) Click the Run Script (F5) to create and compile the package.

c) Implement the new ßÜÜÁÛÓÐÔÑÇÛÛ procedure in the package body as follows:

i) Format the email address in uppercase characters, using the first letter of the first name concatenated with the first seven letters of the last name.

ii) The procedure should call the existing ßÜÜÁÛÓÐÔÑÇÛÛ procedure to perform the actual ×ÒÍÛÎÌ operation using its parameters and formatted email to supply the values.

iii) Click Run Script to create the package. Compile the package.

d) Invoke the new ßÜÜÁÛÓÐÔÑÇÛÛ procedure using the name Í¿³«»´ Ö±°´·² to be added to department 30.

e) Confirm that the new employee was added to the ÛÓÐÔÑÇÛÛÍ table.

2) In the ÛÓÐÁÐÕÙ package, create two overloaded functions called ÙÛÌÁÛÓÐÔÑÇÛÛ:

a) In the package specification, add the following functions:

i) The ÙÛÌÁÛÓÐÔÑÇÛÛ function that accepts the parameter called °Á»³°Á·¼ based on the »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛ type. This function should return ÛÓÐÔÑÇÛÛÍûÎÑÉÌÇÐÛ.

ii) The ÙÛÌÁÛÓÐÔÑÇÛÛ function that accepts the parameter called °Áº¿³·´§Á²¿³» of type »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛ. This function should return ÛÓÐÔÑÇÛÛÍûÎÑÉÌÇÐÛ.

b) Click Run Script to re-create and compile the package.

Page 182: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 70

c) In the package body:

i) Implement the first ÙÛÌÁÛÓÐÔÑÇÛÛ function to query an employee using the employee�s ID.

ii) Implement the second ÙÛÌÁÛÓÐÔÑÇÛÛ function to use the equality operator on the value supplied in the °Áº¿³·´§Á²¿³» parameter.

d) Click Run Script to re-create and compile the package.

e) Add a utility procedure ÐÎ×ÒÌÁÛÓÐÔÑÇÛÛ to the ÛÓÐÁÐÕÙ package as follows:

i) The procedure accepts an ÛÓÐÔÑÇÛÛÍûÎÑÉÌÇÐÛ as a parameter.

ii) The procedure displays the following for an employee on one line, using the ÜÞÓÍÁÑËÌÐËÌ package:

- ¼»°¿®¬³»²¬Á·¼

- »³°´±§»»Á·¼

- º·®­¬Á²¿³»

- ´¿­¬Á²¿³»

- ¶±¾Á·¼

- ­¿´¿®§

f) Click Run Script (F5) to create and compile the package.

g) Use an anonymous block to invoke the ÛÓÐÁÐÕÙòÙÛÌÁÛÓÐÔÑÇÛÛ function with an employee ID of ïðð and family name of ùÖ±°´·²ù. Use the ÐÎ×ÒÌÁÛÓÐÔÑÇÛÛ procedure to display the results for each row returned.

3) Because the company does not frequently change its departmental data, you can improve performance of your ÛÓÐÁÐÕÙ by adding a public procedure, ×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ, to populate a private PL/SQL table of valid department IDs. Modify the ÊßÔ×ÜÁÜÛÐÌ×Ü function to use the private PL/SQL table contents to validate department ID values.

Note: The ­±´ÁðìÁðíò­¯´ solution file script contains the code for steps a, b, and c.

a) In the package specification, create a procedure called ×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ with no parameters by adding the following to the package specification section before the ÐÎ×ÒÌÁÛÓÐÔÑÇÛÛÍ specification: ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­å

b) In the package body, implement the ×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ procedure to store all department IDs in a private PL/SQL index-by table named ª¿´·¼Á¼»°¿®¬³»²¬­ containing ÞÑÑÔÛßÒ values.

i) Declare the ª¿´·¼Á¼»°¿®¬³»²¬­ variable and its type definition ¾±±´»¿²Á¬¿¾Á¬§°» before all procedures in the body. Enter the following at the beginning of the package body:

Page 183: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 71

ÌÇÐÛ ¾±±´»¿²Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ÞÑÑÔÛßÒ ×ÒÜÛÈ ÞÇ Þ×ÒßÎÇÁ×ÒÌÛÙÛÎå ª¿´·¼Á¼»°¿®¬³»²¬­ ¾±±´»¿²Á¬¿¾Á¬§°»å

ii) Use the ¼»°¿®¬³»²¬Á·¼ column value as the index to create the entry in the index-by table to indicate its presence, and assign the entry a value of ÌÎËÛ. Enter the ×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ procedure declaration at the end of the package body (right after the °®·²¬Á»³°´±§»»­ procedure) as follows:

ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­ ×Í ÞÛÙ×Ò ÚÑÎ ®»½ ×Ò øÍÛÔÛÝÌ ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ ¼»°¿®¬³»²¬­÷ ÔÑÑÐ ª¿´·¼Á¼»°¿®¬³»²¬­ø®»½ò¼»°¿®¬³»²¬Á·¼÷ æã ÌÎËÛå ÛÒÜ ÔÑÑÐå ÛÒÜå

c) In the body, create an initialization block that calls the ×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ procedure to initialize the table as follows: ÞÛÙ×Ò ·²·¬Á¼»°¿®¬³»²¬­å ÛÒÜå

d) Click Run Script (F5) to create and compile the package.

4) Change the ÊßÔ×ÜÁÜÛÐÌ×Ü validation processing function to use the private PL/SQL table of department IDs.

a) Modify the ÊßÔ×ÜÁÜÛÐÌ×Ü function to perform its validation by using the PL/SQL table of department ID values. Click Run Script (F5) to create the package. Compile the package.

b) Test your code by calling ßÜÜÁÛÓÐÔÑÇÛÛ using the name Ö¿³»­ Þ±²¼ in department ïë. What happens?

c) Insert a new department. Specify 15 for the department ID and ùÍ»½«®·¬§ù for the department name. Commit and verify the changes.

d) Test your code again, by calling ßÜÜÁÛÓÐÔÑÇÛÛ using the name Ö¿³»­ Þ±²¼ in department 15. What happens?

e) Execute the ÛÓÐÁÐÕÙò×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ procedure to update the internal PL/SQL table with the latest departmental data.

f) Test your code by calling ßÜÜÁÛÓÐÔÑÇÛÛ using the employee name Ö¿³»­ Þ±²¼, who works in department ïë. What happens?

g) Delete employee Ö¿³»­ Þ±²¼ and department ïë from their respective tables, commit the changes, and refresh the department data by invoking the ÛÓÐÁÐÕÙò×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ procedure. Make sure you enter ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ first.

Page 184: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 72

5) Reorganize the subprograms in the package specification and the body so that they are in alphabetical sequence.

a) Edit the package specification and reorganize subprograms alphabetically. Click Run Script to re-create the package specification. Compile the package specification. What happens?

b) Edit the package body and reorganize all subprograms alphabetically. Click Run Script to re-create the package specification. Re-compile the package specification. What happens?

c) Correct the compilation error using a forward declaration in the body for the appropriate subprogram reference. Click Run Script to re-create the package, and then recompile the package. What happens?

Page 185: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 73

Practice Solutions 4-1: Working with Packages

In this practice, you modify the code for the ÛÓÐÁÐÕÙ package that you created earlier, and then overload the ßÜÜÁÛÓÐÔÑÇÛÛ procedure. Next, you create two overloaded functions called ÙÛÌÁÛÓÐÔÑÇÛÛ in the ÛÓÐÁÐÕÙ package. You also add a public procedure to ÛÓÐÁÐÕÙ to populate a private PL/SQL table of valid department IDs and modify the ÊßÔ×ÜÁÜÛÐÌ×Ü function to use the private PL/SQL table contents to validate department ID values. You also change the ÊßÔ×ÜÁÜÛÐÌ×Ü validation processing function to use the private PL/SQL table of department IDs. Finally, you reorganize the subprograms in the package specification and the body so that they are in alphabetical sequence.

1) Modify the code for the ÛÓÐÁÐÕÙ package that you created in Practice 4 step 2, and overload the ßÜÜÁÛÓÐÔÑÇÛÛ procedure.

a) In the package specification, add a new procedure called ßÜÜÁÛÓÐÔÑÇÛÛ that accepts the following three parameters: i) First name

ii) Last name iii) Department ID

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðïÁ¿ò­¯´ file. The code is displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å

ñö Ò»© ±ª»®´±¿¼»¼ ¿¼¼Á»³°´±§»» öñ

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å ÛÒÜ »³°Á°µ¹å ñ

Page 186: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 74

ÍØÑÉ ÛÎÎÑÎÍ

b) Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the package.

c) Implement the new ßÜÜÁÛÓÐÔÑÇÛÛ procedure in the package body as follows:

i) Format the email address in uppercase characters, using the first letter of the first name concatenated with the first seven letters of the last name.

ii) The procedure should call the existing ßÜÜÁÛÓÐÔÑÇÛÛ procedure to perform the actual ×ÒÍÛÎÌ operation using its parameters and formatted email to supply the values.

iii) Click Run Script to create the package. Compile the package.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðïÁ½ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to invoke the package�s procedure. The code and the result are displayed as follows (the newly added code is highlighted in bold face text in the code box below):

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÍÛÔÛÝÌ ï ×ÒÌÑ ªÁ¼«³³§ ÚÎÑÓ ¼»°¿®¬³»²¬­ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ ã °Á¼»°¬·¼å ÎÛÌËÎÒ ÌÎËÛå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô

°Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô

Page 187: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 75

°Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í

ÞÛÙ×Ò ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷ ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å ñö Ò»© ±ª»®´±¿¼»¼ ¿¼¼Á»³°´±§»» °®±½»¼«®» öñ

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ×Í °Á»³¿·´ »³°´±§»»­ò»³¿·´û¬§°»å ÞÛÙ×Ò °Á»³¿·´ æã ËÐÐÛÎøÍËÞÍÌÎø°Áº·®­¬Á²¿³»ô ïô ï÷¤¤ÍËÞÍÌÎø°Á´¿­¬Á²¿³»ô ïô é÷÷å ¿¼¼Á»³°´±§»»ø°Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¼»°¬·¼ ãâ °Á¼»°¬·¼÷å ÛÒÜå

ñö Û²¼ ¼»½´¿®¿¬·±² ±º ¬¸» ±ª»®´±¿¼»¼ ¿¼¼Á»³°´±§»» °®±½»¼«®» öñ

ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼ ×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 188: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 76

d) Invoke the new ßÜÜÁÛÓÐÔÑÇÛÛ procedure using the name Í¿³«»´ Ö±°´·² to be added to department 30.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­/­±´ÁðìÁðïÁ¼ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to invoke the package�s procedure. The code and the result are displayed as follows:

ÛÈÛÝËÌÛ »³°Á°µ¹ò¿¼¼Á»³°´±§»»øùÍ¿³«»´ùô ùÖ±°´·²ùô íð÷

e) Confirm that the new employee was added to the ÛÓÐÔÑÇÛÛÍ table.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðïÁ»ò­¯´ script. Click anywhere on the ÍÛÔÛÝÌ statement, and then click the Execute Statement (F9) icon on the SQL Worksheet toolbar to execute the query. The code and the result are displayed as follows: ÍÛÔÛÝÌ ö ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã ùÖ±°´·²ùå

2) In the ÛÓÐÁÐÕÙ package, create two overloaded functions called ÙÛÌÁÛÓÐÔÑÇÛÛ:

a) In the package specification, add the following functions:

i) The ÙÛÌÁÛÓÐÔÑÇÛÛ function that accepts the parameter called °Á»³°Á·¼ based on the »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛ type. This function should return ÛÓÐÔÑÇÛÛÍûÎÑÉÌÇÐÛ.

Page 189: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 77

ii) The ÙÛÌÁÛÓÐÔÑÇÛÛ function that accepts the parameter called °Áº¿³·´§Á²¿³» of type »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛ. This function should return ÛÓÐÔÑÇÛÛÍûÎÑÉÌÇÐÛ.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­/­±´ÁðìÁðîÁ¿ò­¯´ script.

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å

ñö Ò»© ±ª»®´±¿¼»¼ ¹»¬Á»³°´±§»»­ º«²½¬·±²­ ­°»½­ ­¬¿®¬­ ¸»®»æ öñ

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å

ñö Ò»© ±ª»®´±¿¼»¼ ¹»¬Á»³°´±§»»­ º«²½¬·±²­ ­°»½­ »²¼­ ¸»®»ò öñ

ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

b) Click Run Script to re-create and compile the package specification.

Click the Run Script (F5) icon on the SQL Worksheet toolbar to re-create and compile the package�s specification. The result is shown below:

Page 190: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 78

Note: As mentioned earlier, if your code contains an error message, you can recompile the code using the following procedure to view the details of the error or warning in the Compiler � Log tab: To compile the package specification, right-click the package�s specification (or the entire package) name in the Object Navigator tree, and then select Compile from the shortcut menu. The warning is expected and is for informational purposes only.

c) In the package body:

i) Implement the first ÙÛÌÁÛÓÐÔÑÇÛÛ function to query an employee using the employee�s ID.

ii) Implement the second ÙÛÌÁÛÓÐÔÑÇÛÛ function to use the equality operator on the value supplied in the °Áº¿³·´§Á²¿³» parameter.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­/­±´ÁðìÁðîÁ½ò­¯´ script. The newly added functions are highlighted in the following code box.

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷å

ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø

Page 191: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 79

°Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å

ñö Ò»© ±ª»®´±¿¼»¼ ¹»¬Á»³°´±§»»­ º«²½¬·±²­ ­°»½­ ­¬¿®¬­ ¸»®»æ öñ

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å

ñö Ò»© ±ª»®´±¿¼»¼ ¹»¬Á»³°´±§»»­ º«²½¬·±²­ ­°»½­ »²¼­ ¸»®»ò öñ

ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

óó °¿½µ¿¹» ¾±¼§

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÍÛÔÛÝÌ ï ×ÒÌÑ ªÁ¼«³³§ ÚÎÑÓ ¼»°¿®¬³»²¬­ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ ã °Á¼»°¬·¼å ÎÛÌËÎÒ ÌÎËÛå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í ÞÛÙ×Ò ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô

»³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷

Page 192: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 80

ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ×Í °Á»³¿·´ »³°´±§»»­ò»³¿·´û¬§°»å ÞÛÙ×Ò °Á»³¿·´ æã ËÐÐÛÎøÍËÞÍÌÎø°Áº·®­¬Á²¿³»ô ïô ï÷¤¤ÍËÞÍÌÎø°Á´¿­¬Á²¿³»ô ïô é÷÷å ¿¼¼Á»³°´±§»»ø°Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¼»°¬·¼ ãâ °Á¼»°¬·¼÷å ÛÒÜå

ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼ ×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å

ñö Ò»© ¹»¬Á»³°´±§»» º«²½¬·±² ¼»½´¿®¿¬·±² ­¬¿®¬­ ¸»®» öñ

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã °Áº¿³·´§Á²¿³»å ÎÛÌËÎÒ ®»½Á»³°å

Page 193: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 81

ÛÒÜå

ñö Ò»© ±ª»®´±¿¼»¼ ¹»¬Á»³°´±§»» º«²½¬·±² ¼»½´¿®¿¬·±² »²¼­ ¸»®» öñ

ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

d) Click Run Script to re-create the package. Compile the package.

Click the Run Script (F5) icon on the SQL Worksheet toolbar to re-create and compile the package. The result is shown below:

e) Add a utility procedure ÐÎ×ÒÌÁÛÓÐÔÑÇÛÛ to the ÛÓÐÁÐÕÙ package as follows:

i) The procedure accepts an ÛÓÐÔÑÇÛÛÍûÎÑÉÌÇÐÛ as a parameter.

ii) The procedure displays the following for an employee on one line, using the ÜÞÓÍÁÑËÌÐËÌ package:

- ¼»°¿®¬³»²¬Á·¼

- »³°´±§»»Á·¼

- º·®­¬Á²¿³»

- ´¿­¬Á²¿³»

- ¶±¾Á·¼

- ­¿´¿®§

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ /­±´ÁðìÁðîÁ»ò­¯´ script. The newly added code is highlighted in the following code box.

óó п½µ¿¹» ÍÐÛÝ×Ú×ÝßÌ×ÑÒ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô

Page 194: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 82

°Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ñö Ò»© °®·²¬Á»³°´±§»» °®·²¬Á»³°´±§»» °®±½»¼«®» ­°»½ öñ ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ óó п½µ¿¹» ÞÑÜÇ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÍÛÔÛÝÌ ï ×ÒÌÑ ªÁ¼«³³§ ÚÎÑÓ ¼»°¿®¬³»²¬­ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ ã °Á¼»°¬·¼å ÎÛÌËÎÒ ÌÎËÛå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô

Page 195: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 83

°Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í ÞÛÙ×Ò ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷ ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ×Í °Á»³¿·´ »³°´±§»»­ò»³¿·´û¬§°»å ÞÛÙ×Ò °Á»³¿·´ æã ËÐÐÛÎøÍËÞÍÌÎø°Áº·®­¬Á²¿³»ô ïô ï÷¤¤ÍËÞÍÌÎø°Á´¿­¬Á²¿³»ô ïô é÷÷å ¿¼¼Á»³°´±§»»ø°Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¼»°¬·¼ ãâ °Á¼»°¬·¼÷å ÛÒÜå ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼ ×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷

Page 196: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 84

®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã °Áº¿³·´§Á²¿³»å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ñö Ò»© °®·²¬Á»³°´±§»»­ °®±½»¼«®» ¼»½´¿®¿¬·±²ò öñ ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø°Á®»½Á»³°ò¼»°¿®¬³»²¬Á·¼ ¤¤ù ù¤¤ °Á®»½Á»³°ò»³°´±§»»Á·¼¤¤ù ù¤¤ °Á®»½Á»³°òº·®­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò´¿­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò¶±¾Á·¼¤¤ù ù¤¤ °Á®»½Á»³°ò­¿´¿®§÷å ÛÒÜå ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

f) Click Run Script (F5) to create and compile the package.

Click the Run Script (F5) icon on the SQL Worksheet toolbar to re-create and compile the package.

g) Use an anonymous block to invoke the ÛÓÐÁÐÕÙòÙÛÌÁÛÓÐÔÑÇÛÛ function with an employee ID of ïðð and family name of ùÖ±°´·²ù. Use the ÐÎ×ÒÌÁÛÓÐÔÑÇÛÛ procedure to display the results for each row returned. Make sure you enter ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ first.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ /­±´ÁðìÁðîÁ¹ò­¯´ script. ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ ÞÛÙ×Ò »³°Á°µ¹ò°®·²¬Á»³°´±§»»ø»³°Á°µ¹ò¹»¬Á»³°´±§»»øïðð÷÷å

Page 197: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 85

»³°Á°µ¹ò°®·²¬Á»³°´±§»»ø»³°Á°µ¹ò¹»¬Á»³°´±§»»øùÖ±°´·²ù÷÷å ÛÒÜå ñ

3) Because the company does not frequently change its departmental data, you can improve performance of your ÛÓÐÁÐÕÙ by adding a public procedure, ×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ, to populate a private PL/SQL table of valid department IDs. Modify the ÊßÔ×ÜÁÜÛÐÌ×Ü function to use the private PL/SQL table contents to validate department ID values.

Note: The ­±´ÁðìÁðíò­¯´ solution file script contains the code for steps a, b, and c.

a) In the package specification, create a procedure called ×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ with no parameters by adding the following to the package specification section before the ÐÎ×ÒÌÁÛÓÐÔÑÇÛÛÍ specification: ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­å

b) In the package body, implement the ×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ procedure to store all department IDs in a private PL/SQL index-by table named ª¿´·¼Á¼»°¿®¬³»²¬­ containing ÞÑÑÔÛßÒ values.

i) Declare the ª¿´·¼Á¼»°¿®¬³»²¬­ variable and its type definition ¾±±´»¿²Á¬¿¾Á¬§°» before all procedures in the body. Enter the following at the beginning of the package body:

ÌÇÐÛ ¾±±´»¿²Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ÞÑÑÔÛßÒ ×ÒÜÛÈ ÞÇ Þ×ÒßÎÇÁ×ÒÌÛÙÛÎå ª¿´·¼Á¼»°¿®¬³»²¬­ ¾±±´»¿²Á¬¿¾Á¬§°»å

ii) Use the ¼»°¿®¬³»²¬Á·¼ column value as the index to create the entry in the index-by table to indicate its presence, and assign the entry a value of ÌÎËÛ. Enter the ×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ procedure declaration at the end of the package body (right after the °®·²¬Á»³°´±§»»­ procedure) as follows:

ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­ ×Í ÞÛÙ×Ò ÚÑÎ ®»½ ×Ò øÍÛÔÛÝÌ ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ ¼»°¿®¬³»²¬­÷ ÔÑÑÐ ª¿´·¼Á¼»°¿®¬³»²¬­ø®»½ò¼»°¿®¬³»²¬Á·¼÷ æã ÌÎËÛå ÛÒÜ ÔÑÑÐå ÛÒÜå

Page 198: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 86

c) In the body, create an initialization block that calls the ×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ procedure to initialize the table as follows: ÞÛÙ×Ò ·²·¬Á¼»°¿®¬³»²¬­å ÛÒÜå

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðíò­¯´ script. The newly added code is highlighted in the following code box.

óó п½µ¿¹» ÍÐÛÝ×Ú×ÝßÌ×ÑÒ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ñö Ò»© °®±½»¼«®» ·²·¬Á¼»°¿®¬³»²¬­ ­°»½ öñ ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­å ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ óó п½µ¿¹» ÞÑÜÇ

Page 199: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 87

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ñö Ò»© ¬§°» öñ ÌÇÐÛ ¾±±´»¿²Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ÞÑÑÔÛßÒ ×ÒÜÛÈ ÞÇ Þ×ÒßÎÇÁ×ÒÌÛÙÛÎå ª¿´·¼Á¼»°¿®¬³»²¬­ ¾±±´»¿²Á¬¿¾Á¬§°»å ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÍÛÔÛÝÌ ï ×ÒÌÑ ªÁ¼«³³§ ÚÎÑÓ ¼»°¿®¬³»²¬­ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ ã °Á¼»°¬·¼å ÎÛÌËÎÒ ÌÎËÛå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í ÞÛÙ×Ò ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ

×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷

ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ×Í

Page 200: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 88

°Á»³¿·´ »³°´±§»»­ò»³¿·´û¬§°»å ÞÛÙ×Ò °Á»³¿·´ æã ËÐÐÛÎøÍËÞÍÌÎø°Áº·®­¬Á²¿³»ô ïô ï÷¤¤ÍËÞÍÌÎø°Á´¿­¬Á²¿³»ô ïô é÷÷å ¿¼¼Á»³°´±§»»ø°Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¼»°¬·¼ ãâ °Á¼»°¬·¼÷å ÛÒÜå ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼ ×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã °Áº¿³·´§Á²¿³»å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø°Á®»½Á»³°ò¼»°¿®¬³»²¬Á·¼ ¤¤ù ù¤¤ ÐÁ®»½Á»³°ò»³°´±§»»Á·¼¤¤ù ù¤¤ ÐÁ®»½Á»³°òº·®­¬Á²¿³»¤¤ù ù¤¤ ÐÁ®»½Á»³°ò´¿­¬Á²¿³»¤¤ù ù¤¤ ÐÁ®»½Á»³°ò¶±¾Á·¼¤¤ù ù¤¤ ÐÁ®»½Á»³°ò­¿´¿®§÷å ÛÒÜå ñö Ò»© ·²·¬Á¼»°¿®¬³»²¬­ °®±½»¼«®» ¼»½´¿®¿¬·±²ò öñ

Page 201: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 89

ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­ ×Í ÞÛÙ×Ò ÚÑÎ ®»½ ×Ò øÍÛÔÛÝÌ ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ ¼»°¿®¬³»²¬­÷ ÔÑÑÐ ª¿´·¼Á¼»°¿®¬³»²¬­ø®»½ò¼»°¿®¬³»²¬Á·¼÷ æã ÌÎËÛå ÛÒÜ ÔÑÑÐå ÛÒÜå ñö ½¿´´ ¬¸» ²»© ·²·¬Á¼»°¿®¬³»²¬­ °®±½»¼«®»ò öñ ÞÛÙ×Ò ·²·¬Á¼»°¿®¬³»²¬­å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

d) Click Run Script (F5) to re-create and compile the package.

Click the Run Script (F5) icon on the SQL Worksheet toolbar to re-create and compile the package.

4) Change the ÊßÔ×ÜÁÜÛÐÌ×Ü validation processing function to use the private PL/SQL table of department IDs.

a) Modify the ÊßÔ×ÜÁÜÛÐÌ×Ü function to perform its validation by using the PL/SQL table of department ID values. Click Run Script (F5) to create and compile the package.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðìÁ¿ò­¯´ script. Click Run Script (F5) to create and compile the package. The newly added code is highlighted in the following code box.

óó п½µ¿¹» ÍÐÛÝ×Ú×ÝßÌ×ÑÒ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô

Page 202: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 90

°Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ñö Ò»© °®±½»¼«®» ·²·¬Á¼»°¿®¬³»²¬­ ­°»½ öñ ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­å ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ óó п½µ¿¹» ÞÑÜÇ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ÌÇÐÛ ¾±±´»¿²Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ÞÑÑÔÛßÒ ×ÒÜÛÈ ÞÇ Þ×ÒßÎÇÁ×ÒÌÛÙÛÎå ª¿´·¼Á¼»°¿®¬³»²¬­ ¾±±´»¿²Á¬¿¾Á¬§°»å ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÎÛÌËÎÒ ª¿´·¼Á¼»°¿®¬³»²¬­ò»¨·­¬­ø°Á¼»°¬·¼÷å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å

Page 203: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 91

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í ÞÛÙ×Ò ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷ ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô°Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ×Í °Á»³¿·´ »³°´±§»»­ò»³¿·´û¬§°»å ÞÛÙ×Ò °Á»³¿·´ æã ËÐÐÛÎøÍËÞÍÌÎø°Áº·®­¬Á²¿³»ô ïô ï÷¤¤ÍËÞÍÌÎø°Á´¿­¬Á²¿³»ô ïô é÷÷å ¿¼¼Á»³°´±§»»ø°Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¼»°¬·¼ ãâ °Á¼»°¬·¼÷å ÛÒÜå ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼ ×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­

Page 204: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 92

ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã °Áº¿³·´§Á²¿³»å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø°Á®»½Á»³°ò¼»°¿®¬³»²¬Á·¼ ¤¤ù ù¤¤ °Á®»½Á»³°ò»³°´±§»»Á·¼¤¤ù ù¤¤ °Á®»½Á»³°òº·®­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò´¿­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò¶±¾Á·¼¤¤ù ù¤¤ °Á®»½Á»³°ò­¿´¿®§÷å ÛÒÜå ñö Ò»© ·²·¬Á¼»°¿®¬³»²¬­ °®±½»¼«®» ¼»½´¿®¿¬·±²ò öñ ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­ ×Í ÞÛÙ×Ò ÚÑÎ ®»½ ×Ò øÍÛÔÛÝÌ ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ ¼»°¿®¬³»²¬­÷ ÔÑÑÐ ª¿´·¼Á¼»°¿®¬³»²¬­ø®»½ò¼»°¿®¬³»²¬Á·¼÷ æã ÌÎËÛå ÛÒÜ ÔÑÑÐå ÛÒÜå ñö ½¿´´ ¬¸» ²»© ·²·¬Á¼»°¿®¬³»²¬­ °®±½»¼«®»ò öñ ÞÛÙ×Ò ·²·¬Á¼»°¿®¬³»²¬­å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 205: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 93

b) Test your code by calling ßÜÜÁÛÓÐÔÑÇÛÛ using the name Ö¿³»­ Þ±²¼ in department ïë. What happens?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðìÁ¾ò­¯´ script. ÛÈÛÝËÌÛ »³°Á°µ¹ò¿¼¼Á»³°´±§»»øùÖ¿³»­ùô ùÞ±²¼ùô ïë÷

Click the Run Script (F5) icon on the SQL Worksheet toolbar to test inserting a new employee. The insert operation to add the employee fails with an exception because department 15 does not exist.

c) Insert a new department. Specify 15 for the department ID and ùÍ»½«®·¬§ù for the department name. Commit and verify the changes.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðìÁ½ò­¯´ script. The code and result are displayed as follows: ×ÒÍÛÎÌ ×ÒÌÑ ¼»°¿®¬³»²¬­ ø¼»°¿®¬³»²¬Á·¼ô ¼»°¿®¬³»²¬Á²¿³»÷ ÊßÔËÛÍ øïëô ùÍ»½«®·¬§ù÷å ÝÑÓÓ×Ìå

d) Test your code again, by calling ßÜÜÁÛÓÐÔÑÇÛÛ using the name Ö¿³»­ Þ±²¼ in department 15. What happens?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðìÁ¼ò­¯´ script. The code and the result are displayed as follows: ÛÈÛÝËÌÛ »³°Á°µ¹ò¿¼¼Á»³°´±§»»øùÖ¿³»­ùô ùÞ±²¼ùô ïë÷

Page 206: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 94

The insert operation to add the employee fails with an exception. Department 15 does not exist as an entry in the PL/SQL associative array (index-by-table) package state variable.

e) Execute the ÛÓÐÁÐÕÙò×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ procedure to update the internal PL/SQL table with the latest departmental data.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðìÁ»ò­¯´ script. The code and result are displayed as follows: ÛÈÛÝËÌÛ ÛÓÐÁÐÕÙò×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ

f) Test your code by calling ßÜÜÁÛÓÐÔÑÇÛÛ using the employee name Ö¿³»­

Þ±²¼, who works in department ïë. What happens?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðìÁºò­¯´ script. The code and the result are displayed as follows. ÛÈÛÝËÌÛ »³°Á°µ¹ò¿¼¼Á»³°´±§»»øùÖ¿³»­ùô ùÞ±²¼ùô ïë÷

The row is finally inserted because the department 15 record exists in the database and the package�s PL/SQL index-by table, due to invoking ÛÓÐÁÐÕÙò×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ, which refreshes the package state data.

Page 207: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 95

g) Delete employee Ö¿³»­ Þ±²¼ and department ïë from their respective tables, commit the changes, and refresh the department data by invoking the ÛÓÐÁÐÕÙò×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ procedure.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðìÁ¹ò­¯´ script. The code and the result are displayed as follows.

ÜÛÔÛÌÛ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ º·®­¬Á²¿³» ã ùÖ¿³»­ù ßÒÜ ´¿­¬Á²¿³» ã ùÞ±²¼ùå ÜÛÔÛÌÛ ÚÎÑÓ ¼»°¿®¬³»²¬­ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ ã ïëå ÝÑÓÓ×Ìå ÛÈÛÝËÌÛ ÛÓÐÁÐÕÙò×Ò×ÌÁÜÛÐßÎÌÓÛÒÌÍ

5) Reorganize the subprograms in the package specification and the body so that they are in alphabetical sequence. a) Edit the package specification and reorganize subprograms alphabetically. Click

Run Script to re-create the package specification. Compile the package specification. What happens?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðëÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to re-create and compile the package. The code and the result are displayed as follows. The package�s specification subprograms are already in an alphabetical order. ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í

ñö ¬¸» °¿½µ¿¹» ­°»½ ·­ ¿´®»¿¼§ ·² ¿² ¿´°¸¿¾»¬·½¿´ ±®¼»®ò öñ

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å

Page 208: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 96

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷å

ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å

ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­å

ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷å

ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

b) Edit the package body and reorganize all subprograms alphabetically. Click Run Script to re-create the package specification. Re-compile the package specification. What happens?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðëÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to re-create the package. The code and the result are displayed as follows. óó п½µ¿¹» ÞÑÜÇ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ÌÇÐÛ ¾±±´»¿²Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ÞÑÑÔÛßÒ ×ÒÜÛÈ ÞÇ Þ×ÒßÎÇÁ×ÒÌÛÙÛÎå ª¿´·¼Á¼»°¿®¬³»²¬­ ¾±±´»¿²Á¬¿¾Á¬§°»å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô

°Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô

Page 209: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 97

°Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í ÞÛÙ×Ò ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷ ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ×Í °Á»³¿·´ »³°´±§»»­ò»³¿·´û¬§°»å ÞÛÙ×Ò °Á»³¿·´ æã ËÐÐÛÎøÍËÞÍÌÎø°Áº·®­¬Á²¿³»ô ïô ï÷¤¤ÍËÞÍÌÎø°Á´¿­¬Á²¿³»ô ïô é÷÷å ¿¼¼Á»³°´±§»»ø°Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¼»°¬·¼ ãâ °Á¼»°¬·¼÷å ÛÒÜå

ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼ ×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³°

ÚÎÑÓ »³°´±§»»­

Page 210: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 98

ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã °Áº¿³·´§Á²¿³»å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå

ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­ ×Í ÞÛÙ×Ò ÚÑÎ ®»½ ×Ò øÍÛÔÛÝÌ ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ ¼»°¿®¬³»²¬­÷ ÔÑÑÐ ª¿´·¼Á¼»°¿®¬³»²¬­ø®»½ò¼»°¿®¬³»²¬Á·¼÷ æã ÌÎËÛå ÛÒÜ ÔÑÑÐå ÛÒÜå

ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø°Á®»½Á»³°ò¼»°¿®¬³»²¬Á·¼ ¤¤ù ù¤¤ °Á®»½Á»³°ò»³°´±§»»Á·¼¤¤ù ù¤¤ °Á®»½Á»³°òº·®­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò´¿­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò¶±¾Á·¼¤¤ù ù¤¤ °Á®»½Á»³°ò­¿´¿®§÷å ÛÒÜå

ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÎÛÌËÎÒ ª¿´·¼Á¼»°¿®¬³»²¬­ò»¨·­¬­ø°Á¼»°¬·¼÷å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å

ÞÛÙ×Ò ·²·¬Á¼»°¿®¬³»²¬­å ÛÒÜ »³°Á°µ¹å

ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 211: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 99

The package does not compile successfully because the ÊßÔ×ÜÁÜÛÐÌ×Ü function is referenced before it is declared.

c) Correct the compilation error using a forward declaration in the body for the appropriate subprogram reference. Click Run Script to re-create the package, and then recompile the package. What happens?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðìÁðëÁ½ò­¯´ script. The function�s forward declaration is highlighted in the code box below. Click the Run Script (F5) icon on the SQL Worksheet toolbar to re-create and compile the package. The code and the result are displayed as follows.

óó п½µ¿¹» ÞÑÜÇ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ÌÇÐÛ ¾±±´»¿²Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ÞÑÑÔÛßÒ ×ÒÜÛÈ ÞÇ Þ×ÒßÎÇÁ×ÒÌÛÙÛÎå ª¿´·¼Á¼»°¿®¬³»²¬­ ¾±±´»¿²Á¬¿¾Á¬§°»å ñö º±®©¿®¼ ¼»½´¿®¿¬·±² ±º ª¿´·¼Á¼»°¬·¼ öñ ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒå ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í ÞÛÙ×Ò ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ñö ª¿´·¼Á¼»°¬·¼ º«²½¬·±² ®»º»®²»½»¼ öñ ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷

Page 212: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 100

ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ×Í °Á»³¿·´ »³°´±§»»­ò»³¿·´û¬§°»å ÞÛÙ×Ò °Á»³¿·´ æã ËÐÐÛÎøÍËÞÍÌÎø°Áº·®­¬Á²¿³»ô ïô ï÷¤¤ÍËÞÍÌÎø°Á´¿­¬Á²¿³»ô ïô é÷÷å ¿¼¼Á»³°´±§»»ø°Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¼»°¬·¼ ãâ °Á¼»°¬·¼÷å ÛÒÜå ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼ ×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã °Áº¿³·´§Á²¿³»å ÎÛÌËÎÒ ®»½Á»³°å

Page 213: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 4-1: Working with Packages (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 101

ÛÒÜå ñö Ò»© ¿´°¸¿¾»¬·½¿´ ´±½¿¬·±² ±º º«²½¬·±² ·²·¬Á¼»°¿®¬³»²¬­ò öñ ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­ ×Í ÞÛÙ×Ò ÚÑÎ ®»½ ×Ò øÍÛÔÛÝÌ ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ ¼»°¿®¬³»²¬­÷ ÔÑÑÐ ª¿´·¼Á¼»°¿®¬³»²¬­ø®»½ò¼»°¿®¬³»²¬Á·¼÷ æã ÌÎËÛå ÛÒÜ ÔÑÑÐå ÛÒÜå ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø°Á®»½Á»³°ò¼»°¿®¬³»²¬Á·¼ ¤¤ù ù¤¤ °Á®»½Á»³°ò»³°´±§»»Á·¼¤¤ù ù¤¤ °Á®»½Á»³°òº·®­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò´¿­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò¶±¾Á·¼¤¤ù ù¤¤ °Á®»½Á»³°ò­¿´¿®§÷å ÛÒÜå ñö Ò»© ¿´°¸¿¾»¬·½¿´ ´±½¿¬·±² ±º º«²½¬·±² ª¿´·¼Á¼»°¬·¼ò öñ ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÎÛÌËÎÒ ª¿´·¼Á¼»°¿®¬³»²¬­ò»¨·­¬­ø°Á¼»°¬·¼÷å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å ÞÛÙ×Ò ·²·¬Á¼»°¿®¬³»²¬­å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

A forward declaration for the ÊßÔ×ÜÁÜÛÐÌ×Ü function enables the package body to compile successfully as shown below:

Page 214: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 102

Practices and Solutions for Lesson 5

In this practice, you use the ËÌÔÁÚ×ÔÛ package to generate a text file report of employees in each department.

Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

Page 215: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 103

Practice 5-1: Using the ËÌÔÁÚ×ÔÛ Package

In this practice, you use the ËÌÔÁÚ×ÔÛ package to generate a text file report of employees in each department. You first create and execute a procedure called ÛÓÐÔÑÇÛÛÁÎÛÐÑÎÌ that generates an employee report in a file in the operating system, using the ËÌÔÁÚ×ÔÛ package. The report should generate a list of employees who have exceeded the average salary of their departments. Finally, you view the generated output text file.

1) Create a procedure called ÛÓÐÔÑÇÛÛÁÎÛÐÑÎÌ that generates an employee report in a file in the operating system, using the ËÌÔÁÚ×ÔÛ package. The report should generate a list of employees who have exceeded the average salary of their departments. a) Your program should accept two parameters. The first parameter is the output

directory. The second parameter is the name of the text file that is written.

Note: Use the directory location value ËÌÔÁÚ×ÔÛ. Add an exception-handling section to handle errors that may be encountered when using the ËÌÔÁÚ×ÔÛ package.

b) Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the procedure.

2) Invoke the procedure using the following two arguments:

a) Use ÎÛÐÑÎÌÍÁÜ×Î as the alias for the directory object as the first parameter.

b) Use ­¿´Á®°¬êïò¬¨¬ as the second parameter.

3) View the generated output text file as follows: a) Double-click the Terminal icon on your desktop. The Terminal window is

displayed.

b) At the $ prompt, change to the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ®»°±®¬­folder that contains the generated output file, ­¿´Á®°¬êïò¬¨¬ using the ½¼ command.

Note: You can use the °©¼ command to list the current working directory.

c) List the contents of the current directory using the ´­ command.

d) Open the transferred the ­¿´Á®°¬êïò¬¨¬, file using ¹»¼·¬ or an editor of your choice.

Page 216: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 104

Practice Solutions 5-1: Using the ËÌÔÁÚ×ÔÛ Package

In this practice, you use the ËÌÔÁÚ×ÔÛ package to generate a text file report of employees in each department. You first create and execute a procedure called ÛÓÐÔÑÇÛÛÁÎÛÐÑÎÌ that generates an employee report in a file in the operating system, using the ËÌÔÁÚ×ÔÛ package. The report should generate a list of employees who have exceeded the average salary of their departments. Finally, you view the generated output text file.

1) Create a procedure called ÛÓÐÔÑÇÛÛÁÎÛÐÑÎÌ that generates an employee report in a file in the operating system, using the ËÌÔÁÚ×ÔÛ package. The report should generate a list of employees who have exceeded the average salary of their departments.

a) Your program should accept two parameters. The first parameter is the output directory. The second parameter is the name of the text file that is written.

Note: Use the directory location value ËÌÔÁÚ×ÔÛ. Add an exception-handling section to handle errors that may be encountered when using the ËÌÔÁÚ×ÔÛ package. Open the file in the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ ­±´ÁðëÁðïò­¯´ script.

óó Ê»®·º§ ©·¬¸ §±«® ·²­¬®«½¬±® ¬¸¿¬ ¬¸» ¼¿¬¿¾¿­» ·²·¬Í×Üò±®¿ óó º·´» ¸¿­ ¬¸» ¼·®»½¬±®§ °¿¬¸ §±« ¿®» ¹±·²¹ ¬± «­» ©·¬¸ ¬¸·­ óó °®±½»¼«®»ò óó Ú±® »¨¿³°´»ô ¬¸»®» ­¸±«´¼ ¾» ¿² »²¬®§ ­«½¸ ¿­æ óó ËÌÔÁÚ×ÔÛÁÜ×Î ã ñ¸±³»ïñ¬»¿½¸ÈñËÌÔÁÚ×ÔÛ ·² §±«® ·²·¬Í×Üò±®¿ óó ø±® ¬¸» ÍÐÚ×ÔÛ÷ óó ØÑÉÛÊÛÎæ ̸» ½±«®­» ¸¿­ ¿ ¼·®»½¬±®§ ¿´·¿­ °®±ª·¼»¼ ½¿´´»¼ óó þÎÛÐÑÎÌÍÁÜ×Îþ ¬¸¿¬ ·­ ¿­­±½·¿¬»¼ ©·¬¸ ¿² ¿°°®±°®·¿¬» óó ¼·®»½¬±®§ò Ë­» ¬¸» ¼·®»½¬±®§ ¿´·¿­ ²¿³» ·² ¯«±¬»­ º±® ¬¸» óó º·®­¬ °¿®¿³»¬»® ¬± ½®»¿¬» ¿ º·´» ·² ¬¸» ¿°°®±°®·¿¬» óó ¼·®»½¬±®§ò ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ »³°´±§»»Á®»°±®¬ø °Á¼·® ×Ò ÊßÎÝØßÎîô °Áº·´»²¿³» ×Ò ÊßÎÝØßÎî÷ ×Í º ËÌÔÁÚ×ÔÛòÚ×ÔÛÁÌÇÐÛå ÝËÎÍÑÎ ½«®Á¿ª¹ ×Í ÍÛÔÛÝÌ ´¿­¬Á²¿³»ô ¼»°¿®¬³»²¬Á·¼ô ­¿´¿®§ ÚÎÑÓ »³°´±§»»­ ±«¬»® ÉØÛÎÛ ­¿´¿®§ â øÍÛÔÛÝÌ ßÊÙø­¿´¿®§÷ ÚÎÑÓ »³°´±§»»­ ·²²»® ÙÎÑËÐ ÞÇ ±«¬»®ò¼»°¿®¬³»²¬Á·¼÷ ÑÎÜÛÎ ÞÇ ¼»°¿®¬³»²¬Á·¼å ÞÛÙ×Ò º æã ËÌÔÁÚ×ÔÛòÚÑÐÛÒø°Á¼·®ô °Áº·´»²¿³»ôùÉù÷å

Page 217: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 5-1: Using the ËÌÔÁÚ×ÔÛ Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 105

ËÌÔÁÚ×ÔÛòÐËÌÁÔ×ÒÛøºô ùÛ³°´±§»»­ ©¸± »¿®² ³±®» ¬¸¿² ¿ª»®¿¹» ­¿´¿®§æ ù÷å ËÌÔÁÚ×ÔÛòÐËÌÁÔ×ÒÛøºô ùÎÛÐÑÎÌ ÙÛÒÛÎßÌÛÜ ÑÒ ù ¤¤ÍÇÍÜßÌÛ÷å ËÌÔÁÚ×ÔÛòÒÛÉÁÔ×ÒÛøº÷å ÚÑÎ »³° ×Ò ½«®Á¿ª¹ ÔÑÑÐ ËÌÔÁÚ×ÔÛòÐËÌÁÔ×ÒÛøºô ÎÐßÜø»³°ò´¿­¬Á²¿³»ô íð÷ ¤¤ ù ù ¤¤ ÔÐßÜøÒÊÔøÌÑÁÝØßÎø»³°ò¼»°¿®¬³»²¬Á·¼ôùççççù÷ôùóù÷ô ë÷ ¤¤ ù ù ¤¤ ÔÐßÜøÌÑÁÝØßÎø»³°ò­¿´¿®§ô ùüççôçççòððù÷ô ïî÷÷å ÛÒÜ ÔÑÑÐå ËÌÔÁÚ×ÔÛòÒÛÉÁÔ×ÒÛøº÷å ËÌÔÁÚ×ÔÛòÐËÌÁÔ×ÒÛøºô ùööö ÛÒÜ ÑÚ ÎÛÐÑÎÌ öööù÷å ËÌÔÁÚ×ÔÛòÚÝÔÑÍÛøº÷å ÛÒÜ »³°´±§»»Á®»°±®¬å ñ

b) Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the procedure.

Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the procedure.

2) Invoke the procedure using the following as arguments:

a) Use ÎÛÐÑÎÌÍÁÜ×Î as the alias for the directory object as the first parameter.

b) Use ­¿´Á®°¬êïò¬¨¬ as the second parameter.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðëÁðîò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to execute the procedure. The result is shown below.

óó Ú±® »¨¿³°´»ô ·º §±« ¿®» ­¬«¼»²¬ ±®¿êïô «­» êï ¿­ ¿ °®»º·¨ ÛÈÛÝËÌÛ »³°´±§»»Á®»°±®¬øùÎÛÐÑÎÌÍÁÜ×Îùôù­¿´Á®°¬êïò¬¨¬ù÷

3) View the generated output text file as follows:

Page 218: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 5-1: Using the ËÌÔÁÚ×ÔÛ Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 106

a) Double-click the Terminal icon on your desktop. The Terminal window is displayed.

b) At the $ prompt, change to the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ®»°±®¬­ folder that contains the generated output file, ­¿´Á®°¬êïò¬¨¬ using the ½¼ command as follows:

Note: You can use the °©¼ command to list the current working directory as shown in the screenshot above.

c) List the contents of the current directory using the ´­ command as follows:

Note the generated output file, ­¿´Á®°¬êïò¬¨¬.

d) Open the transferred the ­¿´Á®°¬êïò¬¨¬, file using ¹»¼·¬ or an editor of your choice. The report is displayed as follows:

Page 219: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 5-1: Using the ËÌÔÁÚ×ÔÛ Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 107

. . .

Page 220: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 108

Practices and Solutions for Lesson 6

In this practice, you create a package that uses Native Dynamic SQL to create or drop a table, and to populate, modify, and delete rows from the table. In addition, you create a package that compiles the PL/SQL code in your schema, either all the PL/SQL code or only code that has an ×ÒÊßÔ×Ü status in the ËÍÛÎÁÑÞÖÛÝÌÍ table.

Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

Page 221: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 109

Practice 6-1: Using Native Dynamic SQL

In this practice, you create a package that uses Native Dynamic SQL to create or drop a table, and to populate, modify, and delete rows from the table. In addition, you create a package that compiles the PL/SQL code in your schema, either all the PL/SQL code or only code that has an ×ÒÊßÔ×Ü status in the ËÍÛÎÁÑÞÖÛÝÌÍ table.

1) Create a package called ÌßÞÔÛÁÐÕÙ that uses Native Dynamic SQL to create or drop a table, and to populate, modify, and delete rows from the table. The subprograms should manage optional default parameters with ÒËÔÔ values.

a) Create a package specification with the following procedures: ÐÎÑÝÛÜËÎÛ ³¿µ»ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á½±´Á­°»½­ ÊßÎÝØßÎî÷ ÐÎÑÝÛÜËÎÛ ¿¼¼Á®±©ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á½±´Áª¿´«»­ ÊßÎÝØßÎîô °Á½±´­ ÊßÎÝØßÎî æã ÒËÔÔ÷ ÐÎÑÝÛÜËÎÛ «°¼Á®±©ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á­»¬Áª¿´«»­ ÊßÎÝØßÎîô °Á½±²¼·¬·±²­ ÊßÎÝØßÎî æã ÒËÔÔ÷ ÐÎÑÝÛÜËÎÛ ¼»´Á®±©ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á½±²¼·¬·±²­ ÊßÎÝØßÎî æã ÒËÔÔ÷å ÐÎÑÝÛÜËÎÛ ®»³±ª»ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎî÷

b) Create the package body that accepts the parameters and dynamically constructs the appropriate SQL statements that are executed using Native Dynamic SQL, except for the remove procedure. This procedure should be written using the ÜÞÓÍÁÍÏÔ package.

c) Execute the ÓßÕÛ package procedure to create a table as follows: ³¿µ»øù³§Á½±²¬¿½¬­ùô ù·¼ ²«³¾»®øì÷ô ²¿³» ª¿®½¸¿®îøìð÷ù÷å

d) Describe the ÓÇÁÝÑÒÌßÝÌÍ table structure.

e) Execute the ßÜÜÁÎÑÉ package procedure to add the following rows. Enable ÍÛÎÊÛÎÑËÌÐËÌ. ¿¼¼Á®±©øù³§Á½±²¬¿½¬­ùôùïôùùÔ¿«®¿² Í»®¸¿´ùùùôù·¼ô ²¿³»ù÷å ¿¼¼Á®±©øù³§Á½±²¬¿½¬­ùôùîôùùÒ¿²½§ùùùôù·¼ô ²¿³»ù÷å ¿¼¼Á®±©øù³§Á½±²¬¿½¬­ùôùíôùùÍ«²·¬¸¿ כּ´ùùùôù·¼ô²¿³»ù÷å ¿¼¼Á®±©øù³§Á½±²¬¿½¬­ùôùìôùùÊ¿´´· п¬¿¾¿´´¿ùùùôù·¼ô²¿³»ù÷å

f) Query the ÓÇÁÝÑÒÌßÝÌÍ table contents to verify the additions.

g) Execute the ÜÛÔÁÎÑÉ package procedure to delete a contact with an ID value of í.

h) Execute the ËÐÜÁÎÑÉ procedure with the following row data: «°¼Á®±©øù³§Á½±²¬¿½¬­ùôù²¿³»ãùùÒ¿²½§ Ù®»»²¾»®¹ùùùôù·¼ãîù÷å

i) Query the ÓÇÁÝÑÒÌßÝÌÍ table contents to verify the changes.

Page 222: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 6-1: Using Native Dynamic SQL (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 110

j) Drop the table by using the remove procedure and describe the ÓÇÁÝÑÒÌßÝÌÍ table.

2) Create a ÝÑÓÐ×ÔÛÁÐÕÙ package that compiles the PL/SQL code in your schema.

a) In the specification, create a package procedure called ÓßÕÛ that accepts the name of a PL/SQL program unit to be compiled.

b) In the package body, include the following:

i) The ÛÈÛÝËÌÛ procedure used in the ÌßÞÔÛÁÐÕÙ procedure in step 1 of this practice.

ii) A private function named ÙÛÌÁÌÇÐÛ to determine the PL/SQL object type from the data dictionary.

- The function returns the type name (use ÐßÝÕßÙÛ for a package with a body) if the object exists; otherwise, it should return a ÒËÔÔ.

- In the ÉØÛÎÛ clause condition, add the following to the condition to ensure that only one row is returned if the name represents a ÐßÝÕßÙÛ, which may also have a ÐßÝÕßÙÛ ÞÑÜÇ. In this case, you can only compile the complete package, but not the specification or body as separate components:

®±©²«³ ã ï

iii) Create the ÓßÕÛ procedure by using the following information:

- The ÓßÕÛ procedure accepts one argument, ²¿³», which represents the object name.

- The ÓßÕÛ procedure should call the ÙÛÌÁÌÇÐÛ function. If the object exists, ÓßÕÛ dynamically compiles it with the ßÔÌÛÎ statement.

c) Use the ÝÑÓÐ×ÔÛÁÐÕÙòÓßÕÛ procedure to compile the following:

i) The ÛÓÐÔÑÇÛÛÁÎÛÐÑÎÌ procedure

ii) The ÛÓÐÁÐÕÙ package

iii) A nonexistent object called ÛÓÐÁÜßÌß

Page 223: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 111

Practice Solutions 6-1: Using Native Dynamic SQL

In this practice, you create a package that uses Native Dynamic SQL to create or drop a table, and to populate, modify, and delete rows from the table. In addition, you create a package that compiles the PL/SQL code in your schema, either all the PL/SQL code or only code that has an ×ÒÊßÔ×Ü status in the ËÍÛÎÁÑÞÖÛÝÌÍ table.

1) Create a package called ÌßÞÔÛÁÐÕÙ that uses Native Dynamic SQL to create or drop a table, and to populate, modify, and delete rows from the table. The subprograms should manage optional default parameters with ÒËÔÔ values.

a) Create a package specification with the following procedures: ÐÎÑÝÛÜËÎÛ ³¿µ»ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á½±´Á­°»½­ ÊßÎÝØßÎî÷ ÐÎÑÝÛÜËÎÛ ¿¼¼Á®±©ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á½±´Áª¿´«»­ ÊßÎÝØßÎîô °Á½±´­ ÊßÎÝØßÎî æã ÒËÔÔ÷ ÐÎÑÝÛÜËÎÛ «°¼Á®±©ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á­»¬Áª¿´«»­ ÊßÎÝØßÎîô °Á½±²¼·¬·±²­ ÊßÎÝØßÎî æã ÒËÔÔ÷ ÐÎÑÝÛÜËÎÛ ¼»´Á®±©ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á½±²¼·¬·±²­ ÊßÎÝØßÎî æã ÒËÔÔ÷å ÐÎÑÝÛÜËÎÛ ®»³±ª»ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎî÷

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðêÁðïÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the package specification. The code and the result are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ¬¿¾´»Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ³¿µ»ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á½±´Á­°»½­ ÊßÎÝØßÎî÷å ÐÎÑÝÛÜËÎÛ ¿¼¼Á®±©ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á½±´Áª¿´«»­ ÊßÎÝØßÎîô °Á½±´­ ÊßÎÝØßÎî æã ÒËÔÔ÷å ÐÎÑÝÛÜËÎÛ «°¼Á®±©ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á­»¬Áª¿´«»­ ÊßÎÝØßÎîô °Á½±²¼·¬·±²­ ÊßÎÝØßÎî æã ÒËÔÔ÷å ÐÎÑÝÛÜËÎÛ ¼»´Á®±©ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á½±²¼·¬·±²­ ÊßÎÝØßÎî æã ÒËÔÔ÷å ÐÎÑÝÛÜËÎÛ ®»³±ª»ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎî÷å ÛÒÜ ¬¿¾´»Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 224: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 6-1: Using Native Dynamic SQL (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 112

b) Create the package body that accepts the parameters and dynamically constructs the appropriate SQL statements that are executed using Native Dynamic SQL, except for the remove procedure. This procedure should be written using the ÜÞÓÍÁÍÏÔ package.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðêÁðïÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the package specification. The code and the result are shown below.

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ ¬¿¾´»Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ »¨»½«¬»ø°Á­¬³¬ ÊßÎÝØßÎî÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø°Á­¬³¬÷å ÛÈÛÝËÌÛ ×ÓÓÛÜ×ßÌÛ °Á­¬³¬å ÛÒÜå ÐÎÑÝÛÜËÎÛ ³¿µ»ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á½±´Á­°»½­ ÊßÎÝØßÎî÷ ×Í ªÁ­¬³¬ ÊßÎÝØßÎîøîðð÷ æã ùÝÎÛßÌÛ ÌßÞÔÛ ù¤¤ °Á¬¿¾´»Á²¿³» ¤¤ ù øù ¤¤ °Á½±´Á­°»½­ ¤¤ ù÷ùå ÞÛÙ×Ò »¨»½«¬»øªÁ­¬³¬÷å ÛÒÜå ÐÎÑÝÛÜËÎÛ ¿¼¼Á®±©ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á½±´Áª¿´«»­ ÊßÎÝØßÎîô °Á½±´­ ÊßÎÝØßÎî æã ÒËÔÔ÷ ×Í ªÁ­¬³¬ ÊßÎÝØßÎîøîðð÷ æã ù×ÒÍÛÎÌ ×ÒÌÑ ù¤¤ °Á¬¿¾´»Á²¿³»å ÞÛÙ×Ò ×Ú °Á½±´­ ×Í ÒÑÌ ÒËÔÔ ÌØÛÒ ªÁ­¬³¬ æã ªÁ­¬³¬ ¤¤ ù øù ¤¤ °Á½±´­ ¤¤ ù÷ùå ÛÒÜ ×Úå ªÁ­¬³¬ æã ªÁ­¬³¬ ¤¤ ù ÊßÔËÛÍ øù ¤¤ °Á½±´Áª¿´«»­ ¤¤ ù÷ùå »¨»½«¬»øªÁ­¬³¬÷å ÛÒÜå ÐÎÑÝÛÜËÎÛ «°¼Á®±©ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á­»¬Áª¿´«»­ ÊßÎÝØßÎîô °Á½±²¼·¬·±²­ ÊßÎÝØßÎî æã ÒËÔÔ÷ ×Í ªÁ­¬³¬ ÊßÎÝØßÎîøîðð÷ æã ùËÐÜßÌÛ ù¤¤ °Á¬¿¾´»Á²¿³» ¤¤ ù ÍÛÌ ù ¤¤ °Á­»¬Áª¿´«»­å ÞÛÙ×Ò ×Ú °Á½±²¼·¬·±²­ ×Í ÒÑÌ ÒËÔÔ ÌØÛÒ ªÁ­¬³¬ æã ªÁ­¬³¬ ¤¤ ù ÉØÛÎÛ ù ¤¤ °Á½±²¼·¬·±²­å ÛÒÜ ×Úå »¨»½«¬»øªÁ­¬³¬÷å ÛÒÜå

Page 225: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 6-1: Using Native Dynamic SQL (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 113

ÐÎÑÝÛÜËÎÛ ¼»´Á®±©ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎîô °Á½±²¼·¬·±²­ ÊßÎÝØßÎî æã ÒËÔÔ÷ ×Í ªÁ­¬³¬ ÊßÎÝØßÎîøîðð÷ æã ùÜÛÔÛÌÛ ÚÎÑÓ ù¤¤ °Á¬¿¾´»Á²¿³»å ÞÛÙ×Ò ×Ú °Á½±²¼·¬·±²­ ×Í ÒÑÌ ÒËÔÔ ÌØÛÒ ªÁ­¬³¬ æã ªÁ­¬³¬ ¤¤ ù ÉØÛÎÛ ù ¤¤ °Á½±²¼·¬·±²­å ÛÒÜ ×Úå »¨»½«¬»øªÁ­¬³¬÷å ÛÒÜå ÐÎÑÝÛÜËÎÛ ®»³±ª»ø°Á¬¿¾´»Á²¿³» ÊßÎÝØßÎî÷ ×Í ½«®Á·¼ ×ÒÌÛÙÛÎå ªÁ­¬³¬ ÊßÎÝØßÎîøïðð÷ æã ùÜÎÑÐ ÌßÞÔÛ ù¤¤°Á¬¿¾´»Á²¿³»å ÞÛÙ×Ò ½«®Á·¼ æã ÜÞÓÍÁÍÏÔòÑÐÛÒÁÝËÎÍÑÎå ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøªÁ­¬³¬÷å ÜÞÓÍÁÍÏÔòÐßÎÍÛø½«®Á·¼ô ªÁ­¬³¬ô ÜÞÓÍÁÍÏÔòÒßÌ×ÊÛ÷å óó п®­» »¨»½«¬»­ ÜÜÔ ­¬¿¬»³»²¬­ô²± ÛÈÛÝËÌÛ ·­ ®»¯«·®»¼ò ÜÞÓÍÁÍÏÔòÝÔÑÍÛÁÝËÎÍÑÎø½«®Á·¼÷å ÛÒÜå ÛÒÜ ¬¿¾´»Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

c) Execute the ÓßÕÛ package procedure to create a table as follows: ³¿µ»øù³§Á½±²¬¿½¬­ùô ù·¼ ²«³¾»®øì÷ô ²¿³» ª¿®½¸¿®îøìð÷ù÷å

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðêÁðïÁ½ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create the package specification. The code and the results are displayed as follows:

ÛÈÛÝËÌÛ ¬¿¾´»Á°µ¹ò³¿µ»øù³§Á½±²¬¿½¬­ùô ù·¼ ²«³¾»®øì÷ô ²¿³» ª¿®½¸¿®îøìð÷ù÷

Page 226: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 6-1: Using Native Dynamic SQL (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 114

d) Describe the ÓÇÁÝÑÒÌßÝÌÍ table structure.

The code and the results are displayed as follows:

e) Execute the ßÜÜÁÎÑÉ package procedure to add the following rows. Enable

ÍÛÎÊÛÎÑËÌÐËÌ. ¿¼¼Á®±©øù³§Á½±²¬¿½¬­ùôùïôùùÔ¿«®¿² Í»®¸¿´ùùùôù·¼ô ²¿³»ù÷å ¿¼¼Á®±©øù³§Á½±²¬¿½¬­ùôùîôùùÒ¿²½§ùùùôù·¼ô ²¿³»ù÷å ¿¼¼Á®±©øù³§Á½±²¬¿½¬­ùôùíôùùÍ«²·¬¸¿ כּ´ùùùôù·¼ô²¿³»ù÷å ¿¼¼Á®±©øù³§Á½±²¬¿½¬­ùôùìôùùÊ¿´´· п¬¿¾¿´´¿ùùùôù·¼ô²¿³»ù÷å

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðêÁðïÁ»ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to execute the script.

Page 227: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 6-1: Using Native Dynamic SQL (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 115

f) Query the ÓÇÁÝÑÒÌßÝÌÍ table contents to verify the additions.

The code and the results are displayed as follows:

g) Execute the ÜÛÔÁÎÑÉ package procedure to delete a contact with an ID value of í.

The code and the results are displayed as follows:

Page 228: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 6-1: Using Native Dynamic SQL (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 116

h) Execute the ËÐÜÁÎÑÉ procedure with the following row data: «°¼Á®±©øù³§Á½±²¬¿½¬­ùôù²¿³»ãùùÒ¿²½§ Ù®»»²¾»®¹ùùùôù·¼ãîù÷å

The code and the results are displayed as follows:

i) Query the ÓÇÁÝÑÒÌßÝÌÍ table contents to verify the changes.

The code and the results are displayed as follows:

j) Drop the table by using the remove procedure and describe the ÓÇÁÝÑÒÌßÝÌÍ table.

The code and the results are displayed as follows:

Page 229: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 6-1: Using Native Dynamic SQL (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 117

2) Create a ÝÑÓÐ×ÔÛÁÐÕÙ package that compiles the PL/SQL code in your schema.

a) In the specification, create a package procedure called ÓßÕÛ that accepts the name of a PL/SQL program unit to be compiled.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðêÁðîÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the package specification. The code and the results are shown below.

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ½±³°·´»Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ³¿µ»ø°Á²¿³» ÊßÎÝØßÎî÷å ÛÒÜ ½±³°·´»Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 230: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 6-1: Using Native Dynamic SQL (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 118

b) In the package body, include the following:

i) The ÛÈÛÝËÌÛ procedure used in the ÌßÞÔÛÁÐÕÙ procedure in step 1 of this practice.

ii) A private function named ÙÛÌÁÌÇÐÛ to determine the PL/SQL object type from the data dictionary.

- The function returns the type name (use ÐßÝÕßÙÛ for a package with a body) if the object exists; otherwise, it should return a ÒËÔÔ.

- In the ÉØÛÎÛ clause condition, add the following to the condition to ensure that only one row is returned if the name represents a ÐßÝÕßÙÛ, which may also have a ÐßÝÕßÙÛ ÞÑÜÇ. In this case, you can only compile the complete package, but not the specification or body as separate components:

®±©²«³ ã ï

iii) Create the ÓßÕÛ procedure by using the following information:

- The ÓßÕÛ procedure accepts one argument, ²¿³», which represents the object name.

- The ÓßÕÛ procedure should call the ÙÛÌÁÌÇÐÛ function. If the object exists, ÓßÕÛ dynamically compiles it with the ßÔÌÛÎ statement.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðêÁðîÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the package body. The code and the results are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ ½±³°·´»Á°µ¹ ×Í

ÐÎÑÝÛÜËÎÛ »¨»½«¬»ø°Á­¬³¬ ÊßÎÝØßÎî÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø°Á­¬³¬÷å ÛÈÛÝËÌÛ ×ÓÓÛÜ×ßÌÛ °Á­¬³¬å ÛÒÜå

ÚËÒÝÌ×ÑÒ ¹»¬Á¬§°»ø°Á²¿³» ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÊßÎÝØßÎî ×Í ªÁ°®±½Á¬§°» ÊßÎÝØßÎîøíð÷ æã ÒËÔÔå ÞÛÙ×Ò ñö ö ̸» ÎÑÉÒËÓ ã ï ·­ ¿¼¼»¼ ¬± ¬¸» ½±²¼·¬·±² ö ¬± »²­«®» ±²´§ ±²» ®±© ·­ ®»¬«®²»¼ ·º ¬¸» ö ²¿³» ®»°®»­»²¬­ ¿ ÐßÝÕßÙÛô ©¸·½¸ ³¿§ ¿´­± ö ¸¿ª» ¿ ÐßÝÕßÙÛ ÞÑÜÇò ײ ¬¸·­ ½¿­»ô ©» ½¿² ö ±²´§ ½±³°·´» ¬¸» ½±³°´»¬» °¿½µ¿¹»ô ¾«¬ ²±¬ ö ¬¸» ­°»½·º·½¿¬·±² ±® ¾±¼§ ¿­ ­»°¿®¿¬» ö ½±³°±²»²¬­ò öñ

Page 231: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 6-1: Using Native Dynamic SQL (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 119

ÍÛÔÛÝÌ ±¾¶»½¬Á¬§°» ×ÒÌÑ ªÁ°®±½Á¬§°» ÚÎÑÓ «­»®Á±¾¶»½¬­ ÉØÛÎÛ ±¾¶»½¬Á²¿³» ã ËÐÐÛÎø°Á²¿³»÷ ßÒÜ ÎÑÉÒËÓ ã ïå ÎÛÌËÎÒ ªÁ°®±½Á¬§°»å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÒËÔÔå ÛÒÜå ÐÎÑÝÛÜËÎÛ ³¿µ»ø°Á²¿³» ÊßÎÝØßÎî÷ ×Í ªÁ­¬³¬ ÊßÎÝØßÎîøïðð÷å ªÁ°®±½Á¬§°» ÊßÎÝØßÎîøíð÷ æã ¹»¬Á¬§°»ø°Á²¿³»÷å ÞÛÙ×Ò ×Ú ªÁ°®±½Á¬§°» ×Í ÒÑÌ ÒËÔÔ ÌØÛÒ ªÁ­¬³¬ æã ùßÔÌÛÎ ù¤¤ ªÁ°®±½Á¬§°» ¤¤ù ù¤¤ °Á²¿³» ¤¤ù ÝÑÓÐ×ÔÛùå »¨»½«¬»øªÁ­¬³¬÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðððïô ùÍ«¾°®±¹®¿³ ùùù¤¤ °Á²¿³» ¤¤ùùù ¼±»­ ²±¬ »¨·­¬ù÷å ÛÒÜ ×Úå ÛÒÜ ³¿µ»å ÛÒÜ ½±³°·´»Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

c) Use the ÝÑÓÐ×ÔÛÁÐÕÙòÓßÕÛ procedure to compile the following:

i) The ÛÓÐÔÑÇÛÛÁÎÛÐÑÎÌ procedure

ii) The ÛÓÐÁÐÕÙ package

iii) A nonexistent object called ÛÓÐÁÜßÌß

Open the file in the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ ­±´ÁðêÁðîÁ½ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to execute the package�s procedure. The code and the results are shown below.

ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ

ÛÈÛÝËÌÛ ½±³°·´»Á°µ¹ò³¿µ»øù»³°´±§»»Á®»°±®¬ù÷ ÛÈÛÝËÌÛ ½±³°·´»Á°µ¹ò³¿µ»øù»³°Á°µ¹ù÷ ÛÈÛÝËÌÛ ½±³°·´»Á°µ¹ò³¿µ»øù»³°Á¼¿¬¿ù÷

Page 232: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 6-1: Using Native Dynamic SQL (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 120

Page 233: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 121

Practices and Solutions for Lesson 7

In this practice, you create a package that performs a bulk fetch of employees in a specified department. The data is stored in a PL/SQL table in the package. You also provide a procedure to display the contents of the table. In addition, you create the ¿¼¼Á»³°´±§»» procedure that inserts new employees. The procedure uses a local autonomous subprogram to write a log record each time the ¿¼¼Á»³°´±§»» procedure is called, whether it successfully adds a record or not. Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

Page 234: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 122

Practice 7-1: Using Bulk Binding and Autonomous Transactions

In this practice, you create a package that performs a bulk fetch of employees in a specified department. The data is stored in a PL/SQL table in the package. You also provide a procedure to display the contents of the table. In addition, you create the ¿¼¼Á»³°´±§»» procedure that inserts new employees. The procedure uses a local autonomous subprogram to write a log record each time the ¿¼¼Á»³°´±§»» procedure is called, whether it successfully adds a record or not.

1) Update the ÛÓÐÁÐÕÙ package with a new procedure to query employees in a specified department.

a) In the package specification:

i) Declare a ¹»¬Á»³°´±§»»­ procedure with a parameter called ¼»°¬Á·¼, which is based on the »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ column type

ii) Define an index-by PL/SQL type as a ÌßÞÔÛ ÑÚ ÛÓÐÔÑÇÛÛÍûÎÑÉÌÇÐÛ

b) In the package body:

i) Define a private variable called »³°Á¬¿¾´» based on the type defined in the specification to hold employee records

ii) Implement the ¹»¬Á»³°´±§»»­ procedure to bulk fetch the data into the table

c) Create a new procedure in the specification and body, called ­¸±©Á»³°´±§»»­, which does not take arguments. The procedure displays the contents of the private PL/SQL table variable (if any data exists). Use the °®·²¬Á»³°´±§»» procedure that you created in an earlier practice. To view the results, click the Enable DBMS Output icon in the DBMS Output tab in SQL Developer, if you have not already done so.

d) Enable ÍÛÎÊÛÎÑËÌÐËÌ. Invoke the »³°Á°µ¹ò¹»¬Á»³°´±§»»­ procedure for department íð, and then invoke »³°Á°µ¹ò­¸±©Á»³°´±§»»­. Repeat this for department êð.

2) Your manager wants to keep a log whenever the ¿¼¼Á»³°´±§»» procedure in the package is invoked to insert a new employee into the ÛÓÐÔÑÇÛÛÍ table.

a) First, load and execute the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðéÁðîÁ¿ò­¯´ script to create a log table called ÔÑÙÁÒÛÉÛÓÐ, and a sequence called ´±¹Á²»©»³°Á­»¯.

b) In the ÛÓÐÁÐÕÙ package body, modify the ¿¼¼Á»³°´±§»» procedure, which performs the actual ×ÒÍÛÎÌ operation. Add a local procedure called ¿«¼·¬Á²»©»³° as follows:

i) The ¿«¼·¬Á²»©»³° procedure must use an autonomous transaction to insert a log record into the ÔÑÙÁÒÛÉÛÓÐ table.

Page 235: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 123

ii) Store the ËÍÛÎ, the current time, and the new employee name in the log table row.

iii) Use ´±¹Á²»©»³°Á­»¯ to set the »²¬®§Á·¼ column.

Note: Remember to perform a ÝÑÓÓ×Ì operation in a procedure with an autonomous transaction.

c) Modify the ¿¼¼Á»³°´±§»» procedure to invoke ¿«¼·¬Á»³° before it performs the insert operation.

d) Invoke the ¿¼¼Á»³°´±§»» procedure for these new employees: Ó¿¨ ͳ¿®¬ in department 20 and Ý´¿®µ Õ»²¬ in department 10. What happens?

c) Query the two ÛÓÐÔÑÇÛÛÍ records added, and the records in the ÔÑÙÁÒÛÉÛÓÐ table. How many log records are present?

d) Execute a ÎÑÔÔÞßÝÕ statement to undo the insert operations that have not been committed. Use the same queries from step 2 e. as follows:

i) Use the first query to check whether the employee rows for ͳ¿®¬ and Õ»²¬ have been removed.

ii) Use the second query to check the log records in the ÔÑÙÁÒÛÉÛÓÐ table. How many log records are present? Why?

Page 236: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 124

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions

In this practice, you create a package that performs a bulk fetch of employees in a specified department. The data is stored in a PL/SQL table in the package. You also provide a procedure to display the contents of the table. In addition, you create the ¿¼¼Á»³°´±§»» procedure that inserts new employees. The procedure uses a local autonomous subprogram to write a log record each time the ¿¼¼Á»³°´±§»» procedure is called, whether it successfully adds a record or not.

1) Update the ÛÓÐÁÐÕÙ package with a new procedure to query employees in a specified department.

a) In the package specification:

i) Declare a ¹»¬Á»³°´±§»»­ procedure with a parameter called ¼»°¬Á·¼, which is based on the »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ column type

ii) Define an index-by PL/SQL type as a ÌßÞÔÛ ÑÚ ÛÓÐÔÑÇÛÛÍûÎÑÉÌÇÐÛ

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­/­±´ÁðéÁðïÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the specification. The code and the results are displayed as follows. The newly added code is highlighted in bold letters in the code box below. ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í ÌÇÐÛ »³°Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ »³°´±§»»­ûÎÑÉÌÇÐÛå ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å

Page 237: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 125

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»­ø°Á¼»°¬Á·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼û¬§°»÷å ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­å ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

b) In the package body:

i) Define a private variable called »³°Á¬¿¾´» based on the type defined in the specification to hold employee records

ii) Implement the ¹»¬Á»³°´±§»»­ procedure to bulk fetch the data into the table

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðéÁðïÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the package body. The code and the results are shown below. The newly added code is highlighted in bold letters in the code box below. ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ÌÇÐÛ ¾±±´»¿²Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ÞÑÑÔÛßÒ ×ÒÜÛÈ ÞÇ Þ×ÒßÎÇÁ×ÒÌÛÙÛÎå ª¿´·¼Á¼»°¿®¬³»²¬­ ¾±±´»¿²Á¬¿¾Á¬§°»å »³°Á¬¿¾´» »³°Á¬¿¾Á¬§°»å

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô

Page 238: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 126

°Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í ÞÛÙ×Ò ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷ ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ×Í °Á»³¿·´ »³°´±§»»­ò»³¿·´û¬§°»å ÞÛÙ×Ò °Á»³¿·´ æã ËÐÐÛÎøÍËÞÍÌÎø°Áº·®­¬Á²¿³»ô ïô ï÷¤¤ÍËÞÍÌÎø°Á´¿­¬Á²¿³»ô ïô é÷÷å ¿¼¼Á»³°´±§»»ø°Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¼»°¬·¼ ãâ °Á¼»°¬·¼÷å ÛÒÜå ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼ ×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå

Page 239: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 127

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã °Áº¿³·´§Á²¿³»å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ñö Ò»© ¹»¬Á»³°´±§»»­ °®±½»¼«®»ò öñ ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»­ø°Á¼»°¬Á·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼û¬§°»÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ö ÞËÔÕ ÝÑÔÔÛÝÌ ×ÒÌÑ »³°Á¬¿¾´» ÚÎÑÓ ÛÓÐÔÑÇÛÛÍ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ ã °Á¼»°¬Á·¼å ÛÒÜå ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­ ×Í ÞÛÙ×Ò ÚÑÎ ®»½ ×Ò øÍÛÔÛÝÌ ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ ¼»°¿®¬³»²¬­÷ ÔÑÑÐ ª¿´·¼Á¼»°¿®¬³»²¬­ø®»½ò¼»°¿®¬³»²¬Á·¼÷ æã ÌÎËÛå ÛÒÜ ÔÑÑÐå ÛÒÜå ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø°Á®»½Á»³°ò¼»°¿®¬³»²¬Á·¼ ¤¤ù ù¤¤ °Á®»½Á»³°ò»³°´±§»»Á·¼¤¤ù ù¤¤ °Á®»½Á»³°òº·®­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò´¿­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò¶±¾Á·¼¤¤ù ù¤¤ °Á®»½Á»³°ò­¿´¿®§÷å ÛÒÜå ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÎÛÌËÎÒ ª¿´·¼Á¼»°¿®¬³»²¬­ò»¨·­¬­ø°Á¼»°¬·¼÷å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å

Page 240: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 128

ÞÛÙ×Ò ·²·¬Á¼»°¿®¬³»²¬­å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

c) Create a new procedure in the specification and body, called

­¸±©Á»³°´±§»»­, which does not take arguments. The procedure displays the contents of the private PL/SQL table variable (if any data exists). Use the °®·²¬Á»³°´±§»» procedure that you created in an earlier practice. To view the results, click the Enable DBMS Output icon in the DBMS Output tab in SQL Developer, if you have not already done so.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðéÁðïÁ½ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to re-create and compile the package with the new procedure. The code and the results are shown below.

óó п½µ¿¹» ÍÐÛÝ×Ú×ÝßÌ×ÑÒ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í ÌÇÐÛ »³°Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ »³°´±§»»­ûÎÑÉÌÇÐÛå ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø

Page 241: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 129

°Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»­ø°Á¼»°¬Á·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼û¬§°»÷å ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­å ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷å ÐÎÑÝÛÜËÎÛ ­¸±©Á»³°´±§»»­å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ óó п½µ¿¹» ÞÑÜÇ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ÌÇÐÛ ¾±±´»¿²Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ÞÑÑÔÛßÒ ×ÒÜÛÈ ÞÇ Þ×ÒßÎÇÁ×ÒÌÛÙÛÎå ª¿´·¼Á¼»°¿®¬³»²¬­ ¾±±´»¿²Á¬¿¾Á¬§°»å »³°Á¬¿¾´» »³°Á¬¿¾Á¬§°»å ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒå

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í ÞÛÙ×Ò ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷

Page 242: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 130

ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ×Í °Á»³¿·´ »³°´±§»»­ò»³¿·´û¬§°»å ÞÛÙ×Ò °Á»³¿·´ æã ËÐÐÛÎøÍËÞÍÌÎø°Áº·®­¬Á²¿³»ô ïô ï÷¤¤ÍËÞÍÌÎø°Á´¿­¬Á²¿³»ô ïô é÷÷å ¿¼¼Á»³°´±§»»ø°Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¼»°¬·¼ ãâ °Á¼»°¬·¼÷å ÛÒÜå ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼ ×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã °Áº¿³·´§Á²¿³»å

Page 243: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 131

ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»­ø°Á¼»°¬Á·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼û¬§°»÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ö ÞËÔÕ ÝÑÔÔÛÝÌ ×ÒÌÑ »³°Á¬¿¾´» ÚÎÑÓ ÛÓÐÔÑÇÛÛÍ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ ã °Á¼»°¬Á·¼å ÛÒÜå ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­ ×Í ÞÛÙ×Ò ÚÑÎ ®»½ ×Ò øÍÛÔÛÝÌ ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ ¼»°¿®¬³»²¬­÷ ÔÑÑÐ ª¿´·¼Á¼»°¿®¬³»²¬­ø®»½ò¼»°¿®¬³»²¬Á·¼÷ æã ÌÎËÛå ÛÒÜ ÔÑÑÐå ÛÒÜå ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø°Á®»½Á»³°ò¼»°¿®¬³»²¬Á·¼ ¤¤ù ù¤¤ °Á®»½Á»³°ò»³°´±§»»Á·¼¤¤ù ù¤¤ °Á®»½Á»³°òº·®­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò´¿­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò¶±¾Á·¼¤¤ù ù¤¤ °Á®»½Á»³°ò­¿´¿®§÷å ÛÒÜå ÐÎÑÝÛÜËÎÛ ­¸±©Á»³°´±§»»­ ×Í ÞÛÙ×Ò ×Ú »³°Á¬¿¾´» ×Í ÒÑÌ ÒËÔÔ ÌØÛÒ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùÛ³°´±§»»­ ·² п½µ¿¹» ¬¿¾´»ù÷å ÚÑÎ · ×Ò ï òò »³°Á¬¿¾´»òÝÑËÒÌ ÔÑÑÐ °®·²¬Á»³°´±§»»ø»³°Á¬¿¾´»ø·÷÷å ÛÒÜ ÔÑÑÐå ÛÒÜ ×Úå ÛÒÜ ­¸±©Á»³°´±§»»­å ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÎÛÌËÎÒ ª¿´·¼Á¼»°¿®¬³»²¬­ò»¨·­¬­ø°Á¼»°¬·¼÷å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å

Page 244: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 132

ÞÛÙ×Ò ·²·¬Á¼»°¿®¬³»²¬­å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

d) Enable ÍÛÎÊÛÎÑËÌÐËÌ. Invoke the »³°Á°µ¹ò¹»¬Á»³°´±§»»­ procedure for department íð, and then invoke »³°Á°µ¹ò­¸±©Á»³°´±§»»­. Repeat this for department êð.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðéÁðïÁ¼ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to invoke the package�s procedures. The code and the results are shown below:

ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ ÛÈÛÝËÌÛ »³°Á°µ¹ò¹»¬Á»³°´±§»»­øíð÷ ÛÈÛÝËÌÛ »³°Á°µ¹ò­¸±©Á»³°´±§»»­ ÛÈÛÝËÌÛ »³°Á°µ¹ò¹»¬Á»³°´±§»»­øêð÷ ÛÈÛÝËÌÛ »³°Á°µ¹ò­¸±©Á»³°´±§»»­

Page 245: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 133

2) Your manager wants to keep a log whenever the ¿¼¼Á»³°´±§»» procedure in the package is invoked to insert a new employee into the ÛÓÐÔÑÇÛÛÍ table.

a) First, load and execute the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðéÁðîÁ¿ò­¯´ script to create a log table called ÔÑÙÁÒÛÉÛÓÐ, and a sequence called ´±¹Á²»©»³°Á­»¯.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðéÁðîÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÝÎÛßÌÛ ÌßÞÔÛ ´±¹Á²»©»³° ø »²¬®§Á·¼ ÒËÓÞÛÎøê÷ ÝÑÒÍÌÎß×ÒÌ ´±¹Á²»©»³°Á°µ ÐÎ×ÓßÎÇ ÕÛÇô «­»®Á·¼ ÊßÎÝØßÎîøíð÷ô ´±¹Á¬·³» ÜßÌÛô ²¿³» ÊßÎÝØßÎîøêð÷ ÷å

ÝÎÛßÌÛ ÍÛÏËÛÒÝÛ ´±¹Á²»©»³°Á­»¯å

Page 246: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 134

b) In the ÛÓÐÁÐÕÙ package body, modify the ¿¼¼Á»³°´±§»» procedure, which performs the actual ×ÒÍÛÎÌ operation. Add a local procedure called ¿«¼·¬Á²»©»³° as follows:

i) The ¿«¼·¬Á²»©»³° procedure must use an autonomous transaction to insert a log record into the ÔÑÙÁÒÛÉÛÓÐ table.

ii) Store the ËÍÛÎ, the current time, and the new employee name in the log table row.

iii) Use ´±¹Á²»©»³°Á­»¯ to set the »²¬®§Á·¼ column.

Note: Remember to perform a ÝÑÓÓ×Ì operation in a procedure with an autonomous transaction. Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðéÁðîÁ¾ò­¯´ script. The newly added code is highlighted in bold letters in the following code box. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are displayed as follows:

óó п½µ¿¹» ÍÐÛÝ×Ú×ÝßÌ×ÑÒ

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í

ÌÇÐÛ »³°Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ »³°´±§»»­ûÎÑÉÌÇÐÛå

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷å

ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å

Page 247: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 135

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å

ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»­ø°Á¼»°¬Á·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼û¬§°»÷å

ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­å

ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷å

ÐÎÑÝÛÜËÎÛ ­¸±©Á»³°´±§»»­å

ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

óó п½µ¿¹» ÞÑÜÇ

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ÌÇÐÛ ¾±±´»¿²Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ÞÑÑÔÛßÒ ×ÒÜÛÈ ÞÇ Þ×ÒßÎÇÁ×ÒÌÛÙÛÎå ª¿´·¼Á¼»°¿®¬³»²¬­ ¾±±´»¿²Á¬¿¾Á¬§°»å »³°Á¬¿¾´» »³°Á¬¿¾Á¬§°»å

ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒå

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í óó Ò»© ´±½¿´ °®±½»¼«®»

ÐÎÑÝÛÜËÎÛ ¿«¼·¬Á²»©»³° ×Í ÐÎßÙÓß ßËÌÑÒÑÓÑËÍÁÌÎßÒÍßÝÌ×ÑÒå «­»®Á·¼ ÊßÎÝØßÎîøíð÷ æã ËÍÛÎå ÞÛÙ×Ò ×ÒÍÛÎÌ ×ÒÌÑ ´±¹Á²»©»³° ø»²¬®§Á·¼ô «­»®Á·¼ô ´±¹Á¬·³»ô

Page 248: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 136

²¿³»÷ ÊßÔËÛÍ ø´±¹Á²»©»³°Á­»¯òÒÛÈÌÊßÔô «­»®Á·¼ô ­§­¼¿¬»ô°Áº·®­¬Á²¿³»¤¤ù ù¤¤°Á´¿­¬Á²¿³»÷å ÝÑÓÓ×Ìå ÛÒÜ ¿«¼·¬Á²»©»³°å

ÞÛÙ×Ò óó ¿¼¼Á»³°´±§»» ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷ ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ×Í °Á»³¿·´ »³°´±§»»­ò»³¿·´û¬§°»å ÞÛÙ×Ò °Á»³¿·´ æã ËÐÐÛÎøÍËÞÍÌÎø°Áº·®­¬Á²¿³»ô ïô ï÷¤¤ÍËÞÍÌÎø°Á´¿­¬Á²¿³»ô ïô é÷÷å ¿¼¼Á»³°´±§»»ø°Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¼»°¬·¼ ãâ °Á¼»°¬·¼÷å ÛÒÜå

ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼ ×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³°

Page 249: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 137

ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã °Áº¿³·´§Á²¿³»å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå

ñö Ò»© ¹»¬Á»³°´±§»»­ °®±½»¼«®»ò öñ

ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»­ø°Á¼»°¬Á·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼û¬§°»÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ö ÞËÔÕ ÝÑÔÔÛÝÌ ×ÒÌÑ »³°Á¬¿¾´» ÚÎÑÓ ÛÓÐÔÑÇÛÛÍ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ ã °Á¼»°¬Á·¼å ÛÒÜå

ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­ ×Í ÞÛÙ×Ò ÚÑÎ ®»½ ×Ò øÍÛÔÛÝÌ ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ ¼»°¿®¬³»²¬­÷ ÔÑÑÐ ª¿´·¼Á¼»°¿®¬³»²¬­ø®»½ò¼»°¿®¬³»²¬Á·¼÷ æã ÌÎËÛå ÛÒÜ ÔÑÑÐå ÛÒÜå

ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø°Á®»½Á»³°ò¼»°¿®¬³»²¬Á·¼ ¤¤ù ù¤¤ °Á®»½Á»³°ò»³°´±§»»Á·¼¤¤ù ù¤¤ °Á®»½Á»³°òº·®­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò´¿­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò¶±¾Á·¼¤¤ù ù¤¤ °Á®»½Á»³°ò­¿´¿®§÷å ÛÒÜå

ÐÎÑÝÛÜËÎÛ ­¸±©Á»³°´±§»»­ ×Í ÞÛÙ×Ò ×Ú »³°Á¬¿¾´» ×Í ÒÑÌ ÒËÔÔ ÌØÛÒ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùÛ³°´±§»»­ ·² п½µ¿¹» ¬¿¾´»ù÷å ÚÑÎ · ×Ò ï òò »³°Á¬¿¾´»òÝÑËÒÌ ÔÑÑÐ °®·²¬Á»³°´±§»»ø»³°Á¬¿¾´»ø·÷÷å

Page 250: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 138

ÛÒÜ ÔÑÑÐå ÛÒÜ ×Úå ÛÒÜ ­¸±©Á»³°´±§»»­å

ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÎÛÌËÎÒ ª¿´·¼Á¼»°¿®¬³»²¬­ò»¨·­¬­ø°Á¼»°¬·¼÷å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å

ÞÛÙ×Ò ·²·¬Á¼»°¿®¬³»²¬­å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

c) Modify the ¿¼¼Á»³°´±§»» procedure to invoke ¿«¼·¬Á»³° before it performs the insert operation.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðéÁðîÁ½ò­¯´script. The newly added code is highlighted in bold letters in the following code box. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

óó п½µ¿¹» ÍÐÛÝ×Ú×ÝßÌ×ÑÒ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í ÌÇÐÛ »³°Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ »³°´±§»»­ûÎÑÉÌÇÐÛå ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô

Page 251: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 139

°Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»­ø°Á¼»°¬Á·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼û¬§°»÷å ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­å ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷å ÐÎÑÝÛÜËÎÛ ­¸±©Á»³°´±§»»­å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ óó п½µ¿¹» ÞÑÜÇ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ÌÇÐÛ ¾±±´»¿²Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ÞÑÑÔÛßÒ ×ÒÜÛÈ ÞÇ Þ×ÒßÎÇÁ×ÒÌÛÙÛÎå ª¿´·¼Á¼»°¿®¬³»²¬­ ¾±±´»¿²Á¬¿¾Á¬§°»å »³°Á¬¿¾´» »³°Á¬¿¾Á¬§°»å ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒå

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô

Page 252: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 140

°Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í ÐÎÑÝÛÜËÎÛ ¿«¼·¬Á²»©»³° ×Í ÐÎßÙÓß ßËÌÑÒÑÓÑËÍÁÌÎßÒÍßÝÌ×ÑÒå «­»®Á·¼ ÊßÎÝØßÎîøíð÷ æã ËÍÛÎå ÞÛÙ×Ò ×ÒÍÛÎÌ ×ÒÌÑ ´±¹Á²»©»³° ø»²¬®§Á·¼ô «­»®Á·¼ô ´±¹Á¬·³»ô ²¿³»÷ ÊßÔËÛÍ ø´±¹Á²»©»³°Á­»¯òÒÛÈÌÊßÔô «­»®Á·¼ô ­§­¼¿¬»ô°Áº·®­¬Á²¿³»¤¤ù ù¤¤°Á´¿­¬Á²¿³»÷å ÝÑÓÓ×Ìå ÛÒÜ ¿«¼·¬Á²»©»³°å ÞÛÙ×Ò óó ¿¼¼Á»³°´±§»» ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ¿«¼·¬Á²»©»³°å ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷ ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ×Í °Á»³¿·´ »³°´±§»»­ò»³¿·´û¬§°»å ÞÛÙ×Ò °Á»³¿·´ æã ËÐÐÛÎøÍËÞÍÌÎø°Áº·®­¬Á²¿³»ô ïô ï÷¤¤ÍËÞÍÌÎø°Á´¿­¬Á²¿³»ô ïô é÷÷å ¿¼¼Á»³°´±§»»ø°Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¼»°¬·¼ ãâ °Á¼»°¬·¼÷å ÛÒÜå

ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô

Page 253: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 141

°Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼ ×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã °Áº¿³·´§Á²¿³»å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»­ø°Á¼»°¬Á·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼û¬§°»÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ö ÞËÔÕ ÝÑÔÔÛÝÌ ×ÒÌÑ »³°Á¬¿¾´» ÚÎÑÓ ÛÓÐÔÑÇÛÛÍ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ ã °Á¼»°¬Á·¼å ÛÒÜå ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­ ×Í ÞÛÙ×Ò ÚÑÎ ®»½ ×Ò øÍÛÔÛÝÌ ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ ¼»°¿®¬³»²¬­÷ ÔÑÑÐ ª¿´·¼Á¼»°¿®¬³»²¬­ø®»½ò¼»°¿®¬³»²¬Á·¼÷ æã ÌÎËÛå ÛÒÜ ÔÑÑÐå ÛÒÜå ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø°Á®»½Á»³°ò¼»°¿®¬³»²¬Á·¼ ¤¤ù ù¤¤ °Á®»½Á»³°ò»³°´±§»»Á·¼¤¤ù ù¤¤

Page 254: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 142

°Á®»½Á»³°òº·®­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò´¿­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò¶±¾Á·¼¤¤ù ù¤¤ °Á®»½Á»³°ò­¿´¿®§÷å ÛÒÜå ÐÎÑÝÛÜËÎÛ ­¸±©Á»³°´±§»»­ ×Í ÞÛÙ×Ò ×Ú »³°Á¬¿¾´» ×Í ÒÑÌ ÒËÔÔ ÌØÛÒ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùÛ³°´±§»»­ ·² п½µ¿¹» ¬¿¾´»ù÷å ÚÑÎ · ×Ò ï òò »³°Á¬¿¾´»òÝÑËÒÌ ÔÑÑÐ °®·²¬Á»³°´±§»»ø»³°Á¬¿¾´»ø·÷÷å ÛÒÜ ÔÑÑÐå ÛÒÜ ×Úå ÛÒÜ ­¸±©Á»³°´±§»»­å ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÎÛÌËÎÒ ª¿´·¼Á¼»°¿®¬³»²¬­ò»¨·­¬­ø°Á¼»°¬·¼÷å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å ÞÛÙ×Ò ·²·¬Á¼»°¿®¬³»²¬­å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

d) Invoke the ¿¼¼Á»³°´±§»» procedure for these new employees: Ó¿¨ ͳ¿®¬ in department 20 and Ý´¿®µ Õ»²¬ in department 10. What happens?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðéÁðîÁ¼ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are as follows.

Page 255: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 143

ÛÈÛÝËÌÛ »³°Á°µ¹ò¿¼¼Á»³°´±§»»øùÓ¿¨ùô ùͳ¿®¬ùô îð÷ ÛÈÛÝËÌÛ »³°Á°µ¹ò¿¼¼Á»³°´±§»»øùÝ´¿®µùô ùÕ»²¬ùô ïð÷

Both insert statements complete successfully. The log table has two log records as shown in the next step.

e) Query the two ÛÓÐÔÑÇÛÛÍ records added, and the records in the ÔÑÙÁÒÛÉÛÓÐ table. How many log records are present?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðéÁðîÁ»ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are displayed as follows:

­»´»½¬ ¼»°¿®¬³»²¬Á·¼ô »³°´±§»»Á·¼ô ´¿­¬Á²¿³»ô º·®­¬Á²¿³» º®±³ »³°´±§»»­ ©¸»®» ´¿­¬Á²¿³» ·² øùÕ»²¬ùô ùͳ¿®¬ù÷å

­»´»½¬ ö º®±³ ´±¹Á²»©»³°å

There are two log records, one for ͳ¿®¬ and another for Õ»²¬.

f) Execute a ÎÑÔÔÞßÝÕ statement to undo the insert operations that have not been committed. Use the same queries from step 2 e. as follows:

Page 256: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 7-1: Using Bulk Binding and Autonomous Transactions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 144

i) Use the first query to check whether the employee rows for ͳ¿®¬ and Õ»²¬ have been removed.

ii) Use the second query to check the log records in the ÔÑÙÁÒÛÉÛÓÐ table. How many log records are present? Why?

ÎÑÔÔÞßÝÕå

The two employee records are removed (rolled back). The two log records remain in the log table because they were inserted using an autonomous transaction, which is unaffected by the rollback performed in the main transaction.

Page 257: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 145

Practices and Solutions for Lesson 8

In this practice, you create statement and row triggers. You also create procedures that are invoked from within the triggers. Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

Page 258: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 146

Practice 8-1: Creating Statement and Row Triggers

In this practice, you create statement and row triggers. You also create procedures that are invoked from within the triggers.

1) The rows in the ÖÑÞÍ table store a minimum and maximum salary allowed for different ÖÑÞÁ×Ü values. You are asked to write code to ensure that employees� salaries fall in the range allowed for their job type, for insert and update operations.

a) Create a procedure called ÝØÛÝÕÁÍßÔßÎÇ as follows:

i) The procedure accepts two parameters, one for an employee�s job ID string and the other for the salary.

ii) The procedure uses the job ID to determine the minimum and maximum salary for the specified job.

iii) If the salary parameter does not fall within the salary range of the job, inclusive of the minimum and maximum, then it should raise an application exception, with the message �ײª¿´·¼ ­¿´¿®§ ä­¿´âò Í¿´¿®·»­ º±® ¶±¾ 䶱¾·¼â ³«­¬ ¾» ¾»¬©»»² ä³·²â ¿²¼ 䳿¨â.� Replace the various items in the message with values supplied by parameters and variables populated by queries. Save the file.

b) Create a trigger called ÝØÛÝÕÁÍßÔßÎÇÁÌÎÙ on the ÛÓÐÔÑÇÛÛÍ table that fires before an ×ÒÍÛÎÌ or ËÐÜßÌÛ operation on each row:

i) The trigger must call the ÝØÛÝÕÁÍßÔßÎÇ procedure to carry out the business logic.

ii) The trigger should pass the new job ID and salary to the procedure parameters.

2) Test the ÝØÛÝÕÁÍßÔÁÌÎÙ trigger using the following cases:

a) Using your ÛÓÐÁÐÕÙòßÜÜÁÛÓÐÔÑÇÛÛ procedure, add employee Û´»¿²±® Þ»¸ to department 30. What happens and why?

b) Update the salary of employee 115 to $2,000. In a separate update operation, change the employee job ID to ØÎÁÎÛÐ. What happens in each case?

c) Update the salary of employee 115 to $2,800. What happens?

3) Update the ÝØÛÝÕÁÍßÔßÎÇÁÌÎÙ trigger to fire only when the job ID or salary values have actually changed.

a) Implement the business rule using a ÉØÛÒ clause to check whether the ÖÑÞÁ×Ü or ÍßÔßÎÇ values have changed.

Note: Make sure that the condition handles the ÒËÔÔ in the ÑÔÜò½±´«³²Á²¿³» values if an ×ÒÍÛÎÌ operation is performed; otherwise, an insert operation will fail.

Page 259: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 8-1: Creating Statement and Row Triggers (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 147

b) Test the trigger by executing the ÛÓÐÁÐÕÙòßÜÜÁÛÓÐÔÑÇÛÛ procedure with the following parameter values:

- °Áº·®­¬Á²¿³»æ ùÛ´»¿²±®ù

- °Á´¿­¬ ²¿³»æ ùÞ»¸ù

- °ÁÛ³¿·´æ ùÛÞÛØù

- °ÁÖ±¾æ ù×ÌÁÐÎÑÙù

- °ÁÍ¿´æ ëððð

c) Update employees with the ×ÌÁÐÎÑÙ job by incrementing their salary by $2,000. What happens?

d) Update the salary to $9,000 for Û´»¿²±® Þ»¸.

Hint: Use an ËÐÜßÌÛ statement with a subquery in the ÉØÛÎÛ clause. What happens?

e) Change the job of Û´»¿²±® Þ»¸ to ÍÌÁÓßÒ using another ËÐÜßÌÛ statement with a subquery. What happens?

4) You are asked to prevent employees from being deleted during business hours.

a) Write a statement trigger called ÜÛÔÛÌÛÁÛÓÐÁÌÎÙ on the ÛÓÐÔÑÇÛÛÍ table to prevent rows from being deleted during weekday business hours, which are from 9:00 AM to 6:00 PM.

b) Attempt to delete employees with ÖÑÞÁ×Ü of ÍßÁÎÛÐ who are not assigned to a department.

Hint: This is employee Grant with ID 178.

Page 260: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 148

Practice Solutions 8-1: Creating Statement and Row Triggers

In this practice, you create statement and row triggers. You also create procedures that are invoked from within the triggers.

1) The rows in the ÖÑÞÍ table store a minimum and maximum salary allowed for different ÖÑÞÁ×Ü values. You are asked to write code to ensure that employees� salaries fall in the range allowed for their job type, for insert and update operations.

a) Create a procedure called ÝØÛÝÕÁÍßÔßÎÇ as follows:

i) The procedure accepts two parameters, one for an employee�s job ID string and the other for the salary.

ii) The procedure uses the job ID to determine the minimum and maximum salary for the specified job.

iii) If the salary parameter does not fall within the salary range of the job, inclusive of the minimum and maximum, then it should raise an application exception, with the message �ײª¿´·¼ ­¿´¿®§ ä­¿´âò Í¿´¿®·»­ º±® ¶±¾ 䶱¾·¼â ³«­¬ ¾» ¾»¬©»»² ä³·²â ¿²¼ 䳿¨â�. Replace the various items in the message with values supplied by parameters and variables populated by queries. Save the file.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðèÁðïÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ ½¸»½µÁ­¿´¿®§ ø°Á¬¸»Á¶±¾ ÊßÎÝØßÎîô °Á¬¸»Á­¿´¿®§ ÒËÓÞÛÎ÷ ×Í ªÁ³·²­¿´ ¶±¾­ò³·²Á­¿´¿®§û¬§°»å ªÁ³¿¨­¿´ ¶±¾­ò³¿¨Á­¿´¿®§û¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ³·²Á­¿´¿®§ô ³¿¨Á­¿´¿®§ ×ÒÌÑ ªÁ³·²­¿´ô ªÁ³¿¨­¿´ ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã ËÐÐÛÎø°Á¬¸»Á¶±¾÷å ×Ú °Á¬¸»Á­¿´¿®§ ÒÑÌ ÞÛÌÉÛÛÒ ªÁ³·²­¿´ ßÒÜ ªÁ³¿¨­¿´ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðïððô ùײª¿´·¼ ­¿´¿®§ üù ¤¤°Á¬¸»Á­¿´¿®§ ¤¤ùò ù¤¤ ùÍ¿´¿®·»­ º±® ¶±¾ ù¤¤ °Á¬¸»Á¶±¾ ¤¤ ù ³«­¬ ¾» ¾»¬©»»² üù¤¤ ªÁ³·²­¿´ ¤¤ù ¿²¼ üù ¤¤ ªÁ³¿¨­¿´÷å ÛÒÜ ×Úå ÛÒÜå ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 261: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 8-1: Creating Statement and Row Triggers (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 149

b) Create a trigger called ÝØÛÝÕÁÍßÔßÎÇÁÌÎÙ on the ÛÓÐÔÑÇÛÛÍ table that fires before an ×ÒÍÛÎÌ or ËÐÜßÌÛ operation on each row:

i) The trigger must call the ÝØÛÝÕÁÍßÔßÎÇ procedure to carry out the business logic.

ii) The trigger should pass the new job ID and salary to the procedure parameters.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðèÁðïÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÌÎ×ÙÙÛÎ ½¸»½µÁ­¿´¿®§Á¬®¹ ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ ÑÚ ¶±¾Á·¼ô ­¿´¿®§ ÑÒ »³°´±§»»­ ÚÑÎ ÛßÝØ ÎÑÉ ÞÛÙ×Ò ½¸»½µÁ­¿´¿®§øæ²»©ò¶±¾Á·¼ô æ²»©ò­¿´¿®§÷å ÛÒÜå ñ ÍØÑÉ ÛÎÎÑÎÍ

2) Test the ÝØÛÝÕÁÍßÔÁÌÎÙ trigger using the following cases:

a) Using your ÛÓÐÁÐÕÙòßÜÜÁÛÓÐÔÑÇÛÛ procedure, add employee Û´»¿²±® Þ»¸ to department 30. What happens and why?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðèÁðîÁ¿ò­¯´script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÛÈÛÝËÌÛ »³°Á°µ¹ò¿¼¼Á»³°´±§»»øùÛ´»¿²±®ùô ùÞ»¸ùô íð÷

Page 262: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 8-1: Creating Statement and Row Triggers (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 150

The trigger raises an exception because the ÛÓÐÁÐÕÙòßÜÜÁÛÓÐÔÑÇÛÛprocedure invokes an overloaded version of itself that uses the default salary of $1,000 and a default job ID of ÍßÁÎÛÐ. However, the ÖÑÞÍ table stores a minimum salary of $ 6,000 for the ÍßÁÎÛÐ type.

b) Update the salary of employee 115 to $2,000. In a separate update operation,

change the employee job ID to ØÎÁÎÛÐ. What happens in each case?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðèÁðîÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ËÐÜßÌÛ »³°´±§»»­ ÍÛÌ ­¿´¿®§ ã îððð ÉØÛÎÛ »³°´±§»»Á·¼ ã ïïëå

ËÐÜßÌÛ »³°´±§»»­ ÍÛÌ ¶±¾Á·¼ ã ùØÎÁÎÛÐù ÉØÛÎÛ »³°´±§»»Á·¼ ã ïïëå

Page 263: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 8-1: Creating Statement and Row Triggers (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 151

The first update statement fails to set the salary to $2,000. The check salary trigger rule fails the update operation because the new salary for employee 115 is less than the minimum allowed for the ÐËÁÝÔÛÎÕ job ID. The second update fails to change the employee�s job because the current employee�s salary of $3,100 is less than the minimum for the new ØÎÁÎÛÐ job ID.

c) Update the salary of employee 115 to $2,800. What happens?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðèÁðîÁ½ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ËÐÜßÌÛ »³°´±§»»­ ÍÛÌ ­¿´¿®§ ã îèðð ÉØÛÎÛ »³°´±§»»Á·¼ ã ïïëå

The update operation is successful because the new salary falls within the acceptable range for the current job ID.

3) Update the ÝØÛÝÕÁÍßÔßÎÇÁÌÎÙ trigger to fire only when the job ID or salary values have actually changed.

Page 264: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 8-1: Creating Statement and Row Triggers (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 152

a) Implement the business rule using a ÉØÛÒ clause to check whether the ÖÑÞÁ×Ü or ÍßÔßÎÇ values have changed.

Note: Make sure that the condition handles the ÒËÔÔ in the ÑÔÜò½±´«³²Á²¿³» values if an ×ÒÍÛÎÌ operation is performed; otherwise, an insert operation will fail.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðèÁðíÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÌÎ×ÙÙÛÎ ½¸»½µÁ­¿´¿®§Á¬®¹ ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ ÑÚ ¶±¾Á·¼ô ­¿´¿®§ ÑÒ »³°´±§»»­ ÚÑÎ ÛßÝØ ÎÑÉ ÉØÛÒ ø²»©ò¶±¾Á·¼ äâ ÒÊÔø±´¼ò¶±¾Á·¼ôùáù÷ ÑÎ ²»©ò­¿´¿®§ äâ ÒÊÔø±´¼ò­¿´¿®§ôð÷÷ ÞÛÙ×Ò ½¸»½µÁ­¿´¿®§øæ²»©ò¶±¾Á·¼ô æ²»©ò­¿´¿®§÷å ÛÒÜå ñ ÍØÑÉ ÛÎÎÑÎÍ

b) Test the trigger by executing the ÛÓÐÁÐÕÙòßÜÜÁÛÓÐÔÑÇÛÛ procedure with the following parameter values:

- °Áº·®­¬Á²¿³»æ ùÛ´»¿²±®ù

- °Á´¿­¬ ²¿³»æ ùÞ»¸ù

- °ÁÛ³¿·´æ ùÛÞÛØù

- °ÁÖ±¾æ ù×ÌÁÐÎÑÙù

- °ÁÍ¿´æ ëððð

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðèÁðíÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ÞÛÙ×Ò »³°Á°µ¹ò¿¼¼Á»³°´±§»»øùÛ´»¿²±®ùô ùÞ»¸ùô ùÛÞÛØùô ¶±¾ ãâ ù×ÌÁÐÎÑÙùô ­¿´ ãâ ëððð÷å ÛÒÜå ñ

Page 265: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 8-1: Creating Statement and Row Triggers (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 153

c) Update employees with the ×ÌÁÐÎÑÙ job by incrementing their salary by $2,000. What happens?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðèÁðíÁ½ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ËÐÜßÌÛ »³°´±§»»­ ÍÛÌ ­¿´¿®§ ã ­¿´¿®§ õ îððð ÉØÛÎÛ ¶±¾Á·¼ ã ù×ÌÁÐÎÑÙùå

An employee�s salary in the specified job type exceeds the maximum salary for that job type. No employee salaries in the ×ÌÁÐÎÑÙ job type are updated.

d) Update the salary to $9,000 for Û´»¿²±® Þ»¸.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðèÁðíÁ¼ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ËÐÜßÌÛ »³°´±§»»­ ÍÛÌ ­¿´¿®§ ã çððð ÉØÛÎÛ »³°´±§»»Á·¼ ã øÍÛÔÛÝÌ »³°´±§»»Á·¼ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã ùÞ»¸ù÷å

Page 266: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 8-1: Creating Statement and Row Triggers (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 154

Hint: Use an ËÐÜßÌÛ statement with a subquery in the ÉØÛÎÛ clause. What happens?

e) Change the job of Û´»¿²±® Þ»¸ to ÍÌÁÓßÒ using another ËÐÜßÌÛ statement with a subquery. What happens?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðèÁðíÁ»ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ËÐÜßÌÛ »³°´±§»»­ ­»¬ ¶±¾Á·¼ ã ùÍÌÁÓßÒù ÉØÛÎÛ »³°´±§»»Á·¼ ã øÍÛÔÛÝÌ »³°´±§»»Á·¼ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã ùÞ»¸ù÷å

The maximum salary of the new job type is less than the employee�s current salary; therefore, the update operation fails.

4) You are asked to prevent employees from being deleted during business hours.

a) Write a statement trigger called ÜÛÔÛÌÛÁÛÓÐÁÌÎÙ on the ÛÓÐÔÑÇÛÛÍ table to prevent rows from being deleted during weekday business hours, which are from 9:00 AM to 6:00 PM.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðèÁðìÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

Page 267: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 8-1: Creating Statement and Row Triggers (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 155

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÌÎ×ÙÙÛÎ ¼»´»¬»Á»³°Á¬®¹ ÞÛÚÑÎÛ ÜÛÔÛÌÛ ÑÒ »³°´±§»»­ ÜÛÝÔßÎÛ ¬¸»Á¼¿§ ÊßÎÝØßÎîøí÷ æã ÌÑÁÝØßÎøÍÇÍÜßÌÛô ùÜÇù÷å ¬¸»Á¸±«® ÐÔÍÁ×ÒÌÛÙÛÎ æã ÌÑÁÒËÓÞÛÎøÌÑÁÝØßÎøÍÇÍÜßÌÛô ùØØîìù÷÷å ÞÛÙ×Ò ×Ú ø¬¸»Á¸±«® ÞÛÌÉÛÛÒ ç ßÒÜ ïè÷ ßÒÜ ø¬¸»Á¼¿§ ÒÑÌ ×Ò øùÍßÌùôùÍËÒù÷÷ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðïëðô ùÛ³°´±§»» ®»½±®¼­ ½¿²²±¬ ¾» ¼»´»¬»¼ ¼«®·²¹ ¬¸» ¾«­·²»­­ ¸±«®­ ±º çßÓ ¿²¼ êÐÓù÷å ÛÒÜ ×Úå ÛÒÜå ñ ÍØÑÉ ÛÎÎÑÎÍ

b) Attempt to delete employees with ÖÑÞÁ×Ü of ÍßÁÎÛÐ who are not assigned to a department. Hint: This is employee Grant with ID 178. Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðèÁðìÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ÜÛÔÛÌÛ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ¶±¾Á·¼ ã ùÍßÁÎÛÐù ßÒÜ ¼»°¿®¬³»²¬Á·¼ ×Í ÒËÔÔå

Page 268: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 8-1: Creating Statement and Row Triggers (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 156

Note: Depending on the current time on your host machine in the classroom, you may or may not be able to perform the delete operations. For example, in the screen capture above, the delete operation failed as it was performed outside the allowed business hours (based on the host machine time).

Page 269: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 157

Practices and Solutions for Lesson 9

In this practice, you implement a simple business rule for ensuring data integrity of employees� salaries with respect to the valid salary range for their jobs. You create a trigger for this rule. During this process, your new triggers cause a cascading effect with triggers created in the practice section of the previous lesson. The cascading effect results in a mutating table exception on the ÖÑÞÍ table. You then create a PL/SQL package and additional triggers to solve the mutating table issue.

Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

Page 270: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 158

Practice 9-1: Managing Data Integrity Rules and Mutating Table Exceptions

In this practice, you implement a simple business rule for ensuring data integrity of employees� salaries with respect to the valid salary range for their jobs. You create a trigger for this rule. During this process, your new triggers cause a cascading effect with triggers created in the practice section of the previous lesson. The cascading effect results in a mutating table exception on the ÖÑÞÍ table. You then create a PL/SQL package and additional triggers to solve the mutating table issue. 1) Employees receive an automatic increase in salary if the minimum salary for a job is

increased to a value larger than their current salaries. Implement this requirement through a package procedure called by a trigger on the ÖÑÞÍ table. When you attempt to update the minimum salary in the ÖÑÞÍ table and try to update the employees� salaries, the ÝØÛÝÕÁÍßÔßÎÇ trigger attempts to read the ÖÑÞÍ table, which is subject to change, and you get a mutating table exception that is resolved by creating a new package and additional triggers.

a. Update your ÛÓÐÁÐÕÙ package (that you last updated in Practice 8) as follows:

i. Add a procedure called ÍÛÌÁÍßÔßÎÇ that updates the employees� salaries.

ii. The ÍÛÌÁÍßÔßÎÇ procedure accepts the following two parameters: The job ID for those salaries that may have to be updated, and the new minimum salary for the job ID

b. Create a row trigger named ËÐÜÁÓ×ÒÍßÔßÎÇÁÌÎÙ on the ÖÑÞÍ table that invokes the ÛÓÐÁÐÕÙòÍÛÌÁÍßÔßÎÇ procedure, when the minimum salary in the ÖÑÞÍ table is updated for a specified job ID.

c. Write a query to display the employee ID, last name, job ID, current salary, and minimum salary for employees who are programmers�that is, their ÖÑÞÁ×Ü is ù×ÌÁÐÎÑÙù. Then, update the minimum salary in the ÖÑÞÍ table to increase it by $1,000. What happens?

2) To resolve the mutating table issue, create a ÖÑÞÍÁÐÕÙ package to maintain in memory a copy of the rows in the ÖÑÞÍ table. Next, modify the ÝØÛÝÕÁÍßÔßÎÇ procedure to use the package data rather than issue a query on a table that is mutating to avoid the exception. However, you must create a ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ statement trigger on the ÛÓÐÔÑÇÛÛÍ table to initialize the ÖÑÞÍÁÐÕÙ package state before the ÝØÛÝÕÁÍßÔßÎÇ row trigger is fired.

a. Create a new package called ÖÑÞÍÁÐÕÙ with the following specification: ÐÎÑÝÛÜËÎÛ ·²·¬·¿´·¦»å ÚËÒÝÌ×ÑÒ ¹»¬Á³·²­¿´¿®§ø¶±¾·¼ ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÒËÓÞÛÎå ÚËÒÝÌ×ÑÒ ¹»¬Á³¿¨­¿´¿®§ø¶±¾·¼ ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÒËÓÞÛÎå

ÐÎÑÝÛÜËÎÛ ­»¬Á³·²­¿´¿®§ø¶±¾·¼ ÊßÎÝØßÎîô³·²Á­¿´¿®§ ÒËÓÞÛÎ÷å

Page 271: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (Continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 159

ÐÎÑÝÛÜËÎÛ ­»¬Á³¿¨­¿´¿®§ø¶±¾·¼ ÊßÎÝØßÎîô³¿¨Á­¿´¿®§ ÒËÓÞÛÎ÷å

b. Implement the body of ÖÑÞÍÁÐÕÙ as follows:

·ò Declare a private PL/SQL index-by table called ¶±¾­Á¬¿¾Á¬§°» that is indexed by a string type based on the ÖÑÞÍòÖÑÞÁ×ÜûÌÇÐÛ.

··ò Declare a private variable called ¶±¾­¬¿¾ based on the ¶±¾­Á¬¿¾Á¬§°».

iii. The ×Ò×Ì×ßÔ×ÆÛ procedure reads the rows in the ÖÑÞÍ table by using a cursor loop, and uses the ÖÑÞÁ×Ü value for the ¶±¾­¬¿¾ index that is assigned its corresponding row.

iv. The ÙÛÌÁÓ×ÒÍßÔßÎÇ function uses a °Á¶±¾·¼ parameter as an index to the ¶±¾­¬¿¾ and returns the ³·²Á­¿´¿®§ for that element.

v. The ÙÛÌÁÓßÈÍßÔßÎÇ function uses a °Á¶±¾·¼ parameter as an index to the ¶±¾­¬¿¾ and returns the ³¿¨Á­¿´¿®§ for that element.

vi. The ÍÛÌÁÓ×ÒÍßÔßÎÇ procedure uses its °Á¶±¾·¼ as an index to the ¶±¾­¬¿¾ to set the ³·²Á­¿´¿®§ field of its element to the value in the ³·²Á­¿´¿®§ parameter.

vii. The ÍÛÌÁÓßÈÍßÔßÎÇ procedure uses its °Á¶±¾·¼ as an index to the ¶±¾­¬¿¾ to set the ³¿¨Á­¿´¿®§ field of its element to the value in the ³¿¨Á­¿´¿®§ parameter.

c. Copy the ÝØÛÝÕÁÍßÔßÎÇ procedure from Practice 10, Exercise 1a, and modify the code by replacing the query on the ÖÑÞÍ table with statements to set the local ³·²­¿´ and ³¿¨­¿´ variables with values from the ÖÑÞÍÁÐÕÙ data by calling the appropriate ÙÛÌÁöÍßÔßÎÇ functions. This step should eliminate the mutating trigger exception.

d. Implement a ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ statement trigger called ×Ò×ÌÁÖÑÞÐÕÙÁÌÎÙ that uses the ÝßÔÔ syntax to invoke the ÖÑÞÍÁÐÕÙò×Ò×Ì×ßÔ×ÆÛ procedure to ensure that the package state is current before the DML operations are performed.

e. Test the code changes by executing the query to display the employees who are programmers, and then issue an update statement to increase the minimum salary of the ×ÌÁÐÎÑÙ job type by 1,000 in the ÖÑÞÍ table. Follow this up with a query on the employees with the ×ÌÁÐÎÑÙ job type to check the resulting changes. Which employees� salaries have been set to the minimum for their jobs?

3) Because the ÝØÛÝÕÁÍßÔßÎÇ procedure is fired by ÝØÛÝÕÁÍßÔßÎÇÁÌÎÙ before inserting or updating an employee, you must check whether this still works as expected.

Page 272: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (Continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 160

a. Test this by adding a new employee using ÛÓÐÁÐÕÙòßÜÜÁÛÓÐÔÑÇÛÛ with the following parameters: ø�ͬ»ª»�ô �Ó±®­»�ô �ÍÓÑÎÍÛ�ô ¿²¼ ­¿´ ãâ êëðð÷. What happens?

b. To correct the problem encountered when adding or updating an employee:

i. Create a ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ statement trigger called ÛÓÐÔÑÇÛÛÁ×Ò×ÌÖÑÞÍÁÌÎÙ on the ÛÓÐÔÑÇÛÛÍ table that calls the ÖÑÞÍÁÐÕÙò×Ò×Ì×ßÔ×ÆÛ procedure.

ii. Use the ÝßÔÔ syntax in the trigger body.

c. Test the trigger by adding employee Steve Morse again. Confirm the inserted record in the ÛÓÐÔÑÇÛÛÍ table by displaying the employee ID, first and last names, salary, job ID, and department ID.

Page 273: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 161

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions

In this practice, you implement a simple business rule for ensuring data integrity of employees� salaries with respect to the valid salary range for their jobs. You create a trigger for this rule. During this process, your new triggers cause a cascading effect with triggers created in the practice section of the previous lesson. The cascading effect results in a mutating table exception on the ÖÑÞÍ table. You then create a PL/SQL package and additional triggers to solve the mutating table issue. 1) Employees receive an automatic increase in salary if the minimum salary for a job is

increased to a value larger than their current salaries. Implement this requirement through a package procedure called by a trigger on the ÖÑÞÍ table. When you attempt to update the minimum salary in the ÖÑÞÍ table and try to update the employees� salaries, the ÝØÛÝÕÁÍßÔßÎÇ trigger attempts to read the ÖÑÞÍ table, which is subject to change, and you get a mutating table exception that is resolved by creating a new package and additional triggers.

a) Update your ÛÓÐÁÐÕÙ package (that you last updated in Practice 8) as follows:

i. Add a procedure called ÍÛÌÁÍßÔßÎÇ that updates the employees� salaries.

ii. The ÍÛÌÁÍßÔßÎÇ procedure accepts the following two parameters: The job ID for those salaries that may have to be updated, and the new minimum salary for the job ID

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðçÁðïÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown as follows. The newly added code is highlighted in bold letters in the following code box.

óó п½µ¿¹» ÍÐÛÝ×Ú×ÝßÌ×ÑÒ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°Á°µ¹ ×Í ÌÇÐÛ »³°Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ »³°´±§»»­ûÎÑÉÌÇÐÛå ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô °Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷å ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø

Page 274: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 162

°Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°»å ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»­ø°Á¼»°¬Á·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼û¬§°»÷å ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­å ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷å ÐÎÑÝÛÜËÎÛ ­¸±©Á»³°´±§»»­å ñö Ò»© ­»¬Á­¿´¿®§ °®±½»¼«®» öñ ÐÎÑÝÛÜËÎÛ ­»¬Á­¿´¿®§ø°Á¶±¾·¼ ÊßÎÝØßÎîô °Á³·²Á­¿´¿®§ ÒËÓÞÛÎ÷å ÛÒÜ »³°Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ óó п½µ¿¹» ÞÑÜÇ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°Á°µ¹ ×Í ÌÇÐÛ ¾±±´»¿²Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ÞÑÑÔÛßÒ ×ÒÜÛÈ ÞÇ Þ×ÒßÎÇÁ×ÒÌÛÙÛÎå

ª¿´·¼Á¼»°¿®¬³»²¬­ ¾±±´»¿²Á¬¿¾Á¬§°»å »³°Á¬¿¾´» »³°Á¬¿¾Á¬§°»å ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒå ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á»³¿·´ »³°´±§»»­ò»³¿·´ûÌÇÐÛô °Á¶±¾ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ùÍßÁÎÛÐùô

Page 275: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 163

°Á³¹® »³°´±§»»­ò³¿²¿¹»®Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ ïìëô °Á­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛ ÜÛÚßËÔÌ ïðððô °Á½±³³ »³°´±§»»­ò½±³³·­­·±²Á°½¬ûÌÇÐÛ ÜÛÚßËÔÌ ðô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ ÜÛÚßËÔÌ íð÷ ×Í ÐÎÑÝÛÜËÎÛ ¿«¼·¬Á²»©»³° ×Í ÐÎßÙÓß ßËÌÑÒÑÓÑËÍÁÌÎßÒÍßÝÌ×ÑÒå «­»®Á·¼ ÊßÎÝØßÎîøíð÷ æã ËÍÛÎå ÞÛÙ×Ò ×ÒÍÛÎÌ ×ÒÌÑ ´±¹Á²»©»³° ø»²¬®§Á·¼ô «­»®Á·¼ô ´±¹Á¬·³»ô ²¿³»÷ ÊßÔËÛÍ ø´±¹Á²»©»³°Á­»¯òÒÛÈÌÊßÔô «­»®Á·¼ô ­§­¼¿¬»ô°Áº·®­¬Á²¿³»¤¤ù ù¤¤°Á´¿­¬Á²¿³»÷å ÝÑÓÓ×Ìå ÛÒÜ ¿«¼·¬Á²»©»³°å ÞÛÙ×Ò óó ¿¼¼Á»³°´±§»» ×Ú ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼÷ ÌØÛÒ ¿«¼·¬Á²»©»³°å ×ÒÍÛÎÌ ×ÒÌÑ »³°´±§»»­ø»³°´±§»»Á·¼ô º·®­¬Á²¿³»ô ´¿­¬Á²¿³»ô »³¿·´ô ¶±¾Á·¼ô ³¿²¿¹»®Á·¼ô ¸·®»Á¼¿¬»ô ­¿´¿®§ô ½±³³·­­·±²Á°½¬ô ¼»°¿®¬³»²¬Á·¼÷ ÊßÔËÛÍ ø»³°´±§»»­Á­»¯òÒÛÈÌÊßÔô °Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¶±¾ô °Á³¹®ô ÌÎËÒÝøÍÇÍÜßÌÛ÷ô °Á­¿´ô °Á½±³³ô °Á¼»°¬·¼÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðîðìô ùײª¿´·¼ ¼»°¿®¬³»²¬ ×Üò Ì®§ ¿¹¿·²òù÷å ÛÒÜ ×Úå ÛÒÜ ¿¼¼Á»³°´±§»»å

ÐÎÑÝÛÜËÎÛ ¿¼¼Á»³°´±§»»ø °Áº·®­¬Á²¿³» »³°´±§»»­òº·®­¬Á²¿³»ûÌÇÐÛô °Á´¿­¬Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¼»°¬·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ×Í °Á»³¿·´ »³°´±§»»­ò»³¿·´û¬§°»å ÞÛÙ×Ò °Á»³¿·´ æã ËÐÐÛÎøÍËÞÍÌÎø°Áº·®­¬Á²¿³»ô ïô ï÷¤¤ÍËÞÍÌÎø°Á´¿­¬Á²¿³»ô ïô é÷÷å ¿¼¼Á»³°´±§»»ø°Áº·®­¬Á²¿³»ô °Á´¿­¬Á²¿³»ô °Á»³¿·´ô °Á¼»°¬·¼ ãâ °Á¼»°¬·¼÷å ÛÒÜå ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»ø °Á»³°·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á­¿´ ÑËÌ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛô °Á¶±¾ ÑËÌ »³°´±§»»­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ­¿´¿®§ô ¶±¾Á·¼

Page 276: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 164

×ÒÌÑ °Á­¿´ô °Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°·¼å ÛÒÜ ¹»¬Á»³°´±§»»å

ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Á»³°Á·¼ »³°´±§»»­ò»³°´±§»»Á·¼û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ÚËÒÝÌ×ÑÒ ¹»¬Á»³°´±§»»ø°Áº¿³·´§Á²¿³» »³°´±§»»­ò´¿­¬Á²¿³»û¬§°»÷ ®»¬«®² »³°´±§»»­û®±©¬§°» ×Í ®»½Á»³° »³°´±§»»­û®±©¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ö ×ÒÌÑ ®»½Á»³° ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ´¿­¬Á²¿³» ã °Áº¿³·´§Á²¿³»å ÎÛÌËÎÒ ®»½Á»³°å ÛÒÜå ÐÎÑÝÛÜËÎÛ ¹»¬Á»³°´±§»»­ø°Á¼»°¬Á·¼ »³°´±§»»­ò¼»°¿®¬³»²¬Á·¼û¬§°»÷ ×Í ÞÛÙ×Ò ÍÛÔÛÝÌ ö ÞËÔÕ ÝÑÔÔÛÝÌ ×ÒÌÑ »³°Á¬¿¾´» ÚÎÑÓ ÛÓÐÔÑÇÛÛÍ ÉØÛÎÛ ¼»°¿®¬³»²¬Á·¼ ã °Á¼»°¬Á·¼å ÛÒÜå ÐÎÑÝÛÜËÎÛ ·²·¬Á¼»°¿®¬³»²¬­ ×Í ÞÛÙ×Ò ÚÑÎ ®»½ ×Ò øÍÛÔÛÝÌ ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ ¼»°¿®¬³»²¬­÷ ÔÑÑÐ ª¿´·¼Á¼»°¿®¬³»²¬­ø®»½ò¼»°¿®¬³»²¬Á·¼÷ æã ÌÎËÛå ÛÒÜ ÔÑÑÐå ÛÒÜå ÐÎÑÝÛÜËÎÛ °®·²¬Á»³°´±§»»ø°Á®»½Á»³° »³°´±§»»­û®±©¬§°»÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø°Á®»½Á»³°ò¼»°¿®¬³»²¬Á·¼ ¤¤ù ù¤¤ °Á®»½Á»³°ò»³°´±§»»Á·¼¤¤ù ù¤¤ °Á®»½Á»³°òº·®­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò´¿­¬Á²¿³»¤¤ù ù¤¤ °Á®»½Á»³°ò¶±¾Á·¼¤¤ù ù¤¤ °Á®»½Á»³°ò­¿´¿®§÷å ÛÒÜå

Page 277: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 165

ÐÎÑÝÛÜËÎÛ ­¸±©Á»³°´±§»»­ ×Í ÞÛÙ×Ò ×Ú »³°Á¬¿¾´» ×Í ÒÑÌ ÒËÔÔ ÌØÛÒ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùÛ³°´±§»»­ ·² п½µ¿¹» ¬¿¾´»ù÷å ÚÑÎ · ×Ò ï òò »³°Á¬¿¾´»òÝÑËÒÌ ÔÑÑÐ °®·²¬Á»³°´±§»»ø»³°Á¬¿¾´»ø·÷÷å ÛÒÜ ÔÑÑÐå ÛÒÜ ×Úå ÛÒÜ ­¸±©Á»³°´±§»»­å

ÚËÒÝÌ×ÑÒ ª¿´·¼Á¼»°¬·¼ø°Á¼»°¬·¼ ×Ò ¼»°¿®¬³»²¬­ò¼»°¿®¬³»²¬Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÞÑÑÔÛßÒ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå ÞÛÙ×Ò ÎÛÌËÎÒ ª¿´·¼Á¼»°¿®¬³»²¬­ò»¨·­¬­ø°Á¼»°¬·¼÷å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÚßÔÍÛå ÛÒÜ ª¿´·¼Á¼»°¬·¼å

ñö Ò»© ­»¬Á­¿´¿®§ °®±½»¼«®» öñ ÐÎÑÝÛÜËÎÛ ­»¬Á­¿´¿®§ø°Á¶±¾·¼ ÊßÎÝØßÎîô °Á³·²Á­¿´¿®§ ÒËÓÞÛÎ÷ ×Í ÝËÎÍÑÎ ½«®Á»³° ×Í ÍÛÔÛÝÌ »³°´±§»»Á·¼ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ¶±¾Á·¼ ã °Á¶±¾·¼ ßÒÜ ­¿´¿®§ ä °Á³·²Á­¿´¿®§å ÞÛÙ×Ò ÚÑÎ ®»½Á»³° ×Ò ½«®Á»³° ÔÑÑÐ ËÐÜßÌÛ »³°´±§»»­ ÍÛÌ ­¿´¿®§ ã °Á³·²Á­¿´¿®§ ÉØÛÎÛ »³°´±§»»Á·¼ ã ®»½Á»³°ò»³°´±§»»Á·¼å ÛÒÜ ÔÑÑÐå ÛÒÜ ­»¬Á­¿´¿®§å

ÞÛÙ×Ò ·²·¬Á¼»°¿®¬³»²¬­å ÛÒÜ »³°Á°µ¹å

ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 278: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 166

b) Create a row trigger named ËÐÜÁÓ×ÒÍßÔßÎÇÁÌÎÙ on the ÖÑÞÍ table that invokes the ÛÓÐÁÐÕÙòÍÛÌÁÍßÔßÎÇ procedure, when the minimum salary in the ÖÑÞÍ table is updated for a specified job ID.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðçÁðïÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÌÎ×ÙÙÛÎ «°¼Á³·²­¿´¿®§Á¬®¹ ßÚÌÛÎ ËÐÜßÌÛ ÑÚ ³·²Á­¿´¿®§ ÑÒ ÖÑÞÍ ÚÑÎ ÛßÝØ ÎÑÉ ÞÛÙ×Ò »³°Á°µ¹ò­»¬Á­¿´¿®§øæ²»©ò¶±¾Á·¼ô æ²»©ò³·²Á­¿´¿®§÷å ÛÒÜå ñ ÍØÑÉ ÛÎÎÑÎÍ

c) Write a query to display the employee ID, last name, job ID, current salary, and minimum salary for employees who are programmers�that is, their ÖÑÞÁ×Ü is ù×ÌÁÐÎÑÙù. Then, update the minimum salary in the ÖÑÞÍ table to increase it by $1,000. What happens?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðçÁðïÁ½ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ÍÛÔÛÝÌ »³°´±§»»Á·¼ô ´¿­¬Á²¿³»ô ­¿´¿®§ ÚÎÑÓ »³°´±§»»­

ÉØÛÎÛ ¶±¾Á·¼ ã ù×ÌÁÐÎÑÙùå

ËÐÜßÌÛ ¶±¾­ ÍÛÌ ³·²Á­¿´¿®§ ã ³·²Á­¿´¿®§ õ ïððð

Page 279: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 167

ÉØÛÎÛ ¶±¾Á·¼ ã ù×ÌÁÐÎÑÙùå

The update of the ³·²Á­¿´¿®§ column for job ù×ÌÁÐÎÑÙù fails because the ËÐÜÁÓ×ÒÍßÔßÎÇÁÌÎÙ trigger on the ÖÑÞÍ table attempts to update the employees� salaries by calling the ÛÓÐÁÐÕÙòÍÛÌÁÍßÔßÎÇ procedure. The ÍÛÌÁÍßÔßÎÇ procedure causes the ÝØÛÝÕÁÍßÔßÎÇÁÌÎÙ trigger to fire (a cascading effect). The ÝØÛÝÕÁÍßÔßÎÇÁÌÎÙ calls the ÝØÛÝÕÁÍßÔßÎÇ procedure, which attempts to read the ÖÑÞÍ table data. While reading the ÖÑÞÍ table, the ÝØÛÝÕÁÍßÔßÎÇ procedure encounters the mutating table exception.

2) To resolve the mutating table issue, create a ÖÑÞÍÁÐÕÙ package to maintain in memory a copy of the rows in the ÖÑÞÍ table. Next, modify the ÝØÛÝÕÁÍßÔßÎÇ procedure to use the package data rather than issue a query on a table that is mutating to avoid the exception. However, you must create a ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ statement trigger on the ÛÓÐÔÑÇÛÛÍ table to initialize the ÖÑÞÍÁÐÕÙ package state before the ÝØÛÝÕÁÍßÔßÎÇ row trigger is fired.

a) Create a new package called ÖÑÞÍÁÐÕÙ with the following specification: ÐÎÑÝÛÜËÎÛ ·²·¬·¿´·¦»å ÚËÒÝÌ×ÑÒ ¹»¬Á³·²­¿´¿®§ø¶±¾·¼ ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÒËÓÞÛÎå

Page 280: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 168

ÚËÒÝÌ×ÑÒ ¹»¬Á³¿¨­¿´¿®§ø¶±¾·¼ ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÒËÓÞÛÎå ÐÎÑÝÛÜËÎÛ ­»¬Á³·²­¿´¿®§ø¶±¾·¼ ÊßÎÝØßÎîô³·²Á­¿´¿®§ ÒËÓÞÛÎ÷å

ÐÎÑÝÛÜËÎÛ ­»¬Á³¿¨­¿´¿®§ø¶±¾·¼ ÊßÎÝØßÎîô³¿¨Á­¿´¿®§ ÒËÓÞÛÎ÷å

Open the ­±´ÁðçÁðîÁ¿ò­¯´ file in the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ folder, or copy and paste the following code in the SQL Worksheet area. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ¶±¾­Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ·²·¬·¿´·¦»å ÚËÒÝÌ×ÑÒ ¹»¬Á³·²­¿´¿®§ø°Á¶±¾·¼ ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÒËÓÞÛÎå ÚËÒÝÌ×ÑÒ ¹»¬Á³¿¨­¿´¿®§ø°Á¶±¾·¼ ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÒËÓÞÛÎå ÐÎÑÝÛÜËÎÛ ­»¬Á³·²­¿´¿®§ø°Á¶±¾·¼ ÊßÎÝØßÎîô °Á³·²Á­¿´¿®§ ÒËÓÞÛÎ÷å ÐÎÑÝÛÜËÎÛ ­»¬Á³¿¨­¿´¿®§ø°Á¶±¾·¼ ÊßÎÝØßÎîô °Á³¿¨Á­¿´¿®§ ÒËÓÞÛÎ÷å ÛÒÜ ¶±¾­Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

b) Implement the body of ÖÑÞÍÁÐÕÙ as follows:

i. Declare a private PL/SQL index-by table called ¶±¾­Á¬¿¾Á¬§°» that is indexed by a string type based on the ÖÑÞÍòÖÑÞÁ×ÜûÌÇÐÛ.

··ò Declare a private variable called ¶±¾­¬¿¾ based on the ¶±¾­Á¬¿¾Á¬§°».

iii. The ×Ò×Ì×ßÔ×ÆÛ procedure reads the rows in the ÖÑÞÍ table by using a cursor loop, and uses the ÖÑÞÁ×Ü value for the ¶±¾­¬¿¾ index that is assigned its corresponding row.

iv. The ÙÛÌÁÓ×ÒÍßÔßÎÇ function uses a °Á¶±¾·¼ parameter as an index to the ¶±¾­¬¿¾ and returns the ³·²Á­¿´¿®§ for that element.

v. The ÙÛÌÁÓßÈÍßÔßÎÇ function uses a °Á¶±¾·¼ parameter as an index to the ¶±¾­¬¿¾ and returns the ³¿¨Á­¿´¿®§ for that element.

Page 281: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 169

vi. The ÍÛÌÁÓ×ÒÍßÔßÎÇ procedure uses its °Á¶±¾·¼ as an index to the ¶±¾­¬¿¾ to set the ³·²Á­¿´¿®§ field of its element to the value in the ³·²Á­¿´¿®§ parameter.

vii. The ÍÛÌÁÓßÈÍßÔßÎÇ procedure uses its °Á¶±¾·¼ as an index to the ¶±¾­¬¿¾ to set the ³¿¨Á­¿´¿®§ field of its element to the value in the ³¿¨Á­¿´¿®§ parameter.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðçÁðîÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. To compile the package�s body, right-click the package�s name or body in the Object Navigator tree, and then select Compile.

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ ¶±¾­Á°µ¹ ×Í ÌÇÐÛ ¶±¾­Á¬¿¾Á¬§°» ×Í ÌßÞÔÛ ÑÚ ¶±¾­û®±©¬§°» ×ÒÜÛÈ ÞÇ ¶±¾­ò¶±¾Á·¼û¬§°»å ¶±¾­¬¿¾ ¶±¾­Á¬¿¾Á¬§°»å

ÐÎÑÝÛÜËÎÛ ·²·¬·¿´·¦» ×Í ÞÛÙ×Ò ÚÑÎ ®»½Á¶±¾ ×Ò øÍÛÔÛÝÌ ö ÚÎÑÓ ¶±¾­÷ ÔÑÑÐ ¶±¾­¬¿¾ø®»½Á¶±¾ò¶±¾Á·¼÷ æã ®»½Á¶±¾å ÛÒÜ ÔÑÑÐå ÛÒÜ ·²·¬·¿´·¦»å

ÚËÒÝÌ×ÑÒ ¹»¬Á³·²­¿´¿®§ø°Á¶±¾·¼ ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÒËÓÞÛÎ ×Í ÞÛÙ×Ò ÎÛÌËÎÒ ¶±¾­¬¿¾ø°Á¶±¾·¼÷ò³·²Á­¿´¿®§å ÛÒÜ ¹»¬Á³·²­¿´¿®§å

ÚËÒÝÌ×ÑÒ ¹»¬Á³¿¨­¿´¿®§ø°Á¶±¾·¼ ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÒËÓÞÛÎ ×Í ÞÛÙ×Ò ÎÛÌËÎÒ ¶±¾­¬¿¾ø°Á¶±¾·¼÷ò³¿¨Á­¿´¿®§å ÛÒÜ ¹»¬Á³¿¨­¿´¿®§å

ÐÎÑÝÛÜËÎÛ ­»¬Á³·²­¿´¿®§ø°Á¶±¾·¼ ÊßÎÝØßÎîô °Á³·²Á­¿´¿®§ ÒËÓÞÛÎ÷ ×Í ÞÛÙ×Ò ¶±¾­¬¿¾ø°Á¶±¾·¼÷ò³¿¨Á­¿´¿®§ æã °Á³·²Á­¿´¿®§å ÛÒÜ ­»¬Á³·²­¿´¿®§å

ÐÎÑÝÛÜËÎÛ ­»¬Á³¿¨­¿´¿®§ø°Á¶±¾·¼ ÊßÎÝØßÎîô °Á³¿¨Á­¿´¿®§ ÒËÓÞÛÎ÷ ×Í ÞÛÙ×Ò ¶±¾­¬¿¾ø°Á¶±¾·¼÷ò³¿¨Á­¿´¿®§ æã °Á³¿¨Á­¿´¿®§å ÛÒÜ ­»¬Á³¿¨­¿´¿®§å

ÛÒÜ ¶±¾­Á°µ¹å

Page 282: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 170

ñ ÍØÑÉ ÛÎÎÑÎÍ

c) Copy the ÝØÛÝÕÁÍßÔßÎÇ procedure from Practice 8, Exercise 1a, and modify the code by replacing the query on the ÖÑÞÍ table with statements to set the local ³·²­¿´ and ³¿¨­¿´ variables with values from the ÖÑÞÍÁÐÕÙ data by calling the appropriate ÙÛÌÁöÍßÔßÎÇ functions. This step should eliminate the mutating trigger exception.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðçÁðîÁ½ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ ½¸»½µÁ­¿´¿®§ ø°Á¬¸»Á¶±¾ ÊßÎÝØßÎîô °Á¬¸»Á­¿´¿®§ ÒËÓÞÛÎ÷ ×Í ªÁ³·²­¿´ ¶±¾­ò³·²Á­¿´¿®§û¬§°»å ªÁ³¿¨­¿´ ¶±¾­ò³¿¨Á­¿´¿®§û¬§°»å ÞÛÙ×Ò ñö öö ݱ³³»²¬»¼ ±«¬ ¬± ¿ª±·¼ ³«¬¿¬·²¹ ¬®·¹¹»® »¨½»°¬·±² ±² ¬¸» ÖÑÞÍ ¬¿¾´» ÍÛÔÛÝÌ ³·²Á­¿´¿®§ô ³¿¨Á­¿´¿®§ ×ÒÌÑ ªÁ³·²­¿´ô ªÁ³¿¨­¿´ ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã ËÐÐÛÎø°Á¬¸»Á¶±¾÷å öñ

ªÁ³·²­¿´ æã ¶±¾­Á°µ¹ò¹»¬Á³·²­¿´¿®§øËÐÐÛÎø°Á¬¸»Á¶±¾÷÷å ªÁ³¿¨­¿´ æã ¶±¾­Á°µ¹ò¹»¬Á³¿¨­¿´¿®§øËÐÐÛÎø°Á¬¸»Á¶±¾÷÷å ×Ú °Á¬¸»Á­¿´¿®§ ÒÑÌ ÞÛÌÉÛÛÒ ªÁ³·²­¿´ ßÒÜ ªÁ³¿¨­¿´ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðïððô ùײª¿´·¼ ­¿´¿®§ üù¤¤°Á¬¸»Á­¿´¿®§¤¤ùò ù¤¤ ùÍ¿´¿®·»­ º±® ¶±¾ ù¤¤ °Á¬¸»Á¶±¾ ¤¤ ù ³«­¬ ¾» ¾»¬©»»² üù¤¤ ªÁ³·²­¿´ ¤¤ù ¿²¼ üù ¤¤ ªÁ³¿¨­¿´÷å ÛÒÜ ×Úå ÛÒÜå ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 283: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 171

d) Implement a ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ statement trigger called ×Ò×ÌÁÖÑÞÐÕÙÁÌÎÙ that uses the ÝßÔÔ syntax to invoke the ÖÑÞÍÁÐÕÙò×Ò×Ì×ßÔ×ÆÛ procedure to ensure that the package state is current before the DML operations are performed.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðçÁðîÁ¼ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÌÎ×ÙÙÛÎ ·²·¬Á¶±¾°µ¹Á¬®¹ ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ ÑÒ ¶±¾­ ÝßÔÔ ¶±¾­Á°µ¹ò·²·¬·¿´·¦» ñ ÍØÑÉ ÛÎÎÑÎÍ

e) Test the code changes by executing the query to display the employees who are programmers, and then issue an update statement to increase the minimum salary of the ×ÌÁÐÎÑÙ job type by 1,000 in the ÖÑÞÍ table. Follow this up with a query on the employees with the ×ÌÁÐÎÑÙ job type to check the resulting changes. Which employees� salaries have been set to the minimum for their jobs?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðçÁðîÁ»ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÍÛÔÛÝÌ »³°´±§»»Á·¼ô ´¿­¬Á²¿³»ô ­¿´¿®§ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ¶±¾Á·¼ ã ù×ÌÁÐÎÑÙùå

ËÐÜßÌÛ ¶±¾­ ÍÛÌ ³·²Á­¿´¿®§ ã ³·²Á­¿´¿®§ õ ïððð ÉØÛÎÛ ¶±¾Á·¼ ã ù×ÌÁÐÎÑÙùå

ÍÛÔÛÝÌ »³°´±§»»Á·¼ô ´¿­¬Á²¿³»ô ­¿´¿®§ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ¶±¾Á·¼ ã ù×ÌÁÐÎÑÙùå

Page 284: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 172

The employees with last names ß«­¬·², п¬¿¾¿´´¿, and Ô±®»²¬¦ have all had their salaries updated. No exception occurred during this process, and you implemented a solution for the mutating table trigger exception.

3) Because the ÝØÛÝÕÁÍßÔßÎÇ procedure is fired by ÝØÛÝÕÁÍßÔßÎÇÁÌÎÙ before inserting or updating an employee, you must check whether this still works as expected.

a) Test this by adding a new employee using ÛÓÐÁÐÕÙòßÜÜÁÛÓÐÔÑÇÛÛ with the following parameters: ø�ͬ»ª»�ô �Ó±®­»�ô �ÍÓÑÎÍÛ�ô ¿²¼ ­¿´ ãâ êëðð÷. What happens?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðçÁðíÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÛÈÛÝËÌÛ »³°Á°µ¹ò¿¼¼Á»³°´±§»»øùͬ»ª»ùô ùÓ±®­»ùô ùÍÓÑÎÍÛùô °Á­¿´ ãâ êëðð÷

Page 285: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 9-1: Managing Data Integrity Rules and Mutating Table Exceptions (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 173

b) To correct the problem encountered when adding or updating an employee:

i. Create a ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ statement trigger called ÛÓÐÔÑÇÛÛÁ×Ò×ÌÖÑÞÍÁÌÎÙ on the ÛÓÐÔÑÇÛÛÍ table that calls the ÖÑÞÍÁÐÕÙò×Ò×Ì×ßÔ×ÆÛ procedure.

ii. Use the ÝßÔÔ syntax in the trigger body.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðçÁðíÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÝÎÛßÌÛ ÌÎ×ÙÙÛÎ »³°´±§»»Á·²·¬¶±¾­Á¬®¹ ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ ÑÚ ¶±¾Á·¼ô ­¿´¿®§ ÑÒ »³°´±§»»­ ÝßÔÔ ¶±¾­Á°µ¹ò·²·¬·¿´·¦» ñ

c) Test the trigger by adding employee Steve Morse again. Confirm the inserted

record in the ÛÓÐÔÑÇÛÛÍ table by displaying the employee ID, first and last names, salary, job ID, and department ID.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁðçÁðíÁ½ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

Page 286: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 174

Practices and Solutions for Lesson 10

In this practice, you display the compiler initialization parameters. You then enable native compilation for your session and compile a procedure. You then suppress all compiler-warning categories and then restore the original session-warning settings. Finally, you identify the categories for some compiler-warning message numbers. Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

Page 287: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 175

Practice 10-1: Using the PL/SQL Compiler Parameters and Warnings In this practice, you display the compiler initialization parameters. You then enable native compilation for your session and compile a procedure. You then suppress all compiler-warning categories and then restore the original session-warning settings. Finally, you identify the categories for some compiler-warning message numbers.

1) Create and run a ´¿¾ÁïðÁðï script to display the following information about compiler-initialization parameters by using the ËÍÛÎÁÐÔÍÏÔÁÑÞÖÛÝÌÁÍÛÌÌ×ÒÙÍ data dictionary view. Note the settings for the ßÜÜÁÖÑÞÁØ×ÍÌÑÎÇ object. Note: Use the Execute Statement (F9) icon to display the results in the Results tab.

a) Object name b) Object type

c) The object�s compilation mode d) The compilation optimization level

2) Alter the ÐÔÍÏÔÁÝÑÜÛÁÌÇÐÛ parameter to enable native compilation for your session, and compile ßÜÜÁÖÑÞÁØ×ÍÌÑÎÇ.

a) Execute the ßÔÌÛÎ ÍÛÍÍ×ÑÒ command to enable native compilation for the session.

b) Compile the ßÜÜÁÖÑÞÁØ×ÍÌÑÎÇ procedure.

c) Rerun the ­±´ÁïðÁðï script. Note the ÐÔÍÏÔÁÝÑÜÛÁÌÇÐÛ parameter.

d) Switch compilation to use interpreted compilation mode as follows: 3) Use the Tools > Preferences > PL/SQL Compiler Options region to disable all

compiler warnings categories.

4) Edit, examine, and execute the ´¿¾ÁïðÁðìò­¯´ script to create the ËÒÎÛßÝØßÞÔÛÁÝÑÜÛ procedure. Click the Run Script icon (F5) to create the procedure. Use the procedure name in the Navigation tree to compile the procedure.

5) What are the compiler warnings that are displayed in the Compiler � Log tab, if any? 6) Enable all compiler-warning messages for this session using the Preferences window.

7) Recompile the ËÒÎÛßÝØßÞÔÛÁÝÑÜÛ procedure using the Object Navigation tree. What compiler warnings are displayed, if any?

8) Use the ËÍÛÎÁÛÎÎÑÎÍ data dictionary view to display the compiler-warning messages details as follows.

9) Create a script named ©¿®²·²¹Á³­¹­ that uses the ÛÈÛÝËÌÛ ÜÞÓÍÁÑËÌÐËÌ and the ÜÞÓÍÁÉßÎÒ×ÒÙ packages to identify the categories for the following compiler-warning message numbers: 5050, 6075, and 7100. Enable ÍÛÎÊÛÎÑËÌÐËÌ before running the script.

Page 288: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 176

Practice Solutions 10-1: Using the PL/SQL Compiler Parameters and Warnings

In this practice, you display the compiler initialization parameters. You then enable native compilation for your session and compile a procedure. You then suppress all compiler-warning categories and then restore the original session-warning settings. Finally, you identify the categories for some compiler-warning message numbers.

1) Create and run a ´¿¾ÁïðÁðï script to display the following information about compiler-initialization parameters by using the ËÍÛÎÁÐÔÍÏÔÁÑÞÖÛÝÌÁÍÛÌÌ×ÒÙÍ data dictionary view. Note the settings for the ßÜÜÁÖÑÞÁØ×ÍÌÑÎÇ object. Note: Use the Execute Statement (F9) icon to display the results in the Results tab. a) Object name

b) Object type c) The object�s compilation mode

d) The compilation optimization level

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïðÁðïò­¯´ script. Click the Execute Statement (F9) icon on the SQL Worksheet toolbar to run the query. The code and a sample of the results are shown below. ÍÛÔÛÝÌ ²¿³»ô ¬§°»ô°´­¯´Á½±¼»Á¬§°» ¿­ ½±¼»Á¬§°»ô °´­¯´Á±°¬·³·¦»Á´»ª»´ ¿­ ±°¬Á´ª´ ÚÎÑÓ «­»®Á°´­¯´Á±¾¶»½¬Á­»¬¬·²¹­å

. . .

Page 289: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 10-1: Using the PL/SQL Compiler Parameters and Warnings (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 177

2) Alter the ÐÔÍÏÔÁÝÑÜÛÁÌÇÐÛ parameter to enable native compilation for your session, and compile ßÜÜÁÖÑÞÁØ×ÍÌÑÎÇ.

a) Execute the ßÔÌÛÎ ÍÛÍÍ×ÑÒ command to enable native compilation for the session.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïðÁðîÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the query. The code and the results are shown below.

ßÔÌÛÎ ÍÛÍÍ×ÑÒ ÍÛÌ ÐÔÍÏÔÁÝÑÜÛÁÌÇÐÛ ã ùÒßÌ×ÊÛùå

b) Compile the ßÜÜÁÖÑÞÁØ×ÍÌÑÎÇ procedure.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïðÁðîÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the query. The code and the results are shown below. ßÔÌÛÎ ÐÎÑÝÛÜËÎÛ ¿¼¼Á¶±¾Á¸·­¬±®§ ÝÑÓÐ×ÔÛå

c) Rerun the ­±´ÁïðÁðïò­¯´ script. Note the ÐÔÍÏÔÁÝÑÜÛÁÌÇÐÛ parameter.

ÍÛÔÛÝÌ ²¿³»ô ¬§°»ô °´­¯´Á½±¼»Á¬§°» ¿­ ½±¼»Á¬§°»ô °´­¯´Á±°¬·³·¦»Á´»ª»´ ¿­ ±°¬Á´ª´ ÚÎÑÓ «­»®Á°´­¯´Á±¾¶»½¬Á­»¬¬·²¹­å

. . .

Page 290: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 10-1: Using the PL/SQL Compiler Parameters and Warnings (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 178

d) Switch compilation to use interpreted compilation mode as follows:

ßÔÌÛÎ ÍÛÍÍ×ÑÒ ÍÛÌ ÐÔÍÏÔÁÝÑÜÛÁÌÇÐÛ ã ù×ÒÌÛÎÐÎÛÌÛÜùå

3) Use the Tools > Preferences > PL/SQL Compiler Options region to disable all compiler warnings categories.

Select DISABLE for all four PL/SQL compiler warnings categories, and then click OK.

4) Edit, examine, and execute the ´¿¾ÁïðÁðìò­¯´ script to create the ËÒÎÛßÝØßÞÔÛÁÝÑÜÛ procedure. Click the Run Script icon (F5) to create and compile the procedure.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïðÁðìò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the query. The code and the results are shown below.

Page 291: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 10-1: Using the PL/SQL Compiler Parameters and Warnings (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 179

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ «²®»¿½¸¿¾´»Á½±¼» ßÍ ½Á¨ ÝÑÒÍÌßÒÌ ÞÑÑÔÛßÒ æã ÌÎËÛå ÞÛÙ×Ò ×Ú ½Á¨ ÌØÛÒ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùÌÎËÛù÷å ÛÔÍÛ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùÚßÔÍÛù÷å ÛÒÜ ×Úå ÛÒÜ «²®»¿½¸¿¾´»Á½±¼»å ñ

To view any compiler warning errors, right-click the procedure�s name in the Procedures node in the Navigation tree, and then click Compile.

Note   If the procedure is not displayed in the Navigation tree, click the Refresh icon in

the Connections tab.

Page 292: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 10-1: Using the PL/SQL Compiler Parameters and Warnings (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 180

  Make sure your Messages � Log tab is displayed (select View > Log from the menu bar).

5) What are the compiler warnings that are displayed in the Compiler � Log tab, if any?

Note that the message in the Messages � Log tab is �ËÒÎÛßÝØßÞÔÛÁÝÑÜÛ Compiled� without any warning messages because you disabled the compiler warnings in step 3.

6) Enable all compiler-warning messages for this session using the Preferences window.

Select ENABLE for all four PL/SQL compiler warnings, and then click OK.

7) Recompile the ËÒÎÛßÝØßÞÔÛÁÝÑÜÛ procedure using the Object Navigation tree. What compiler warnings are displayed, if any?

Right-click the procedure�s name in the Object Navigation tree, and then select Compile. Note the messages displayed in the Messages and Compiler subtabs in the Compiler � Log tab.

Page 293: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 10-1: Using the PL/SQL Compiler Parameters and Warnings (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 181

Note: If you get the following two warnings in SQL Developer, it is expected in some versions of SQL Developer. If you do get the following warnings, it is because your version of SQL Developer still uses the Oracle 11g database deprecated ÐÔÍÏÔÁÜÛÞËÙ parameter. É¿®²·²¹øï÷æÐÔÉóðêðïëæ°¿®¿³»¬»® ÐÔÍÏÔÁÜÛÞËÙ ·­ ¼»°®»½¿¬»¼ å «­» ÐÔÍÏÔÁÑÐÌ×Ó×ÆÛÁÔÛÊÛÔãï É¿®²·²¹øï÷æÐÔÉóðêðïíæ¼»°®»½¿¬»¼ °¿®¿³»¬»® ÐÔÍÏÔÁÜÛÞËÙ º±®½»­ ÐÔÍÏÔÁÑÐÌ×Ó×ÆÛÁÔÛÊÛÔäãï

8) Use the ËÍÛÎÁÛÎÎÑÎÍ data dictionary view to display the compiler-warning messages details as follows.

ÜÛÍÝÎ×ÞÛ «­»®Á»®®±®­

ÍÛÔÛÝÌ ö ÚÎÑÓ «­»®Á»®®±®­å

. . .

Page 294: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 10-1: Using the PL/SQL Compiler Parameters and Warnings (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 182

Note: The output was displayed in the Results tab because we used the F9 key to execute the ÍÛÔÛÝÌ statement. The results of the ÍÛÔÛÝÌ statement might be different depending on the amount of errors you had in your session.

9) Create a script named ©¿®²·²¹Á³­¹­ that uses the ÛÈÛÝËÌÛ ÜÞÓÍÁÑËÌÐËÌ and the ÜÞÓÍÁÉßÎÒ×ÒÙ packages to identify the categories for the following compiler-warning message numbers: 5050, 6075, and 7100. Enable ÍÛÎÊÛÎÑËÌÐËÌ before running the script.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïðÁðçò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the query. The code and the results are shown below. ÛÈÛÝËÌÛ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøÜÞÓÍÁÉßÎÒ×ÒÙòÙÛÌÁÝßÌÛÙÑÎÇøú³»­­¿¹»÷÷å

ÛÈÛÝËÌÛ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøÜÞÓÍÁÉßÎÒ×ÒÙòÙÛÌÁÝßÌÛÙÑÎÇøú³»­­¿¹»÷÷å

Page 295: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 10-1: Using the PL/SQL Compiler Parameters and Warnings (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 183

ÛÈÛÝËÌÛ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøÜÞÓÍÁÉßÎÒ×ÒÙòÙÛÌÁÝßÌÛÙÑÎÇøú³»­­¿¹»÷÷å

Page 296: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 10-1: Using the PL/SQL Compiler Parameters and Warnings (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 184

Page 297: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 185

Practices and Solutions for Lesson 11

In this practice, you create a package and a procedure that use conditional compilation. In addition, you use the appropriate package to retrieve the post-processed source text of the PL/SQL unit. You also obfuscate some PL/SQL code. Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

Page 298: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 186

Practice 11-1: Using Conditional Compilation

In this practice, you create a package and a procedure that use conditional compilation. In addition, you use the appropriate package to retrieve the postprocessed source text of the PL/SQL unit. You also obfuscate some PL/SQL code.

1) Examine and then execute the ´¿¾ÁïïÁðïò­¯´ script. This script sets flags for displaying debugging code and tracing information. The script also creates the ³§Á°µ¹ package and the ½·®½´»Á¿®»¿ procedure.

2) Use the ÜÞÓÍÁÐÎÛÐÎÑÝÛÍÍÑÎ subprogram to retrieve the postprocessed source text of the PL/SQL unit after processing the conditional compilation directives from ´¿¾ÁïïÁðï. Enable ÍÛÎÊÛÎÑËÌÐËÌ.

3) Create a PL/SQL script that uses the ÜÞÓÍÁÜÞÁÊÛÎÍ×ÑÒ constant with conditional compilation. The code should test for the Oracle database version: a) If the database version is less than or equal to 10.1, it should display the following

error message: ˲­«°°±®¬»¼ ¼¿¬¿¾¿­» ®»´»¿­»ò

b) If the database version is 11.1 or higher, it should display the following message: λ´»¿­» ïïòï ·­ ­«°°±®¬»¼ò

4) Consider the following code in the ´¿¾ÁïïÁðìò­¯´ script that uses ÝÎÛßÌÛÁÉÎßÐÐÛÜ to dynamically create and wrap a package specification and a package body in a database. Edit the ´¿¾ÁïïÁðìò­¯´ script to add the needed code to obfuscate the PL/SQL code. Save and then execute the script.

ÜÛÝÔßÎÛ óó ¬¸» °¿½µ¿¹»Á¬»¨¬ ª¿®·¿¾´» ½±²¬¿·²­ ¬¸» ¬»¨¬ ¬± ½®»¿¬» óó ¬¸» °¿½µ¿¹» ­°»½ ¿²¼ ¾±¼§ °¿½µ¿¹»Á¬»¨¬ ÊßÎÝØßÎîøíîéêé÷å ÚËÒÝÌ×ÑÒ ¹»²»®¿¬»Á­°»½ ø°µ¹²¿³» ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÊßÎÝØßÎî ßÍ ÞÛÙ×Ò ÎÛÌËÎÒ ùÝÎÛßÌÛ ÐßÝÕßÙÛ ù ¤¤ °µ¹²¿³» ¤¤ ù ßÍ ÐÎÑÝÛÜËÎÛ ®¿·­»Á­¿´¿®§ ø»³°Á·¼ ÒËÓÞÛÎô ¿³±«²¬ ÒËÓÞÛÎ÷å ÐÎÑÝÛÜËÎÛ º·®»Á»³°´±§»» ø»³°Á·¼ ÒËÓÞÛÎ÷å ÛÒÜ ù ¤¤ °µ¹²¿³» ¤¤ ùåùå ÛÒÜ ¹»²»®¿¬»Á­°»½å ÚËÒÝÌ×ÑÒ ¹»²»®¿¬»Á¾±¼§ ø°µ¹²¿³» ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÊßÎÝØßÎî ßÍ ÞÛÙ×Ò ÎÛÌËÎÒ ùÝÎÛßÌÛ ÐßÝÕßÙÛ ÞÑÜÇ ù ¤¤ °µ¹²¿³» ¤¤ ù ßÍ ÐÎÑÝÛÜËÎÛ ®¿·­»Á­¿´¿®§ ø»³°Á·¼ ÒËÓÞÛÎô ¿³±«²¬ ÒËÓÞÛÎ÷ ×Í ÞÛÙ×Ò ËÐÜßÌÛ »³°´±§»»­ ÍÛÌ ­¿´¿®§ ã ­¿´¿®§ õ ¿³±«²¬ ÉØÛÎÛ »³°´±§»»Á·¼ ã »³°Á·¼å ÛÒÜ ®¿·­»Á­¿´¿®§å

Page 299: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 11-1: Using Conditional Compilation (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 187

ÐÎÑÝÛÜËÎÛ º·®»Á»³°´±§»» ø»³°Á·¼ ÒËÓÞÛÎ÷ ×Í ÞÛÙ×Ò ÜÛÔÛÌÛ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã »³°Á·¼å ÛÒÜ º·®»Á»³°´±§»»å ÛÒÜ ù ¤¤ °µ¹²¿³» ¤¤ ùåùå ÛÒÜ ¹»²»®¿¬»Á¾±¼§å

a) Generate the package specification while passing the »³°Á¿½¬·±²­ parameter.

b) Create and wrap the package specification. c) Generate the package body.

d) Create and wrap the package body. e) Call a procedure from the wrapped package as follows:

ÝßÔÔ »³°Á¿½¬·±²­ò®¿·­»Á­¿´¿®§øïîðô ïðð÷å

f) Use the ËÍÛÎÁÍÑËÎÝÛ data dictionary view to verify that the code is hidden as follows: ÍÛÔÛÝÌ ¬»¨¬ ÚÎÑÓ ËÍÛÎÁÍÑËÎÝÛ ÉØÛÎÛ ²¿³» ã ùÛÓÐÁßÝÌ×ÑÒÍùå

Page 300: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 188

Practice Solutions 11-1: Using Conditional Compilation

In this practice, you create a package and a procedure that use conditional compilation. In addition, you use the appropriate package to retrieve the postprocessed source text of the PL/SQL unit. You also obfuscate some PL/SQL code.

1) Examine and then execute the ´¿¾ÁïïÁðïò­¯´ script. This script sets flags for displaying debugging code and tracing information. The script also creates the ³§Á°µ¹ package and the ½·®½´»Á¿®»¿ procedure.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïïÁðïò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ßÔÌÛÎ ÍÛÍÍ×ÑÒ ÍÛÌ ÐÔÍÏÔÁÝÝÚÔßÙÍ ã ù³§Á¼»¾«¹æÚßÔÍÛô ³§Á¬®¿½·²¹æÚßÔÍÛùå ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ³§Á°µ¹ ßÍ ÍËÞÌÇÐÛ ³§Á®»¿´ ×Í ü×Ú ÜÞÓÍÁÜÞÁÊÛÎÍ×ÑÒòÊÛÎÍ×ÑÒ ä ïð üÌØÛÒ ÒËÓÞÛÎå óó ½¸»½µ ¼¿¬¿¾¿­» ª»®­·±² üÛÔÍÛ Þ×ÒßÎÇÁÜÑËÞÔÛå üÛÒÜ ³§Á°· ³§Á®»¿´å ³§Á» ³§Á®»¿´å ÛÒÜ ³§Á°µ¹å ñ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ ³§Á°µ¹ ßÍ ÞÛÙ×Ò ü×Ú ÜÞÓÍÁÜÞÁÊÛÎÍ×ÑÒòÊÛÎÍ×ÑÒ ä ïð üÌØÛÒ ³§Á°· æã íòïìðïêìðèîèçððèîçîìíïçìððîéíìíêêêèêíîîéå ³§Á» æã îòéïèîèïèîèìëçðìëîíëíêðîèéìéïíëîêêîìçééëå üÛÔÍÛ ³§Á°· æã íòïìðïêìðèîèçððèîçîìíïçìððîéíìíêêêèêíîîé¼å ³§Á» æã îòéïèîèïèîèìëçðìëîíëíêðîèéìéïíëîêêîìçééë¼å üÛÒÜ ÛÒÜ ³§Á°µ¹å ñ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ ½·®½´»Á¿®»¿ø®¿¼·«­ ³§Á°µ¹ò³§Á®»¿´÷ ×Í ³§Á¿®»¿ ³§Á°µ¹ò³§Á®»¿´å ³§Á¼¿¬¿¬§°» ÊßÎÝØßÎîøíð÷å ÞÛÙ×Ò ³§Á¿®»¿ æã ³§Á°µ¹ò³§Á°· ö ®¿¼·«­ ö ®¿¼·«­å ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùο¼·«­æ ù ¤¤ ÌÑÁÝØßÎø®¿¼·«­÷ ¤¤ ù ß®»¿æ ù ¤¤ ÌÑÁÝØßÎø³§Á¿®»¿÷ ÷å ü×Ú üü³§Á¼»¾«¹ üÌØÛÒ óó ·º ³§Á¼»¾«¹ ·­ ÌÎËÛô ®«² ­±³» ¼»¾«¹¹·²¹ ½±¼»

Page 301: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 11-1: Using Conditional Compilation (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 189

ÍÛÔÛÝÌ ÜßÌßÁÌÇÐÛ ×ÒÌÑ ³§Á¼¿¬¿¬§°» ÚÎÑÓ ËÍÛÎÁßÎÙËÓÛÒÌÍ ÉØÛÎÛ ÑÞÖÛÝÌÁÒßÓÛ ã ùÝ×ÎÝÔÛÁßÎÛßù ßÒÜ ßÎÙËÓÛÒÌÁÒßÓÛ ã ùÎßÜ×ËÍùå ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùÜ¿¬¿¬§°» ±º ¬¸» ÎßÜ×ËÍ ¿®¹«³»²¬ ·­æ ù ¤¤ ³§Á¼¿¬¿¬§°»÷å üÛÒÜ ÛÒÜå ñ

2) Use the ÜÞÓÍÁÐÎÛÐÎÑÝÛÍÍÑÎ subprogram to retrieve the postprocessed source text of the PL/SQL unit after processing the conditional compilation directives from ´¿¾ÁïïÁðï. Enable ÍÛÎÊÛÎÑËÌÐËÌ.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïïÁðîò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. óó ̸» ½±¼» »¨¿³°´» ¿­­«³»­ §±« ¿®» ¬¸» ­¬«¼»²¬ ©·¬¸ ¬¸» óó ¿½½±«²¬ ±®¿éðò Í«¾­¬·¬«¬» ±®¿éð ©·¬¸ §±«® ¿½½±«²¬ óó ·²º±®³¿¬·±²ò ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ ÝßÔÔ ÜÞÓÍÁÐÎÛÐÎÑÝÛÍÍÑÎòÐÎ×ÒÌÁÐÑÍÌÁÐÎÑÝÛÍÍÛÜÁÍÑËÎÝÛøùÐßÝÕßÙÛùô ùÑÎßêïùô ùÓÇÁÐÕÙù÷å

Page 302: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 11-1: Using Conditional Compilation (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 190

3) Create a PL/SQL script that uses the ÜÞÓÍÁÜÞÁÊÛÎÍ×ÑÒ constant with conditional compilation. The code should test for the Oracle database version:

a) If the database version is less than or equal to 10.1, it should display the following error message: ˲­«°°±®¬»¼ ¼¿¬¿¾¿­» ®»´»¿­»ò

b) If the database version is 11.1 or higher, it should display the following message: λ´»¿­» ïïòî ·­ ­«°°±®¬»¼ò

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïïÁðíò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÞÛÙ×Ò ü×Ú ÜÞÓÍÁÜÞÁÊÛÎÍ×ÑÒòÊÛÎÁÔÛÁïðÁï üÌØÛÒ üÛÎÎÑÎ ù«²­«°°±®¬»¼ ¼¿¬¿¾¿­» ®»´»¿­»òù üÛÒÜ

üÛÔÍÛ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ øùλ´»¿­» ù ¤¤ ÜÞÓÍÁÜÞÁÊÛÎÍ×ÑÒòÊÛÎÍ×ÑÒ ¤¤ ùòù ¤¤ ÜÞÓÍÁÜÞÁÊÛÎÍ×ÑÒòÎÛÔÛßÍÛ ¤¤ ù ·­ ­«°°±®¬»¼òù÷å óó Ò±¬» ¬¸¿¬ ¬¸·­ ÝÑÓÓ×Ì ­§²¬¿¨ ·­ ²»©´§ ­«°°±®¬»¼ ·² ïðòî ÝÑÓÓ×Ì ÉÎ×ÌÛ ×ÓÓÛÜ×ßÌÛ ÒÑÉß×Ìå üÛÒÜ ÛÒÜå ñ

4) Consider the following code in the ´¿¾ÁïïÁðìò­¯´ script that uses ÝÎÛßÌÛÁÉÎßÐÐÛÜ to dynamically create and wrap a package specification and a package body in a database. Edit the ´¿¾ÁïïÁðìò­¯´ script to add the needed code to obfuscate the PL/SQL code. Save and then execute the script.

ÜÛÝÔßÎÛ óó ¬¸» °¿½µ¿¹»Á¬»¨¬ ª¿®·¿¾´» ½±²¬¿·²­ ¬¸» ¬»¨¬ ¬± ½®»¿¬» óó ¬¸» °¿½µ¿¹» ­°»½ ¿²¼ ¾±¼§ °¿½µ¿¹»Á¬»¨¬ ÊßÎÝØßÎîøíîéêé÷å ÚËÒÝÌ×ÑÒ ¹»²»®¿¬»Á­°»½ ø°µ¹²¿³» ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÊßÎÝØßÎî ßÍ ÞÛÙ×Ò ÎÛÌËÎÒ ùÝÎÛßÌÛ ÐßÝÕßÙÛ ù ¤¤ °µ¹²¿³» ¤¤ ù ßÍ

Page 303: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 11-1: Using Conditional Compilation (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 191

ÐÎÑÝÛÜËÎÛ ®¿·­»Á­¿´¿®§ ø»³°Á·¼ ÒËÓÞÛÎô ¿³±«²¬ ÒËÓÞÛÎ÷å ÐÎÑÝÛÜËÎÛ º·®»Á»³°´±§»» ø»³°Á·¼ ÒËÓÞÛÎ÷å ÛÒÜ ù ¤¤ °µ¹²¿³» ¤¤ ùåùå ÛÒÜ ¹»²»®¿¬»Á­°»½å ÚËÒÝÌ×ÑÒ ¹»²»®¿¬»Á¾±¼§ ø°µ¹²¿³» ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÊßÎÝØßÎî ßÍ ÞÛÙ×Ò ÎÛÌËÎÒ ùÝÎÛßÌÛ ÐßÝÕßÙÛ ÞÑÜÇ ù ¤¤ °µ¹²¿³» ¤¤ ù ßÍ ÐÎÑÝÛÜËÎÛ ®¿·­»Á­¿´¿®§ ø»³°Á·¼ ÒËÓÞÛÎô ¿³±«²¬ ÒËÓÞÛÎ÷ ×Í ÞÛÙ×Ò ËÐÜßÌÛ »³°´±§»»­ ÍÛÌ ­¿´¿®§ ã ­¿´¿®§ õ ¿³±«²¬ ÉØÛÎÛ »³°´±§»»Á·¼ ã »³°Á·¼å ÛÒÜ ®¿·­»Á­¿´¿®§å

ÐÎÑÝÛÜËÎÛ º·®»Á»³°´±§»» ø»³°Á·¼ ÒËÓÞÛÎ÷ ×Í ÞÛÙ×Ò ÜÛÔÛÌÛ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã »³°Á·¼å ÛÒÜ º·®»Á»³°´±§»»å ÛÒÜ ù ¤¤ °µ¹²¿³» ¤¤ ùåùå ÛÒÜ ¹»²»®¿¬»Á¾±¼§å

a) Generate the package specification while passing the »³°Á¿½¬·±²­ parameter.

b) Create and wrap the package specification. c) Generate the package body.

d) Create and wrap the package body. e) Call a procedure from the wrapped package as follows:

ÝßÔÔ »³°Á¿½¬·±²­ò®¿·­»Á­¿´¿®§øïîðô ïðð÷å

f) Use the ËÍÛÎÁÍÑËÎÝÛ data dictionary view to verify that the code is hidden as follows: ÍÛÔÛÝÌ ¬»¨¬ ÚÎÑÓ ËÍÛÎÁÍÑËÎÝÛ ÉØÛÎÛ ²¿³» ã ùÛÓÐÁßÝÌ×ÑÒÍùå

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´²ÁïïÁðìò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÜÛÝÔßÎÛ óó ¬¸» °¿½µ¿¹»Á¬»¨¬ ª¿®·¿¾´» ½±²¬¿·²­ ¬¸» ¬»¨¬ ¬± ½®»¿¬» ¬¸» °¿½µ¿¹» ­°»½ ¿²¼ ¾±¼§ °¿½µ¿¹»Á¬»¨¬ ÊßÎÝØßÎîøíîéêé÷å ÚËÒÝÌ×ÑÒ ¹»²»®¿¬»Á­°»½ ø°µ¹²¿³» ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÊßÎÝØßÎî ßÍ ÞÛÙ×Ò ÎÛÌËÎÒ ùÝÎÛßÌÛ ÐßÝÕßÙÛ ù ¤¤ °µ¹²¿³» ¤¤ ù ßÍ

Page 304: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 11-1: Using Conditional Compilation (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 192

ÐÎÑÝÛÜËÎÛ ®¿·­»Á­¿´¿®§ ø»³°Á·¼ ÒËÓÞÛÎô ¿³±«²¬ ÒËÓÞÛÎ÷å ÐÎÑÝÛÜËÎÛ º·®»Á»³°´±§»» ø»³°Á·¼ ÒËÓÞÛÎ÷å ÛÒÜ ù ¤¤ °µ¹²¿³» ¤¤ ùåùå ÛÒÜ ¹»²»®¿¬»Á­°»½å ÚËÒÝÌ×ÑÒ ¹»²»®¿¬»Á¾±¼§ ø°µ¹²¿³» ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÊßÎÝØßÎî ßÍ ÞÛÙ×Ò ÎÛÌËÎÒ ùÝÎÛßÌÛ ÐßÝÕßÙÛ ÞÑÜÇ ù ¤¤ °µ¹²¿³» ¤¤ ù ßÍ ÐÎÑÝÛÜËÎÛ ®¿·­»Á­¿´¿®§ ø»³°Á·¼ ÒËÓÞÛÎô ¿³±«²¬ ÒËÓÞÛÎ÷ ×Í ÞÛÙ×Ò ËÐÜßÌÛ »³°´±§»»­ ÍÛÌ ­¿´¿®§ ã ­¿´¿®§ õ ¿³±«²¬ ÉØÛÎÛ »³°´±§»»Á·¼ ã »³°Á·¼å ÛÒÜ ®¿·­»Á­¿´¿®§å ÐÎÑÝÛÜËÎÛ º·®»Á»³°´±§»» ø»³°Á·¼ ÒËÓÞÛÎ÷ ×Í ÞÛÙ×Ò ÜÛÔÛÌÛ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã »³°Á·¼å ÛÒÜ º·®»Á»³°´±§»»å ÛÒÜ ù ¤¤ °µ¹²¿³» ¤¤ ùåùå ÛÒÜ ¹»²»®¿¬»Á¾±¼§å

ÞÛÙ×Ò

óó ¹»²»®¿¬» °¿½µ¿¹» ­°»½ °¿½µ¿¹»Á¬»¨¬ æã ¹»²»®¿¬»Á­°»½øù»³°Á¿½¬·±²­ù÷å

óó ½®»¿¬» ¿²¼ ©®¿° ¬¸» °¿½µ¿¹» ­°»½ ÍÇÍòÜÞÓÍÁÜÜÔòÝÎÛßÌÛÁÉÎßÐÐÛÜø°¿½µ¿¹»Á¬»¨¬÷å

óó ¹»²»®¿¬» °¿½µ¿¹» ¾±¼§ °¿½µ¿¹»Á¬»¨¬ æã ¹»²»®¿¬»Á¾±¼§øù»³°Á¿½¬·±²­ù÷å

óó ½®»¿¬» ¿²¼ ©®¿° ¬¸» °¿½µ¿¹» ¾±¼§ ÍÇÍòÜÞÓÍÁÜÜÔòÝÎÛßÌÛÁÉÎßÐÐÛÜø°¿½µ¿¹»Á¬»¨¬÷å ÛÒÜå ñ

óó ½¿´´ ¿ °®±½»¼«®» º®±³ ¬¸» ©®¿°°»¼ °¿½µ¿¹»

ÝßÔÔ »³°Á¿½¬·±²­ò®¿·­»Á­¿´¿®§øïîðô ïðð÷å

óó Ë­» ¬¸» ËÍÛÎÁÍÑËÎÝÛ ¼¿¬¿ ¼·½¬·±²¿®§ ª·»© ¬± ª»®·º§ ¬¸¿¬ óó ¬¸» ½±¼» ·­ ¸·¼¼»² ¿­ º±´´±©­æ

ÍÛÔÛÝÌ ¬»¨¬ ÚÎÑÓ ËÍÛÎÁÍÑËÎÝÛ ÉØÛÎÛ ²¿³» ã ùÛÓÐÁßÝÌ×ÑÒÍùå

Page 305: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 11-1: Using Conditional Compilation (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 193

Page 306: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 11-1: Using Conditional Compilation (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 194

Page 307: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 195

Practices and Solutions for Lesson 12

In this practice, you use the ÜÛÐÌÎÛÛÁÚ×ÔÔ procedure and the ×ÜÛÐÌÎÛÛ view to investigate dependencies in your schema. In addition, you recompile invalid procedures, functions, packages, and views. Note: If you missed a step in a practice, please run the appropriate solution script for that practice step before proceeding to the next step or next practice.

Page 308: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 196

Practice 12-1: Managing Dependencies in Your Schema

In this practice, you use the ÜÛÐÌÎÛÛÁÚ×ÔÔ procedure and the ×ÜÛÐÌÎÛÛ view to investigate dependencies in your schema. In addition, you recompile invalid procedures, functions, packages, and views.

1) Create a tree structure showing all dependencies involving your ¿¼¼Á»³°´±§»» procedure and your ª¿´·¼Á¼»°¬·¼ function.

Note: ¿¼¼Á»³°´±§»» and ª¿´·¼Á¼»°¬·¼ were created in the lesson titled �Creating Functions.� You can run the solution scripts for Practice 3 if you need to create the procedure and function.

a) Load and execute the «¬´¼¬®»»ò­¯´ script, which is located in the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ´¿¾­ folder.

b) Execute the ¼»°¬®»»Áº·´´ procedure for the ¿¼¼Á»³°´±§»» procedure.

c) Query the ×ÜÛÐÌÎÛÛ view to see your results.

d) Execute the ¼»°¬®»»Áº·´´ procedure for the ª¿´·¼Á¼»°¬·¼ function.

e) Query the ×ÜÛÐÌÎÛÛ view to see your results.

If you have time, complete the following exercise: 2) Dynamically validate invalid objects.

a) Make a copy of your ÛÓÐÔÑÇÛÛÍ table, called ÛÓÐÍ.

b) Alter your ÛÓÐÔÑÇÛÛÍ table and add the column ÌÑÌÍßÔ with data type ÒËÓÞÛÎøçôî÷.

c) Create and save a query to display the name, type, and status of all invalid objects.

d) In the ½±³°·´»Á°µ¹ (created in Practice 7 of the lesson titled �Using Dynamic SQL�), add a procedure called ®»½±³°·´» that recompiles all invalid procedures, functions, and packages in your schema. Use Native Dynamic SQL to alter the invalid object type and compile it.

e) Execute the ½±³°·´»Á°µ¹ò®»½±³°·´» procedure.

f) Run the script file that you created in step 3 c. to check the value of the STATUS column. Do you still have objects with an ×ÒÊßÔ×Ü status?

Page 309: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units A - 197

Practice Solutions 12-1: Managing Dependencies in Your Schema

In this practice, you use the ÜÛÐÌÎÛÛÁÚ×ÔÔ procedure and the ×ÜÛÐÌÎÛÛ view to investigate dependencies in your schema. In addition, you recompile invalid procedures, functions, packages, and views.

1) Create a tree structure showing all dependencies involving your ¿¼¼Á»³°´±§»» procedure and your ª¿´·¼Á¼»°¬·¼ function.

Note: ¿¼¼Á»³°´±§»» and ª¿´·¼Á¼»°¬·¼ were created in the lesson titled �Creating Functions.� You can run the solution scripts for Practice 3 if you need to create the procedure and function.

a) Load and execute the «¬´¼¬®»»ò­¯´ script, which is located in the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ´¿¾­ folder.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ«¬´¼¬®»»ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

λ³ λ³ üØ»¿¼»®æ «¬´¼¬®»»ò­¯´ôª ïòî ïççîñïðñîê ïêæîìæìì ÎÕÑÑ× Í¬¿¾ ü λ³ λ³ ݱ°§®·¹¸¬ ø½÷ ïççï ¾§ Ñ®¿½´» ݱ®°±®¿¬·±² λ³ ÒßÓÛ Î»³ ¼»°¬®»»ò­¯´ ó ͸±© ±¾¶»½¬­ ®»½«®­·ª»´§ ¼»°»²¼»²¬ ±² λ³ ¹·ª»² ±¾¶»½¬ λ³ ÜÛÍÝÎ×ÐÌ×ÑÒ Î»³ ̸·­ °®±½»¼«®»ô ª·»© ¿²¼ ¬»³° ¬¿¾´» ©·´´ ¿´´±© §±« ¬± ­»» λ³ ¿´´ ±¾¶»½¬­ ¬¸¿¬ ¿®» ø®»½«®­·ª»´§÷ ¼»°»²¼»²¬ ±² ¬¸» ¹·ª»² λ³ ±¾¶»½¬ò λ³ Ò±¬»æ §±« ©·´´ ±²´§ ­»» ±¾¶»½¬­ º±® ©¸·½¸ §±« ¸¿ª» λ³ °»®³·­­·±²ò λ³ Û¨¿³°´»­æ λ³ »¨»½«¬» ¼»°¬®»»Áº·´´øù°®±½»¼«®»ùô ù­½±¬¬ùô ù¾·´´·²¹ù÷å λ³ ­»´»½¬ ö º®±³ ¼»°¬®»» ±®¼»® ¾§ ­»¯ýå λ³ λ³ »¨»½«¬» ¼»°¬®»»Áº·´´øù¬¿¾´»ùô ù­½±¬¬ùô ù»³°ù÷å λ³ ­»´»½¬ ö º®±³ ¼»°¬®»» ±®¼»® ¾§ ­»¯ýå λ³ λ³ »¨»½«¬» ¼»°¬®»»Áº·´´øù°¿½µ¿¹» ¾±¼§ùô ù­½±¬¬ùô λ³ ù¿½½¬­Á°¿§¿¾´»ù÷å λ³ ­»´»½¬ ö º®±³ ¼»°¬®»» ±®¼»® ¾§ ­»¯ýå λ³ λ³ ß °®»¬¬·»® ©¿§ ¬± ¼·­°´¿§ ¬¸·­ ·²º±®³¿¬·±² ¬¸¿² λ³ ­»´»½¬ ö º®±³ ¼»°¬®»» ±®¼»® ¾§ ­»¯ýå

Page 310: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 12-1: Managing Dependencies in Your Schema (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 198

λ³ ·­ λ³ ­»´»½¬ ö º®±³ ·¼»°¬®»»å λ³ ̸·­ ­¸±©­ ¬¸» ¼»°»²¼»²½§ ®»´¿¬·±²­¸·° ª·¿ ·²¼»²¬·²¹ò λ³ Ò±¬·½» ¬¸¿¬ ²± ±®¼»® ¾§ ½´¿«­» ·­ ²»»¼»¼ ©·¬¸ ·¼»°¬®»»ò λ³ ÎÛÌËÎÒÍ Î»³ λ³ ÒÑÌÛÍ Î»³ Ϋ² ¬¸·­ ­½®·°¬ ±²½» º±® »¿½¸ ­½¸»³¿ ¬¸¿¬ ²»»¼­ ¬¸·­ λ³ «¬·´·¬§ò λ³ ÓÑÜ×Ú×ÛÜ øÓÓñÜÜñÇÇ÷ λ³ ®µ±±· ïðñîêñçî ó ±©²»® óâ ­½¸»³¿ º±® ÍÏÔî λ³ ¹´«³°µ·² ïðñîðñçî ó λ²¿³»¼ º®±³ ÜÛÐÌÎÛÛòÍÏÔ Î»³ ®µ±±· ðçñðîñçî ó ½¸¿²¹» ÑÎË »®®±®­ λ³ ®µ±±· ðêñïðñçî ó ¿¼¼ ®¿» »®®±®­ λ³ ®µ±±· ðïñïíñçî ó «°¼¿¬» º±® ­§­ ª­ò ®»¹«´¿® «­»® λ³ ®µ±±· ðïñïðñçî ó º·¨ ·¼»°¬®»» λ³ ®µ±±· ðïñïðñçî ó Þ»¬¬»® º±®³¿¬¬·²¹ô ¿¼¼ ·¼»°¬®»» ª·»© λ³ ®µ±±· ïîñðîñçï ó ¼»¿´ ©·¬¸ ½«®­±®­ λ³ ®µ±±· ïðñïçñçï ó Ý®»¿¬·±² ÜÎÑÐ ÍÛÏËÛÒÝÛ ¼»°¬®»»Á­»¯ ñ ÝÎÛßÌÛ ÍÛÏËÛÒÝÛ ¼»°¬®»»Á­»¯ ½¿½¸» îðð ñö ½¿½¸» îð𠬱 ³¿µ» ­»¯«»²½» º¿­¬»® öñ ñ ÜÎÑÐ ÌßÞÔÛ ¼»°¬®»»Á¬»³°¬¿¾ ñ ÝÎÛßÌÛ ÌßÞÔÛ ¼»°¬®»»Á¬»³°¬¿¾ ø ±¾¶»½¬Á·¼ ²«³¾»®ô ®»º»®»²½»¼Á±¾¶»½¬Á·¼ ²«³¾»®ô ²»­¬Á´»ª»´ ²«³¾»®ô ­»¯ý ²«³¾»® ÷ ñ ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ ¼»°¬®»»Áº·´´ ø¬§°» ½¸¿®ô ­½¸»³¿ ½¸¿®ô ²¿³» ½¸¿®÷ ×Í ±¾¶Á·¼ ²«³¾»®å ÞÛÙ×Ò ÜÛÔÛÌÛ ÚÎÑÓ ¼»°¬®»»Á¬»³°¬¿¾å ÝÑÓÓ×ÌÌå ÍÛÔÛÝÌ ±¾¶»½¬Á·¼ ×ÒÌÑ ±¾¶Á·¼ ÚÎÑÓ ¿´´Á±¾¶»½¬­ ÉØÛÎÛ ±©²»® ã «°°»®ø¼»°¬®»»Áº·´´ò­½¸»³¿÷ ßÒÜ ±¾¶»½¬Á²¿³» ã «°°»®ø¼»°¬®»»Áº·´´ò²¿³»÷ ßÒÜ ±¾¶»½¬Á¬§°» ã «°°»®ø¼»°¬®»»Áº·´´ò¬§°»÷å ×ÒÍÛÎÌ ×ÒÌÑ ¼»°¬®»»Á¬»³°¬¿¾ ÊßÔËÛÍø±¾¶Á·¼ô ðô ðô ð÷å

Page 311: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 12-1: Managing Dependencies in Your Schema (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 199

×ÒÍÛÎÌ ×ÒÌÑ ¼»°¬®»»Á¬»³°¬¿¾ ÍÛÔÛÝÌ ±¾¶»½¬Á·¼ô ®»º»®»²½»¼Á±¾¶»½¬Á·¼ô ´»ª»´ô ¼»°¬®»»Á­»¯ò²»¨¬ª¿´ ÚÎÑÓ °«¾´·½Á¼»°»²¼»²½§ ÝÑÒÒÛÝÌ ÞÇ ÐÎ×ÑÎ ±¾¶»½¬Á·¼ ã ®»º»®»²½»¼Á±¾¶»½¬Á·¼ ÍÌßÎÌ É×ÌØ ®»º»®»²½»¼Á±¾¶»½¬Á·¼ ã ¼»°¬®»»Áº·´´ò±¾¶Á·¼å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ²±Á¼¿¬¿Áº±«²¼ ¬¸»² ®¿·­»Á¿°°´·½¿¬·±²Á»®®±®øóîððððô ùÑÎËóïððïíæ ù ¤¤ ¬§°» ¤¤ ù ù ¤¤ ­½¸»³¿ ¤¤ ùòù ¤¤ ²¿³» ¤¤ ù ©¿­ ²±¬ º±«²¼òù÷å ÛÒÜå ñ ÜÎÑÐ Ê×ÛÉ ¼»°¬®»» ñ ÍÛÌ ÛÝØÑ ÑÒ ÎÛÓ Ì¸·­ ª·»© ©·´´ ­«½½»»¼ ·º ½«®®»²¬ «­»® ·­ ­§­ò ̸·­ ª·»© ÎÛÓ ­¸±©­ ©¸·½¸ ­¸¿®»¼ ½«®­±®­ ¼»°»²¼ ±² ¬¸» ¹·ª»² ±¾¶»½¬ò ׺ ÎÛÓ ¬¸» ½«®®»²¬ «­»® ·­ ²±¬ ­§­ô ¬¸»² ¬¸·­ ª·»© ¹»¬ ¿² »®®±® ÎÛÓ »·¬¸»® ¿¾±«¬ ´¿½µ ±º °®·ª·´»¹»­ ±® ¿¾±«¬ ¬¸» ²±²ó»¨·­¬»²½» ±º ÎÛÓ ¬¿¾´» ¨üµ¹´¨­ò ÍÛÌ ÛÝØÑ ÑÚÚ ÝÎÛßÌÛ Ê×ÛÉ ­§­ò¼»°¬®»» ø²»­¬»¼Á´»ª»´ô ¬§°»ô ­½¸»³¿ô ²¿³»ô ­»¯ý÷ ßÍ ÍÛÔÛÝÌ ¼ò²»­¬Á´»ª»´ô ±ò±¾¶»½¬Á¬§°»ô ±ò±©²»®ô ±ò±¾¶»½¬Á²¿³»ô ¼ò­»¯ý ÚÎÑÓ ¼»°¬®»»Á¬»³°¬¿¾ ¼ô ¼¾¿Á±¾¶»½¬­ ± ÉØÛÎÛ ¼ò±¾¶»½¬Á·¼ ã ±ò±¾¶»½¬Á·¼ øõ÷ ËÒ×ÑÒ ßÔÔ ÍÛÔÛÝÌ ¼ò²»­¬Á´»ª»´õïô ùÝËÎÍÑÎùô ùä­¸¿®»¼âùô ùþù¤¤½òµ¹´²¿±¾¶¤¤ùþùô ¼ò­»¯ýõòë ÚÎÑÓ ¼»°¬®»»Á¬»³°¬¿¾ ¼ô ¨üµ¹´¼° µô ¨üµ¹´±¾ ¹ô ±¾¶ü ±ô «­»®ü «ô ¨üµ¹´±¾ ½ô ¨üµ¹´¨­ ¿ ÉØÛÎÛ ¼ò±¾¶»½¬Á·¼ ã ±ò±¾¶ý ßÒÜ ±ò²¿³» ã ¹òµ¹´²¿±¾¶ ßÒÜ ±ò±©²»®ý ã «ò«­»®ý ßÒÜ «ò²¿³» ã ¹òµ¹´²¿±©² ßÒÜ ¹òµ¹´¸¼¿¼® ã µòµ¹´®º¸¼´ ßÒÜ µòµ¹´¸¼¿¼® ã ¿òµ¹´¸¼¿¼® ñö ³¿µ» ­«®» ·¬ ·­ ²±¬ ¿ ¬®¿²­·¬·ª» öñ ßÒÜ µòµ¹´¼»°²± ã ¿òµ¹´¨­¼»° ñö ®»º»®»²½»ô ¾«¬ ¿ ¼·®»½¬ ±²» öñ ßÒÜ µòµ¹´¸¼¿¼® ã ½òµ¹´¸¼¿¼® ßÒÜ ½òµ¹´¸¼²­° ã ð ñö ¿ ½«®­±® öñ

Page 312: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 12-1: Managing Dependencies in Your Schema (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 200

ñ ÍÛÌ ÛÝØÑ ÑÒ ÎÛÓ Ì¸·­ ª·»© ©·´´ ­«½½»»¼ ·º ½«®®»²¬ «­»® ·­ ²±¬ ­§­ò ̸·­ ª·»© ÎÛÓ ¼±»­ ö²±¬ö ­¸±© ©¸·½¸ ­¸¿®»¼ ½«®­±®­ ¼»°»²¼ ±² ¬¸» ¹·ª»² ÎÛÓ ±¾¶»½¬ò ÎÛÓ ×º ¬¸» ½«®®»²¬ «­»® ·­ ­§­ ¬¸»² ¬¸·­ ª·»© ©·´´ ¹»¬ ¿² »®®±® ÎÛÓ ·²¼·½¿¬·²¹ ¬¸¿¬ ¬¸» ª·»© ¿´®»¿¼§ »¨·­¬­ ø­·²½» °®·±® ª·»© ÎÛÓ ½®»¿¬» ©·´´ ¸¿ª» ­«½½»»¼»¼÷ò ÍÛÌ ÛÝØÑ ÑÚÚ ÝÎÛßÌÛ Ê×ÛÉ ¼»°¬®»» ø²»­¬»¼Á´»ª»´ô ¬§°»ô ­½¸»³¿ô ²¿³»ô ­»¯ý÷ ßÍ ­»´»½¬ ¼ò²»­¬Á´»ª»´ô ±ò±¾¶»½¬Á¬§°»ô ±ò±©²»®ô ±ò±¾¶»½¬Á²¿³»ô ¼ò­»¯ý ÚÎÑÓ ¼»°¬®»»Á¬»³°¬¿¾ ¼ô ¿´´Á±¾¶»½¬­ ± ÉØÛÎÛ ¼ò±¾¶»½¬Á·¼ ã ±ò±¾¶»½¬Á·¼ øõ÷ ñ ÜÎÑÐ Ê×ÛÉ ·¼»°¬®»» ñ ÝÎÛßÌÛ Ê×ÛÉ ·¼»°¬®»» ø¼»°»²¼»²½·»­÷ ßÍ ÍÛÔÛÝÌ ´°¿¼øù ùôíöø³¿¨ø²»­¬»¼Á´»ª»´÷÷÷ ¤¤ ³¿¨ø²ª´ø¬§°»ô ùä²± °»®³·­­·±²âù÷ ¤¤ ù ù ¤¤ ­½¸»³¿ ¤¤ ¼»½±¼»ø¬§°»ô ÒËÔÔô ùùô ùòù÷ ¤¤ ²¿³»÷ ÚÎÑÓ ¼»°¬®»» ÙÎÑËÐ ÞÇ ­»¯ý ñö ͱ «­»® ½¿² ±³·¬ ­±®¬ó¾§ ©¸»² ­»´»½¬·²¹ º®±³ ·¼»°¬®»» öñ ñ

Page 313: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 12-1: Managing Dependencies in Your Schema (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 201

Page 314: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 12-1: Managing Dependencies in Your Schema (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 202

Page 315: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 12-1: Managing Dependencies in Your Schema (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 203

b) Execute the ¼»°¬®»»Áº·´´ procedure for the ¿¼¼Á»³°´±§»» procedure.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïîÁðïÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÛÈÛÝËÌÛ ¼»°¬®»»Áº·´´øùÐÎÑÝÛÜËÎÛùô ËÍÛÎô ù¿¼¼Á»³°´±§»»ù÷

c) Query the ×ÜÛÐÌÎÛÛ view to see your results.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïîÁðïÁ½ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ÍÛÔÛÝÌ ö ÚÎÑÓ ×ÜÛÐÌÎÛÛå

d) Execute the ¼»°¬®»»Áº·´´ procedure for the ª¿´·¼Á¼»°¬·¼ function.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïîÁðïÁ¼ò­¯´script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ÛÈÛÝËÌÛ ¼»°¬®»»Áº·´´øùÚËÒÝÌ×ÑÒùô ËÍÛÎô ùª¿´·¼Á¼»°¬·¼ù÷

Page 316: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 12-1: Managing Dependencies in Your Schema (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 204

e) Query the ×ÜÛÐÌÎÛÛ view to see your results.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïîÁðïÁ»ò­¯´ script. Click the Execute Statement (F9) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÍÛÔÛÝÌ ö ÚÎÑÓ ×ÜÛÐÌÎÛÛå

If you have time, complete the following exercise: 2) Dynamically validate invalid objects.

a) Make a copy of your ÛÓÐÔÑÇÛÛÍ table, called ÛÓÐÍ.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïîÁðîÁ¿ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ÝÎÛßÌÛ ÌßÞÔÛ »³°­ ßÍ ÍÛÔÛÝÌ ö ÚÎÑÓ »³°´±§»»­å

b) Alter your ÛÓÐÔÑÇÛÛÍ table and add the column ÌÑÌÍßÔ with data type ÒËÓÞÛÎøçôî÷.

Page 317: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 12-1: Managing Dependencies in Your Schema (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 205

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïîÁðîÁ¾ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ßÔÌÛÎ ÌßÞÔÛ »³°´±§»»­ ßÜÜ ø¬±¬­¿´ ÒËÓÞÛÎøçôî÷÷å

c) Create and save a query to display the name, type, and status of all invalid objects.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïîÁðîÁ½ò­¯´ script. Click the Execute Statement (F9) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. ÍÛÔÛÝÌ ±¾¶»½¬Á²¿³»ô ±¾¶»½¬Á¬§°»ô ­¬¿¬«­ ÚÎÑÓ ËÍÛÎÁÑÞÖÛÝÌÍ ÉØÛÎÛ ­¬¿¬«­ ã ù×ÒÊßÔ×Üùå

d) In the ½±³°·´»Á°µ¹ (created in Practice 6 of the lesson titled �Using Dynamic SQL�), add a procedure called ®»½±³°·´» that recompiles all invalid procedures, functions, and packages in your schema. Use Native Dynamic SQL to alter the invalid object type and compile it.

Page 318: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 12-1: Managing Dependencies in Your Schema (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 206

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïîÁðîÁ¼ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below. The newly added code is highlighted in bold letters in the following code box.

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ½±³°·´»Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ³¿µ»ø²¿³» ÊßÎÝØßÎî÷å ÐÎÑÝÛÜËÎÛ ®»½±³°·´»å ÛÒÜ ½±³°·´»Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ ½±³°·´»Á°µ¹ ×Í

ÐÎÑÝÛÜËÎÛ »¨»½«¬»ø­¬³¬ ÊßÎÝØßÎî÷ ×Í ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø­¬³¬÷å ÛÈÛÝËÌÛ ×ÓÓÛÜ×ßÌÛ ­¬³¬å ÛÒÜå

ÚËÒÝÌ×ÑÒ ¹»¬Á¬§°»ø²¿³» ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÊßÎÝØßÎî ×Í °®±½Á¬§°» ÊßÎÝØßÎîøíð÷ æã ÒËÔÔå ÞÛÙ×Ò ñö ö ̸» ÎÑÉÒËÓ ã ï ·­ ¿¼¼»¼ ¬± ¬¸» ½±²¼·¬·±² ö ¬± »²­«®» ±²´§ ±²» ®±© ·­ ®»¬«®²»¼ ·º ¬¸» ö ²¿³» ®»°®»­»²¬­ ¿ ÐßÝÕßÙÛô ©¸·½¸ ³¿§ ¿´­± ö ¸¿ª» ¿ ÐßÝÕßÙÛ ÞÑÜÇò ײ ¬¸·­ ½¿­»ô ©» ½¿² ö ±²´§ ½±³°·´» ¬¸» ½±³°´»¬» °¿½µ¿¹»ô ¾«¬ ²±¬ ö ¬¸» ­°»½·º·½¿¬·±² ±® ¾±¼§ ¿­ ­»°¿®¿¬» ö ½±³°±²»²¬­ò öñ ÍÛÔÛÝÌ ±¾¶»½¬Á¬§°» ×ÒÌÑ °®±½Á¬§°» ÚÎÑÓ «­»®Á±¾¶»½¬­ ÉØÛÎÛ ±¾¶»½¬Á²¿³» ã ËÐÐÛÎø²¿³»÷ ßÒÜ ÎÑÉÒËÓ ã ïå ÎÛÌËÎÒ °®±½Á¬§°»å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ ÎÛÌËÎÒ ÒËÔÔå ÛÒÜå ÐÎÑÝÛÜËÎÛ ³¿µ»ø²¿³» ÊßÎÝØßÎî÷ ×Í ­¬³¬ ÊßÎÝØßÎîøïðð÷å °®±½Á¬§°» ÊßÎÝØßÎîøíð÷ æã ¹»¬Á¬§°»ø²¿³»÷å ÞÛÙ×Ò ×Ú °®±½Á¬§°» ×Í ÒÑÌ ÒËÔÔ ÌØÛÒ ­¬³¬ æã ùßÔÌÛÎ ù¤¤ °®±½Á¬§°» ¤¤ù ù¤¤ ²¿³» ¤¤ù ÝÑÓÐ×ÔÛùå

Page 319: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 12-1: Managing Dependencies in Your Schema (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 207

»¨»½«¬»ø­¬³¬÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðððïô ùÍ«¾°®±¹®¿³ ùùù¤¤ ²¿³» ¤¤ùùù ¼±»­ ²±¬ »¨·­¬ù÷å ÛÒÜ ×Úå ÛÒÜ ³¿µ»å

ÐÎÑÝÛÜËÎÛ ®»½±³°·´» ×Í ­¬³¬ ÊßÎÝØßÎîøîðð÷å ±¾¶Á²¿³» «­»®Á±¾¶»½¬­ò±¾¶»½¬Á²¿³»û¬§°»å ±¾¶Á¬§°» «­»®Á±¾¶»½¬­ò±¾¶»½¬Á¬§°»û¬§°»å ÞÛÙ×Ò ÚÑÎ ±¾¶®»½ ×Ò øÍÛÔÛÝÌ ±¾¶»½¬Á²¿³»ô ±¾¶»½¬Á¬§°» ÚÎÑÓ «­»®Á±¾¶»½¬­ ÉØÛÎÛ ­¬¿¬«­ ã ù×ÒÊßÔ×Üù ßÒÜ ±¾¶»½¬Á¬§°» äâ ùÐßÝÕßÙÛ ÞÑÜÇù÷ ÔÑÑÐ ­¬³¬ æã ùßÔÌÛÎ ù¤¤ ±¾¶®»½ò±¾¶»½¬Á¬§°» ¤¤ù ù¤¤ ±¾¶®»½ò±¾¶»½¬Á²¿³» ¤¤ù ÝÑÓÐ×ÔÛùå »¨»½«¬»ø­¬³¬÷å ÛÒÜ ÔÑÑÐå ÛÒÜ ®»½±³°·´»å

ÛÒÜ ½±³°·´»Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

e) Execute the ½±³°·´»Á°µ¹ò®»½±³°·´» procedure.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïîÁðîÁ»ò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÛÈÛÝËÌÛ ½±³°·´»Á°µ¹ò®»½±³°·´»

Page 320: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 12-1: Managing Dependencies in Your Schema (continued)

Oracle Database 11g: Develop PL/SQL Program Units A - 208

f) Run the script file that you created in step 3 c. to check the value of the STATUS column. Do you still have objects with an ×ÒÊßÔ×Ü status?

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´ÁïîÁðîÁºò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to run the script. The code and the results are shown below.

ÍÛÔÛÝÌ ±¾¶»½¬Á²¿³»ô ±¾¶»½¬Á¬§°»ô ­¬¿¬«­ ÚÎÑÓ ËÍÛÎÁÑÞÖÛÝÌÍ ÉØÛÎÛ ­¬¿¬«­ ã ù×ÒÊßÔ×Üùå

Page 321: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2
Page 322: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Copyright © 2009, Oracle. All rights reserved.

Review of PL/SQL

Page 323: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 2

Copyright © 2009, Oracle. All rights reserved.F - 2

Objectives

After completing this appendix, you should be able to do the following:• Review the block structure for anonymous

PL/SQL blocks• Declare PL/SQL variables• Create PL/SQL records and tables• Insert, update, and delete data• Use IF, THEN, and ELSIF statements• Use basic, FOR, and WHILE loops• Declare and use explicit cursors with parameters• Use cursor FOR loops and FOR UPDATE and WHERE

CURRENT OF clauses• Trap predefined and user-defined exceptions

Lesson AimYou have learned about implicit cursors that are automatically created by PL/SQL when you execute a SQL SELECT or DML statement. In this lesson, you learn about explicit cursors. You learn to differentiate between implicit and explicit cursors. You also learn to declare and control simple cursors as well as cursors with parameters.

Page 324: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 3

Copyright © 2009, Oracle. All rights reserved.F - 3

Block Structure for Anonymous PL/SQL Blocks

• DECLARE (optional)– Declare PL/SQL objects to be used within this block.

• BEGIN (mandatory)– Define the executable statements.

• EXCEPTION (optional)– Define the actions that take place if an error or exception

arises.• END; (mandatory)

Anonymous BlocksAnonymous blocks do not have names. You declare them at the point in an application where they are to be run, and they are passed to the PL/SQL engine for execution at run time.

• The section between the keywords DECLARE and BEGIN is referred to as the declaration section. In the declaration section, you define the PL/SQL objects such as variables, constants, cursors, and user-defined exceptions that you want to reference within the block. The DECLARE keyword is optional if you do not declare any PL/SQL objects.

• The BEGIN and END keywords are mandatory and enclose the body of actions to be performed. This section is referred to as the executable section of the block.

• The section between EXCEPTION and END is referred to as the exception section. The exception section traps error conditions. In it, you define actions to take if a specified condition arises. The exception section is optional.

The keywords DECLARE, BEGIN, and EXCEPTION are not followed by semicolons, but ENDand all other PL/SQL statements do require semicolons.

Page 325: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 4

Copyright © 2009, Oracle. All rights reserved.F - 4

Declaring PL/SQL Variables

• Syntax:

• Examples:

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

Declarev_hiredate DATE;v_deptno NUMBER(2) NOT NULL := 10;v_location VARCHAR2(13) := 'Atlanta';c_ comm CONSTANT NUMBER := 1400;v_count BINARY_INTEGER := 0;v_valid BOOLEAN NOT NULL := TRUE;

Declaring PL/SQL VariablesYou need to declare all PL/SQL identifiers within the declaration section before referencing them within the PL/SQL block. You have the option to assign an initial value. You do not need to assign a value to a variable in order to declare it. If you refer to other variables in a declaration, you must be sure to declare them separately in a previous statement.In the syntax:

Identifier is the name of the variable

CONSTANT constrains the variable so that its value cannot change; constants must be initialized.

Datatype is a scalar, composite, reference, or LOB data type (This course covers only scalar and composite data types.)

NOT NULL constrains the variable so that it must contain a value; NOT NULL variables must be initialized.

expr is any PL/SQL expression that can be a literal, another variable, or an expression involving operators and functions

Page 326: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 5

Copyright © 2009, Oracle. All rights reserved.F - 5

Declaring Variables with the %TYPE Attribute: Examples

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

...

Declaring Variables with the %TYPE AttributeDeclare variables to store the name of an employee....v_ename employees.last_name%TYPE;...

Declare variables to store the balance of a bank account, as well as the minimum balance, which starts out as 10....v_balance NUMBER(7,2);v_min_balance v_balance%TYPE := 10;...

A NOT NULL column constraint does not apply to variables declared using %TYPE. Therefore, if you declare a variable using the %TYPE attribute and a database column defined as NOT NULL, then you can assign the NULL value to the variable.

Page 327: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 6

Copyright © 2009, Oracle. All rights reserved.F - 6

Creating a PL/SQL Record

Declare variables to store the name, job, and salary of a new employee.

...TYPE emp_record_type IS RECORD

(ename VARCHAR2(25),job VARCHAR2(10),sal NUMBER(8,2));

emp_record emp_record_type;...

Creating a PL/SQL RecordField declarations are like variable declarations. Each field has a unique name and a specific data type. There are no predefined data types for PL/SQL records, as there are for scalar variables. Therefore, you must create the data type first and then declare an identifier using that data type.The following example shows that you can use the %TYPE attribute to specify a field data type:DECLARE

TYPE emp_record_type IS RECORD(empid NUMBER(6) NOT NULL := 100,ename employees.last_name%TYPE,job employees.job_id%TYPE);

emp_record emp_record_type;...

Note: You can add the NOT NULL constraint to any field declaration to prevent the assigning ofnulls to that field. Remember that fields declared as NOT NULL must be initialized.

Page 328: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 7

Copyright © 2009, Oracle. All rights reserved.F - 7

%ROWTYPE Attribute: Examples

• Declare a variable to store the same information about a department as is stored in the DEPARTMENTS table.

• Declare a variable to store the same information about an employee as is stored in the EMPLOYEES table.

dept_record departments%ROWTYPE;

emp_record employees%ROWTYPE;

ExamplesThe first declaration in the slide creates a record with the same field names and field data types as a row in the DEPARTMENTS table. The fields are DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, and LOCATION_ID.The second declaration in the slide creates a record with the same field names and field data types as a row in the EMPLOYEES table. The fields are EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY, COMMISSION_PCT, MANAGER_ID, and DEPARTMENT_ID.In the following example, you select column values into a record named job_record.

DECLAREjob_record jobs%ROWTYPE;...

BEGINSELECT * INTO job_recordFROM jobsWHERE ...

Page 329: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 8

Copyright © 2009, Oracle. All rights reserved.F - 8

Creating a PL/SQL Table

DECLARETYPE ename_table_type IS TABLE OF

employees.last_name%TYPEINDEX BY BINARY_INTEGER;

TYPE hiredate_table_type IS TABLE OF DATEINDEX BY BINARY_INTEGER;

ename_table ename_table_type; hiredate_table hiredate_table_type;

BEGINename_table(1) := 'CAMERON';hiredate_table(8) := SYSDATE + 7;

IF ename_table.EXISTS(1) THENINSERT INTO ...

...END;

Creating a PL/SQL TableThere are no predefined data types for PL/SQL tables, as there are for scalar variables. Therefore, you must create the data type first and then declare an identifier using that data type.Referencing a PL/SQL TableSyntax

pl/sql_table_name(primary_key_value)In this syntax, primary_key_value belongs to the BINARY_INTEGER type.

Reference the third row in a PL/SQL table ENAME_TABLE.

ename_table(3) ...The magnitude range of a BINARY_INTEGER is –2,147,483,647 through 2,147,483,647. The primary key value can therefore be negative. Indexing need not start with 1.

Note: The table.EXISTS(i) statement returns TRUE if at least one row with index i is returned. Use the EXISTS statement to prevent an error that is raised in reference to a nonexistent table element.

Page 330: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 9

Copyright © 2009, Oracle. All rights reserved.F - 9

SELECT Statements in PL/SQL: Example

The INTO clause is mandatory.

DECLAREv_deptid NUMBER(4);v_loc NUMBER(4);

BEGINSELECT department_id, location_idINTO v_deptid, v_locFROM departmentsWHERE department_name = 'Sales'; ...

END;

INTO ClauseThe INTO clause is mandatory and occurs between the SELECT and FROM clauses. It is used to specify the names of variables to hold the values that SQL returns from the SELECT clause. You must give one variable for each item selected, and the order of variables must correspond to the items selected.You use the INTO clause to populate either PL/SQL variables or host variables.Queries Must Return One and Only One RowSELECT statements within a PL/SQL block fall into the ANSI classification of Embedded SQL, for which the following rule applies: Queries must return one and only one row. More than one row or no row generates an error.PL/SQL deals with these errors by raising standard exceptions, which you can trap in the exception section of the block with the NO_DATA_FOUND and TOO_MANY_ROWS exceptions. You should code SELECT statements to return a single row.

Page 331: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 10

Copyright © 2009, Oracle. All rights reserved.F - 10

Inserting Data: Example

Add new employee information to the EMPLOYEES table.

DECLAREv_empid employees.employee_id%TYPE;

BEGINSELECT employees_seq.NEXTVAL INTO v_empnoFROM dual;INSERT INTO employees(employee_id, last_name,

job_id, department_id)VALUES(v_empid, 'HARDING', 'PU_CLERK', 30);

END;

Inserting Data• Use SQL functions, such as USER and SYSDATE.• Generate primary key values by using database sequences.• Derive values in the PL/SQL block.• Add column default values.

Note: There is no possibility for ambiguity with identifiers and column names in the INSERTstatement. Any identifier in the INSERT clause must be a database column name.

Page 332: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 11

Copyright © 2009, Oracle. All rights reserved.F - 11

Updating Data: Example

Increase the salary of all employees in the EMPLOYEES table who are purchasing clerks.

DECLAREv_sal_increase employees.salary%TYPE := 2000;

BEGINUPDATE employeesSET salary = salary + v_sal_increaseWHERE job_id = 'PU_CLERK';

END;

Updating DataThere may be ambiguity in the SET clause of the UPDATE statement because, although the identifier on the left of the assignment operator is always a database column, the identifier on the right can be either a database column or a PL/SQL variable.

Remember that the WHERE clause is used to determine which rows are affected. If no rows are modified, no error occurs (unlike the SELECT statement in PL/SQL).

Note: PL/SQL variable assignments always use := and SQL column assignments alwaysuse =.. Remember that if column names and identifier names are identical in the WHERE clause, the Oracle server looks to the database first for the name.

Page 333: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 12

Copyright © 2009, Oracle. All rights reserved.F - 12

Deleting Data: Example

Delete rows that belong to department 190 from the EMPLOYEES table.

DECLAREv_deptid employees.department_id%TYPE := 190;

BEGINDELETE FROM employeesWHERE department_id = v_deptid;

END;

Deleting Data: ExampleDelete a specific job:

DECLAREv_jobid jobs.job_id%TYPE := ‘PR_REP’;

BEGINDELETE FROM jobsWHERE job_id = v_jobid;

END;

Page 334: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 13

Copyright © 2009, Oracle. All rights reserved.F - 13

Controlling Transactions with the COMMIT and ROLLBACK Statements

• Initiate a transaction with the first DML command to follow a COMMIT or ROLLBACK statement.

• Use COMMIT and ROLLBACK SQL statements to terminate a transaction explicitly.

Controlling Transactions with the COMMIT and ROLLBACK StatementsYou control the logic of transactions with COMMIT and ROLLBACK SQL statements, rendering some groups of database changes permanent while discarding others. As with the Oracle server, data manipulation language (DML) transactions start at the first command to follow a COMMITor ROLLBACK and end on the next successful COMMIT or ROLLBACK. These actions may occur within a PL/SQL block or as a result of events in the host environment. A COMMIT ends the current transaction by making all pending changes to the database permanent.Syntax

COMMIT [WORK];ROLLBACK [WORK];

In this syntax, WORK is for compliance with ANSI standards.Note: The transaction control commands are all valid within PL/SQL, although the host environment may place some restriction on their use. You can also include explicit locking commands (such as LOCK TABLE and SELECT ... FOR UPDATE) in a block. They stay in effect until the end of the transaction. Also, one PL/SQL block does not necessarily imply one transaction.

Page 335: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 14

Copyright © 2009, Oracle. All rights reserved.F - 14

IF, THEN, and ELSIF Statements: Example

For a given value entered, return a calculated value.

. . .IF v_start > 100 THEN

v_start := 2 * v_start;ELSIF v_start >= 50 THEN

v_start := 0.5 * v_start;ELSE

v_start := 0.1 * v_start;END IF;. . .

IF, THEN, and ELSIF StatementsWhen possible, use the ELSIF clause instead of nesting IF statements. The code is easier to read and understand, and the logic is clearly identified. If the action in the ELSE clause consists purely of another IF statement, it is more convenient to use the ELSIF clause. This makes the code clearer by removing the need for nested END IFs at the end of each further set of conditions and actions.Example

IF condition1 THEN statement1;

ELSIF condition2 THEN statement2;

ELSIF condition3 THENstatement3;

END IF;The statement in the slide is further defined as follows:For a given value entered, return a calculated value. If the entered value is over 100, then the calculated value is two times the entered value. If the entered value is between 50 and 100, then the calculated value is 50% of the starting value. If the entered value is less than 50, then the calculated value is 10% of the starting value.Note: Any arithmetic expression containing null values evaluates to null.

Page 336: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 15

Copyright © 2009, Oracle. All rights reserved.F - 15

Basic Loop: Example

DECLAREv_ordid order_items.order_id%TYPE := 101;v_counter NUMBER(2) := 1;

BEGINLOOP

INSERT INTO order_items(order_id,line_item_id) VALUES(v_ordid, v_counter);v_counter := v_counter + 1;EXIT WHEN v_counter > 10;

END LOOP;END;

Basic Loop: ExampleThe basic loop example shown in the slide is defined as follows:Insert the first 10 new line items for order number 101.Note: A basic loop enables execution of its statements at least once, even if the condition has been met upon entering the loop.

Page 337: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 16

Copyright © 2009, Oracle. All rights reserved.F - 16

FOR Loop: Example

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

DECLAREv_ordid order_items.order_id%TYPE := 101;

BEGINFOR i IN 1..10 LOOP

INSERT INTO order_items(order_id,line_item_id) VALUES(v_ordid, i);

END LOOP;END;

FOR Loop: ExampleThe slide shows a FOR loop that inserts 10 rows into the order_items table.

Page 338: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 17

Copyright © 2009, Oracle. All rights reserved.F - 17

WHILE Loop: Example

ACCEPT p_price PROMPT 'Enter the price of the item: 'ACCEPT p_itemtot -PROMPT 'Enter the maximum total for purchase of item: 'DECLARE...v_qty NUMBER(8) := 1;v_running_total NUMBER(7,2) := 0;

BEGIN...WHILE v_running_total < &p_itemtot LOOP

...v_qty := v_qty + 1;v_running_total := v_qty * &p_price;END LOOP;

...

WHILE Loop: ExampleIn the example in the slide, the quantity increases with each iteration of the loop until the quantity is no longer less than the maximum price allowed for spending on the item.

Page 339: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 18

Copyright © 2009, Oracle. All rights reserved.F - 18

SQL Implicit Cursor Attributes

You can use SQL cursor attributes to test the outcome of your SQL statements.

Boolean attribute that always evaluates to FALSEbecause PL/SQL closes implicit cursors immediately after they are executed

SQL%ISOPEN

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

SQL%NOTFOUND

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

SQL%ROWCOUNT

SQL%FOUND

SQL Cursor Attributes

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

Description

SQL Implicit Cursor AttributesSQL cursor attributes enable you to evaluate what happened when the implicit cursor was last used. You use these attributes in PL/SQL statements such as functions. You cannot use them in SQL statements. You can use the SQL%ROWCOUNT, SQL%FOUND, SQL%NOTFOUND, and SQL%ISOPENattributes in the exception section of a block to gather information about the execution of a DML statement. In PL/SQL, a DML statement that does not change any rows is not seen as an error condition, whereas the SELECT statement will return an exception if it cannot locate any rows.

Page 340: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 19

Copyright © 2009, Oracle. All rights reserved.F - 19

No

Controlling Explicit Cursors

DECLARE OPEN EMPTY? CLOSEYesFETCH

Create a named SQL

area

Identify the active set

Load the current row

into variables

Test for existing rows

Return to FETCH if rows are found

Release the active set

Explicit CursorsControlling Explicit Cursors Using Four Commands

1. Declare the cursor by naming it and defining the structure of the query to be performed within it.

2. Open the cursor. The OPEN statement executes the query and binds any variables that are referenced. Rows identified by the query are called the active set and are now available for fetching.

3. Fetch data from the cursor. The FETCH statement loads the current row from the cursor into variables. Each fetch causes the cursor to move its pointer to the next row in the active set. Therefore, each fetch accesses a different row returned by the query. In the flow diagram in the slide, each fetch tests the cursor for any existing rows. If rows are found, it loads the current row into variables; otherwise, it closes the cursor.

4. Close the cursor. The CLOSE statement releases the active set of rows. It is now possible to reopen the cursor to establish a fresh active set.

Page 341: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 20

Copyright © 2009, Oracle. All rights reserved.F - 20

Controlling Explicit Cursors: Declaring the Cursor

DECLARECURSOR c1 IS

SELECT employee_id, last_nameFROM employees;

CURSOR c2 ISSELECT *FROM departmentsWHERE department_id = 10;

BEGIN...

Declare the cursor

Explicit Cursor DeclarationRetrieve the employees one by one.

DECLAREv_empid employees.employee_id%TYPE;v_ename employees.last_name%TYPE;CURSOR c1 IS

SELECT employee_id, last_nameFROM employees;

BEGIN...

Note: You can reference variables in the query, but you must declare them before the CURSORstatement.

Page 342: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 21

Copyright © 2009, Oracle. All rights reserved.F - 21

Controlling Explicit Cursors: Opening the Cursor

• Open the cursor to execute the query and identify the active set.

• If the query returns no rows, no exception is raised.• Use cursor attributes to test the outcome after a fetch.

OPEN cursor_name;

Cursor pointer

Open the cursor

OPEN StatementOpen the cursor to execute the query and identify the result set, which consists of all rows that meet the query search criteria. The cursor now points to the first row in the result set.In the syntax, cursor_name is the name of the previously declared cursor.OPEN is an executable statement that performs the following operations:

1. Dynamically allocates memory for a context area that eventually contains crucial processing information

2. Parses the SELECT statement3. Binds the input variables—that is, sets the value for the input variables by obtaining their

memory addresses4. Identifies the result set—that is, the set of rows that satisfy the search criteria. Rows in the

result set are not retrieved into variables when the OPEN statement is executed. Rather, the FETCH statement retrieves the rows.

5. Positions the pointer just before the first row in the active setNote: If the query returns no rows when the cursor is opened, then PL/SQL does not raise an exception. However, you can test the cursor’s status after a fetch.For cursors declared by using the FOR UPDATE clause, the OPEN statement also locks those rows.

Page 343: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 22

Copyright © 2009, Oracle. All rights reserved.F - 22

Controlling Explicit Cursors:Fetching Data from the Cursor

FETCH c1 INTO v_empid, v_ename;

...OPEN defined_cursor;LOOP

FETCH defined_cursor INTO defined_variablesEXIT WHEN ...;...

-- Process the retrieved data...

END;

Fetch a rowCursor pointer

FETCH StatementYou use the FETCH statement to retrieve the current row values into output variables. After the fetch, you can manipulate the variables by further statements. For each column value returned by the query associated with the cursor, there must be a corresponding variable in the INTO list. Also, their data types must be compatible. Retrieve the first 10 employees one by one:

DECLAREv_empid employees.employee_id%TYPE;v_ename employees.last_name%TYPE;i NUMBER := 1;CURSOR c1 ISSELECT employee_id, last_nameFROM employees;

BEGINOPEN c1;FOR i IN 1..10 LOOPFETCH c1 INTO v_empid, v_ename;...

END LOOP;END;

Page 344: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 23

Copyright © 2009, Oracle. All rights reserved.F - 23

Controlling Explicit Cursors: Closing the Cursor

• Close the cursor after completing the processing of the rows.

• Reopen the cursor, if required.• Do not attempt to fetch data from a cursor after it has been

closed.

CLOSE cursor_name;

Close the cursor Cursor pointer

CLOSE StatementThe CLOSE statement disables the cursor, and the result set becomes undefined. Close the cursor after completing the processing of the SELECT statement. This step allows the cursor to be reopened, if required. Therefore, you can establish an active set several times.In the syntax, cursor_name is the name of the previously declared cursor.Do not attempt to fetch data from a cursor after it has been closed, or the INVALID_CURSORexception will be raised.Note: The CLOSE statement releases the context area. Although it is possible to terminate the PL/SQL block without closing cursors, you should always close any cursor that you declare explicitly in order to free up resources. There is a maximum limit to the number of open cursors per user, which is determined by the OPEN_CURSORS parameter in the database parameter field. By default, the maximum number of OPEN_CURSORS is 50.

...FOR i IN 1..10 LOOPFETCH c1 INTO v_empid, v_ename; ...

END LOOP;CLOSE c1;

END;

Page 345: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 24

Copyright © 2009, Oracle. All rights reserved.F - 24

Explicit Cursor Attributes

Obtain status information about a cursor.

NUMBER

BOOLEAN

BOOLEAN

BOOLEAN

Type

Evaluates to the total number of rows returned so far

%ROWCOUNT

Evaluates to TRUE if the most recent fetch returns a row; complement of %NOTFOUND

%FOUND

Evaluates to TRUE if the cursor is openISOPEN

%NOTFOUND

Attribute

Evaluates to TRUE if the most recent fetch does not return a row

Description

Explicit Cursor AttributesAs with implicit cursors, there are four attributes for obtaining status information about a cursor. When appended to the cursor or cursor variable, these attributes return useful information about the execution of a DML statement. Note: Do not reference cursor attributes directly in a SQL statement.

Page 346: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 25

Copyright © 2009, Oracle. All rights reserved.F - 25

Cursor FOR Loops: Example

Retrieve employees one by one until there are no more left.

DECLARECURSOR c1 IS

SELECT employee_id, last_nameFROM employees;

BEGINFOR emp_record IN c1 LOOP

-- implicit open and implicit fetch occurIF emp_record.employee_id = 134 THEN

...END LOOP; -- implicit close occurs

END;

Cursor FOR LoopsA cursor FOR loop processes rows in an explicit cursor. The cursor is opened, rows are fetched once for each iteration in the loop, and the cursor is closed automatically when all rows have been processed. The loop itself is terminated automatically at the end of the iteration where the last row was fetched. In the example in the slide, emp_record in the cursor for loop is an implicitly declared record that is used in the FOR LOOP construct.

Page 347: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 26

Copyright © 2009, Oracle. All rights reserved.F - 26

FOR UPDATE Clause: Example

Retrieve the orders for amounts over $1,000 that were processed today.

DECLARECURSOR c1 IS

SELECT customer_id, order_id FROM ordersWHERE order_date = SYSDATE

AND order_total > 1000.00ORDER BY customer_id FOR UPDATE NOWAIT;

FOR UPDATE ClauseIf the database server cannot acquire the locks on the rows it needs in a SELECT FOR UPDATE, then it waits indefinitely. You can use the NOWAIT clause in the SELECT FOR UPDATEstatement and test for the error code that returns due to failure to acquire the locks in a loop. Therefore, you can retry opening the cursor n times before terminating the PL/SQL block.If you intend to update or delete rows by using the WHERE CURRENT OF clause, you must specify a column name in the FOR UPDATE OF clause. If you have a large table, you can achieve better performance by using the LOCK TABLEstatement to lock all rows in the table. However, when using LOCK TABLE, you cannot use the WHERE CURRENT OF clause and must use the notation WHERE column = identifier.

Page 348: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 27

Copyright © 2009, Oracle. All rights reserved.F - 27

WHERE CURRENT OF Clause: Example

DECLARECURSOR c1 IS

SELECT salary FROM employeesFOR UPDATE OF salary NOWAIT;

BEGIN...FOR emp_record IN c1 LOOP

UPDATE ...WHERE CURRENT OF c1;

...END LOOP;COMMIT;

END;

WHERE CURRENT OF ClauseYou can update rows based on criteria from a cursor.Additionally, you can write your DELETE or UPDATE statement to contain the WHERE CURRENT OF cursor_name clause to refer to the latest row processed by the FETCHstatement. When you use this clause, the cursor you reference must exist and must contain the FOR UPDATE clause in the cursor query; otherwise, you get an error. This clause enables you to apply updates and deletes to the currently addressed row without the need to explicitly reference the ROWID pseudocolumn.

Page 349: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 28

Copyright © 2009, Oracle. All rights reserved.F - 28

Trapping Predefined Oracle 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

Trapping Predefined Oracle Server ErrorsTrap a predefined Oracle server error by referencing its standard name within the corresponding exception-handling routine.Note: PL/SQL declares predefined exceptions in the STANDARD package.It is a good idea to always consider the NO_DATA_FOUND and TOO_MANY_ROWS exceptions, which are the most common.

Page 350: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 29

Copyright © 2009, Oracle. All rights reserved.F - 29

BEGIN SELECT ... COMMIT;EXCEPTION

WHEN NO_DATA_FOUND THENstatement1; statement2;

WHEN TOO_MANY_ROWS THENstatement1;

WHEN OTHERS THENstatement1; statement2; statement3;

END;

Trapping PredefinedOracle Server Errors: Example

Trapping Predefined Oracle Server Exceptions: ExampleIn the example in the slide, a message is printed out to the user for each exception. Only one exception is raised and handled at any time.

Page 351: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 30

Copyright © 2009, Oracle. All rights reserved.F - 30

Non-Predefined Error

Trap for Oracle server error number –2292, which is an integrity constraint violation.

DECLAREe_products_invalid EXCEPTION;PRAGMA EXCEPTION_INIT (e_products_invalid, -2292);

v_message VARCHAR2(50);BEGIN. . .EXCEPTION

WHEN e_products_invalid THEN:g_message := 'Product IDspecified is not valid.';

. . .END;

1

2

3

Trapping a Non-Predefined Oracle Server Exception1. Declare the name for the exception within the declarative section.

Syntax

exception EXCEPTION;In this syntax, exception is the name of the exception.

2. Associate the declared exception with the standard Oracle server error number, using the PRAGMA EXCEPTION_INIT statement.Syntax

PRAGMA EXCEPTION_INIT(exception, error_number);In this syntax:

exception Is the previously declared exceptionerror_number Is a standard Oracle server error number

3. Reference the declared exception within the corresponding exception-handling routine.In the example in the slide: If there is product in stock, halt processing and print a message to the user.

Page 352: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 31

Copyright © 2009, Oracle. All rights reserved.F - 31

User-Defined Exceptions: Example

[DECLARE]e_amount_remaining EXCEPTION;

. . .BEGIN. . .

RAISE e_amount_remaining;. . .EXCEPTION

WHEN e_amount_remaining THEN:g_message := 'There is still an amount

in stock.';. . .END;

1

2

3

Trapping User-Defined ExceptionsYou trap a user-defined exception by declaring it and raising it explicitly.

1. Declare the name for the user-defined exception within the declarative section.Syntax: exception EXCEPTION;where: exception Is the name of the exception

2. Use the RAISE statement to raise the exception explicitly within the executable section.Syntax: RAISE exception;where: exception Is the previously declared exception

3. Reference the declared exception within the corresponding exception-handling routine.In the example in the slide: This customer has a business rule that states that a product cannot be removed from its database if there is any inventory left in stock for this product. Because there are no constraints in place to enforce this rule, the developer handles it explicitly in the application. Before performing a DELETE on the PRODUCT_INFORMATION table, the block queries the INVENTORIES table to see whether there is any stock for the product in question. If there is stock, raise an exception. Note: Use the RAISE statement by itself within an exception handler to raise the same exception back to the calling environment.

Page 353: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 32

Copyright © 2009, Oracle. All rights reserved.F - 32

RAISE_APPLICATION_ERROR Procedure

• Enables you to issue user-defined error messages from stored subprograms

• Is called from an executing stored subprogram only

raise_application_error (error_number,message[, {TRUE | FALSE}]);

RAISE_APPLICATION_ERROR ProcedureUse the RAISE_APPLICATION_ERROR procedure to communicate a predefined exception interactively by returning a nonstandard error code and error message. With RAISE_APPLICATION_ERROR, you can report errors to your application and avoid returning unhandled exceptions.In the syntax, error_number is a user-specified number for the exception between –20,000 and –20,999. The message is the user-specified message for the exception. It is a character string that is up to 2,048 bytes long.TRUE | FALSE is an optional Boolean parameter. If TRUE, the error is placed on the stack of previous errors. If FALSE (the default), the error replaces all previous errors.Example:

...EXCEPTIONWHEN NO_DATA_FOUND THEN

RAISE_APPLICATION_ERROR (-20201,'Manager is not a valid employee.');

END;

Page 354: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 33

Copyright © 2009, Oracle. All rights reserved.F - 33

RAISE_APPLICATION_ERROR Procedure

• Is used in two different places:– Executable section– Exception section

• Returns error conditions to the user in a manner consistent with other Oracle server errors

RAISE_APPLICATION_ERROR Procedure: Example...DELETE FROM employeesWHERE manager_id = v_mgr;IF SQL%NOTFOUND THEN

RAISE_APPLICATION_ERROR(-20202,'This is not a valid manager');

END IF;...

Page 355: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units F - 34

Copyright © 2009, Oracle. All rights reserved.F - 34

Summary

In this appendix, you should have learned how to:• Review the block structure for anonymous

PL/SQL blocks• Declare PL/SQL variables• Create PL/SQL records and tables• Insert, update, and delete data• Use IF, THEN, and ELSIF Statements• Use basic, FOR, and WHILE loops• Declare and use explicit cursors with parameters• Use cursor FOR loops and FOR UPDATE and WHERE

CURRENT OF clauses• Trap predefined and user-defined exceptions

SummaryThe Oracle server uses work areas to execute SQL statements and store processing information. You can use a PL/SQL construct called a cursor to name a work area and access its stored information. There are two kinds of cursors: implicit and explicit. PL/SQL implicitly declares a cursor for all SQL data manipulation statements, including queries that return only one row. For queries that return more than one row, you must explicitly declare a cursor to process the rows individually.Every explicit cursor and cursor variable has four attributes: %FOUND, %ISOPEN, %NOTFOUND, and %ROWCOUNT. When appended to the cursor variable name, these attributes return useful information about the execution of a SQL statement. You can use cursor attributes in procedural statements but not in SQL statements. Use simple loops or cursor FOR loops to operate on the multiple rows fetched by the cursor. Ifyou are using simple loops, you have to open, fetch, and close the cursor; however, cursor FORloops do this implicitly. If you are updating or deleting rows, lock the rows by using a FORUPDATE clause. This ensures that the data you are using is not updated by another session after you open the cursor. Use a WHERE CURRENT OF clause in conjunction with the FOR UPDATEclause to reference the current row fetched by the cursor.

Page 356: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Copyright © 2009, Oracle. All rights reserved.

Studies for Implementing Triggers

Page 357: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 2

Copyright © 2009, Oracle. All rights reserved.G - 2

Objectives

After completing this appendix, you should be able to do the following:• Enhance database security with triggers• Enforce data integrity with DML triggers• Maintain referential integrity using triggers• Use triggers to replicate data between tables• Use triggers to automate computation of derived data• Provide event-logging capabilities using triggers

Lesson AimIn this lesson, you learn to develop database triggers in order to enhance features that cannot otherwise be implemented by the Oracle server. In some cases, it may be sufficient to refrain from using triggers and accept the functionality provided by the Oracle server.This lesson covers the following business application scenarios:

• Security• Auditing• Data integrity• Referential integrity• Table replication• Computing derived data automatically• Event logging

Page 358: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 3

Copyright © 2009, Oracle. All rights reserved.G - 3

Controlling Security Within the Server

Using database security with the GRANT statement.

GRANT SELECT, INSERT, UPDATE, DELETEON employeesTO clerk; -- database roleGRANT clerk TO scott;

Controlling Security Within the ServerDevelop schemas and roles within the Oracle server to control the security of data operations on tables according to the identity of the user.

• Base privileges upon the username supplied when the user connects to the database.• Determine access to tables, views, synonyms, and sequences.• Determine query, data-manipulation, and data-definition privileges.

Page 359: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 4

Copyright © 2009, Oracle. All rights reserved.G - 4

CREATE OR REPLACE TRIGGER secure_empBEFORE INSERT OR UPDATE OR DELETE ON employees

DECLAREdummy PLS_INTEGER;BEGINIF (TO_CHAR (SYSDATE, 'DY') IN ('SAT','SUN')) THEN

RAISE_APPLICATION_ERROR(-20506,'You may onlychange data during normal business hours.');

END IF;SELECT COUNT(*) INTO dummy FROM holidayWHERE holiday_date = TRUNC (SYSDATE);IF dummy > 0 THEN

RAISE_APPLICATION_ERROR(-20507,'You may not change data on a holiday.');

END IF;END;/

Controlling Security with a Database Trigger

Controlling Security with a Database Trigger Develop triggers to handle more complex security requirements.

• Base privileges on any database values, such as the time of day, the day of the week, and so on.

• Determine access to tables only.• Determine data-manipulation privileges only.

Page 360: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 5

Copyright © 2009, Oracle. All rights reserved.G - 5

ALTER TABLE employees ADDCONSTRAINT ck_salary CHECK (salary >= 500);

Enforcing Data Integrity Within the Server

Enforcing Data Integrity Within the ServerYou can enforce data integrity within the Oracle server and develop triggers to handle more complex data integrity rules.The standard data integrity rules are not null, unique, primary key, and foreign key.Use these rules to:

• Provide constant default values• Enforce static constraints• Enable and disable dynamically

ExampleThe code sample in the slide ensures that the salary is at least $500.

Page 361: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 6

Copyright © 2009, Oracle. All rights reserved.G - 6

CREATE OR REPLACE TRIGGER check_salaryBEFORE UPDATE OF salary ON employeesFOR EACH ROWWHEN (NEW.salary < OLD.salary)

BEGINRAISE_APPLICATION_ERROR (-20508,

'Do not decrease salary.');END;/

Protecting Data Integrity with a Trigger

Protecting Data Integrity with a TriggerProtect data integrity with a trigger and enforce nonstandard data integrity checks.

• Provide variable default values.• Enforce dynamic constraints.• Enable and disable dynamically.• Incorporate declarative constraints within the definition of a table to protect data integrity.

ExampleThe code sample in the slide ensures that the salary is never decreased.

Page 362: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 7

Copyright © 2009, Oracle. All rights reserved.G - 7

ALTER TABLE employees ADD CONSTRAINT emp_deptno_fkFOREIGN KEY (department_id)

REFERENCES departments(department_id)ON DELETE CASCADE;

Enforcing Referential Integrity Within the Server

Enforcing Referential Integrity Within the ServerIncorporate referential integrity constraints within the definition of a table to prevent data inconsistency and enforce referential integrity within the server.

• Restrict updates and deletes.• Cascade deletes.• Enable and disable dynamically.

ExampleWhen a department is removed from the DEPARTMENTS parent table, cascade the deletion to the corresponding rows in the EMPLOYEES child table.

Page 363: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 8

Copyright © 2009, Oracle. All rights reserved.G - 8

CREATE OR REPLACE TRIGGER cascade_updatesAFTER UPDATE OF department_id ON departmentsFOR EACH ROWBEGINUPDATE employeesSET employees.department_id=:NEW.department_idWHERE employees.department_id=:OLD.department_id;

UPDATE job_historySET department_id=:NEW.department_idWHERE department_id=:OLD.department_id;

END;/

Protecting Referential Integrity with a Trigger

Protecting Referential Integrity with a TriggerThe following referential integrity rules are not supported by declarative constraints:

• Cascade updates.• Set to NULL for updates and deletions.• Set to a default value on updates and deletions.• Enforce referential integrity in a distributed system.• Enable and disable dynamically.

You can develop triggers to implement these integrity rules.ExampleEnforce referential integrity with a trigger. When the value of DEPARTMENT_ID changes in the DEPARTMENTS parent table, cascade the update to the corresponding rows in the EMPLOYEESchild table. For a complete referential integrity solution using triggers, a single trigger is not enough.

Page 364: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 9

Copyright © 2009, Oracle. All rights reserved.G - 9

CREATE MATERIALIZED VIEW emp_copyNEXT sysdate + 7 AS SELECT * FROM employees@ny;

Replicating a Table Within the Server

Creating a Materialized ViewMaterialized views enable you to maintain copies of remote data on your local node for replication purposes. You can select data from a materialized view as you would from a normal database table or view. A materialized view is a database object that contains the results of a query, or a copy of some database on a query. The FROM clause of the query of a materialized view can name tables, views, and other materialized views.When a materialized view is used, replication is performed implicitly by the Oracle server. This performs better than using user-defined PL/SQL triggers for replication. Materialized views:

• Copy data from local and remote tables asynchronously, at user-defined intervals• Can be based on multiple master tables• Are read-only by default, unless using the Oracle Advanced Replication feature• Improve the performance of data manipulation on the master table

Alternatively, you can replicate tables using triggers.The example in the slide creates a copy of the remote EMPLOYEES table from New York. The NEXT clause specifies a date-time expression for the interval between automatic refreshes.

Page 365: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 10

Copyright © 2009, Oracle. All rights reserved.G - 10

CREATE OR REPLACE TRIGGER emp_replicaBEFORE INSERT OR UPDATE ON employees FOR EACH ROWBEGIN /* Proceed if user initiates data operation,

NOT through the cascading trigger.*/IF INSERTING THENIF :NEW.flag IS NULL THEN

INSERT INTO employees@sf VALUES(:new.employee_id,...,'B');:NEW.flag := 'A';

END IF;ELSE /* Updating. */IF :NEW.flag = :OLD.flag THEN

UPDATE employees@sf SET ename=:NEW.last_name,...,flag=:NEW.flagWHERE employee_id = :NEW.employee_id;

END IF;IF :OLD.flag = 'A' THEN :NEW.flag := 'B';

ELSE :NEW.flag := 'A'; END IF;END IF;

END;

Replicating a Table with a Trigger

Replicating a Table with a TriggerYou can replicate a table with a trigger. By replicating a table, you can:

• Copy tables synchronously, in real time• Base replicas on a single master table• Read from replicas as well as write to them

Note: Excessive use of triggers can impair the performance of data manipulation on the master table, particularly if the network fails.ExampleIn New York, replicate the local EMPLOYEES table to San Francisco.

Page 366: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 11

Copyright © 2009, Oracle. All rights reserved.G - 11

Computing Derived Data Within the Server

UPDATE departmentsSET total_sal=(SELECT SUM(salary)

FROM employeesWHERE employees.department_id =

departments.department_id);

Computing Derived Data Within the ServerBy using the server, you can schedule batch jobs or use the database Scheduler for the following scenarios:

• Compute derived column values asynchronously, at user-defined intervals.• Store derived values only within database tables.• Modify data in one pass to the database and calculate derived data in a second pass.

Alternatively, you can use triggers to keep running computations of derived data.ExampleKeep the salary total for each department within a special TOTAL_SALARY column of the DEPARTMENTS table.

Page 367: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 12

Copyright © 2009, Oracle. All rights reserved.G - 12

CREATE PROCEDURE increment_salary(id NUMBER, new_sal NUMBER) IS

BEGINUPDATE departmentsSET total_sal = NVL (total_sal, 0)+ new_salWHERE department_id = id;

END increment_salary;CREATE OR REPLACE TRIGGER compute_salaryAFTER INSERT OR UPDATE OF salary OR DELETEON employees FOR EACH ROWBEGINIF DELETING THEN increment_salary(

:OLD.department_id,(-1*:OLD.salary));ELSIF UPDATING THEN increment_salary(

:NEW.department_id,(:NEW.salary-:OLD.salary));ELSE increment_salary(

:NEW.department_id,:NEW.salary); --INSERTEND IF;END;

Computing Derived Values with a Trigger

Computing Derived Data Values with a TriggerBy using a trigger, you can perform the following tasks:

• Compute derived columns synchronously, in real time.• Store derived values within database tables or within package global variables.• Modify data and calculate derived data in a single pass to the database.

ExampleKeep a running total of the salary for each department in the special TOTAL_SALARY column of the DEPARTMENTS table.

Page 368: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 13

Copyright © 2009, Oracle. All rights reserved.G - 13

CREATE OR REPLACE TRIGGER notify_reorder_repBEFORE UPDATE OF quantity_on_hand, reorder_point ON inventories FOR EACH ROWDECLAREdsc product_descriptions.product_description%TYPE;msg_text VARCHAR2(2000);BEGIN

IF :NEW.quantity_on_hand <=:NEW.reorder_point THEN SELECT product_description INTO dscFROM product_descriptions WHERE product_id = :NEW.product_id;

_ msg_text := 'ALERT: INVENTORY LOW ORDER:'||'Yours,' ||CHR(10) ||user || '.'|| CHR(10);

ELSIF :OLD.quantity_on_hand >= :NEW.quantity_on_hand THEN

msg_text := 'Product #'||... CHR(10); END IF;UTL_MAIL.SEND('[email protected]','[email protected]',message=>msg_text, subject=>'Inventory Notice');

END;

Logging Events with a Trigger

Logging Events with a TriggerIn the server, you can log events by querying data and performing operations manually. This sends an email message when the inventory for a particular product has fallen below the acceptable limit. This trigger uses the Oracle-supplied package UTL_MAIL to send the email message.Logging Events Within the Server

1. Query data explicitly to determine whether an operation is necessary.2. Perform the operation, such as sending a message.

Using Triggers to Log Events1. Perform operations implicitly, such as firing off an automatic electronic memo.2. Modify data and perform its dependent operation in a single step.3. Log events automatically as data is changing.

Page 369: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 14

Logging Events with a Trigger (continued)Logging Events TransparentlyIn the trigger code:

• CHR(10) is a carriage return• Reorder_point is not NULL• Another transaction can receive and read the message in the pipe

ExampleCREATE OR REPLACE TRIGGER notify_reorder_repBEFORE UPDATE OF amount_in_stock, reorder_point ON inventory FOR EACH ROWDECLAREdsc product.descrip%TYPE;msg_text VARCHAR2(2000);

BEGINIF :NEW.amount_in_stock <= :NEW.reorder_point THEN SELECT descrip INTO dscFROM PRODUCT WHERE prodid = :NEW.product_id;msg_text := 'ALERT: INVENTORY LOW ORDER:'||CHR(10)||'It has come to my personal attention that, due to recent'||CHR(10)||'transactions, our inventory for product # '||TO_CHAR(:NEW.product_id)||'-- '|| dsc ||' -- has fallen below acceptable levels.' || CHR(10) ||'Yours,' ||CHR(10) ||user || '.'|| CHR(10)|| CHR(10);

ELSIF :OLD.amount_in_stock >= :NEW.amount_in_stock THENmsg_text := 'Product #'|| TO_CHAR(:NEW.product_id)||' ordered. '|| CHR(10)|| CHR(10);

END IF;UTL_MAIL.SEND('[email protected]', '[email protected]',

message => msg_text, subject => 'Inventory Notice');END;

Page 370: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units G - 15

Copyright © 2009, Oracle. All rights reserved.G - 15

Summary

In this appendix, you should have learned how to:• Enhance database security with triggers• Enforce data integrity with DML triggers• Maintain referential integrity using triggers• Use triggers to replicate data between tables• Use triggers to automate computation of derived data• Provide event-logging capabilities using triggers

SummaryThis lesson provides some detailed comparison of using the Oracle database server functionality to implement security, auditing, data integrity, replication, and logging. The lesson also covers how database triggers can be used to implement the same features but go further to enhance the features that the database server provides. In some cases, you must use a trigger to perform some activities (such as computation of derived data) because the Oracle server cannot know how to implement this kind of business rule without some programming effort.

Page 371: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2
Page 372: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Copyright © 2009, Oracle. All rights reserved.

Using the DBMS_SCHEDULERand HTP Packages

Page 373: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 2

Copyright © 2009, Oracle. All rights reserved.H - 2

Objectives

After completing this appendix, you should be able to do the following:• Use the HTP package to generate a simple Web page• Call the DBMS_SCHEDULER package to schedule PL/SQL

code for execution

Lesson AimIn this lesson, you learn how to use some of the Oracle-supplied packages and their capabilities. This lesson focuses on the packages that generate Web-based output and the provided scheduling capabilities.

Page 374: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 3

Copyright © 2009, Oracle. All rights reserved.H - 3

Generating Web Pages with the HTP Package

• The HTP package procedures generate HTML tags.• The HTP package is used to generate HTML documents

dynamically and can be invoked from:– A browser using Oracle HTTP Server and PL/SQL Gateway

(mod_plsql) services– A SQL*Plus script to display HTML output

Web client

Oracle HTTPServer

Buffer

SQL script

GeneratedHTML

mod_plsql

Oracledatabase

BufferHTP

Generating Web Pages with the HTP PackageThe HTP package contains procedures that are used to generate HTML tags. The HTML tags that are generated typically enclose the data provided as parameters to the various procedures. The slide illustrates two ways in which the HTP package can be used:

• Most likely your procedures are invoked by the PL/SQL Gateway services, via the mod_plsql component supplied with Oracle HTTP Server, which is part of the Oracle Application Server product (represented by solid lines in the graphic).

• Alternatively (as represented by dotted lines in the graphic), your procedure can be called from SQL*Plus that can display the generated HTML output, which can be copied and pasted to a file. This technique is used in this course because Oracle Application Server software is not installed as a part of the course environment.

Note: The HTP procedures output information to a session buffer held in the database server. In the Oracle HTTP Server context, when the procedure completes, the mod_plsql component automatically receives the buffer contents, which are then returned to the browser as the HTTP response. In SQL*Plus, you must manually execute:

• A SET SERVEROUTPUT ON command• The procedure to generate the HTML into the buffer• The OWA_UTIL.SHOWPAGE procedure to display the buffer contents

Page 375: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 4

Copyright © 2009, Oracle. All rights reserved.H - 4

Using the HTP Package Procedures

• Generate one or more HTML tags. For example:

• Are used to create a well-formed HTML document:

htp.bold('Hello'); -- <B>Hello</B>htp.print('Hi <B>World</B>'); -- Hi <B>World</B>

BEGIN -- Generates:htp.htmlOpen; --------->htp.headOpen; --------->htp.title('Welcome'); -->htp.headClose; --------->htp.bodyOpen; --------->htp.print('My home page');htp.bodyClose; --------->htp.htmlClose; --------->END;

<HTML><HEAD><TITLE>Welcome</TITLE></HEAD><BODY>My home page</BODY></HTML>

Using the HTP Package ProceduresThe HTP package is structured to provide a one-to-one mapping of a procedure to standard HTML tags. For example, to display bold text on a Web page, the text must be enclosed in the HTML tag pair <B> and </B>. The first code box in the slide shows how to generate the wordHello in HTML bold text by using the equivalent HTP package procedure—that is, HTP.BOLD. The HTP.BOLD procedure accepts a text parameter and ensures that it is enclosed in the appropriate HTML tags in the HTML output that is generated.The HTP.PRINT procedure copies its text parameter to the buffer. The example in the slide shows how the parameter supplied to the HTP.PRINT procedure can contain HTML tags. This technique is recommended only if you need to use HTML tags that cannot be generated by using the set of procedures provided in the HTP package.The second example in the slide provides a PL/SQL block that generates the basic form of an HTML document. The example serves to illustrate how each of the procedures generates the corresponding HTML line in the enclosed text box on the right.The benefit of using the HTP package is that you create well-formed HTML documents, eliminating the need to manually enter the HTML tags around each piece of data.Note: For information about all the HTP package procedures, refer to PL/SQL Packages and Types Reference.

Page 376: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 5

Copyright © 2009, Oracle. All rights reserved.H - 5

Creating an HTML File with SQL*Plus

To create an HTML file with SQL*Plus, perform the following steps:1. Create a SQL script with the following commands:

2. Load and execute the script in SQL*Plus, supplying values for substitution variables.

3. Select, copy, and paste the HTML text that is generated in the browser to an HTML file.

4. Open the HTML file in a browser.

SET SERVEROUTPUT ONACCEPT procname PROMPT "Procedure: "EXECUTE &procnameEXECUTE owa_util.showpageUNDEFINE proc

Creating an HTML File with SQL*PlusThe example in the slide shows the steps for generating HTML by using any procedure and saving the output into an HTML file. You should perform the following steps:

1. Turn on server output with the SET SERVEROUTPUT ON command. Without this, you receive exception messages when running procedures that have calls to the HTP package.

2. Execute the procedure that contains calls to the HTP package.Note: This does not produce output, unless the procedure has calls to the DBMS_OUTPUTpackage.

3. Execute the OWA_UTIL.SHOWPAGE procedure to display the text. This call actually displays the HTML content that is generated from the buffer.

The ACCEPT command prompts for the name of the procedure to execute. The call to OWA_UTIL.SHOWPAGE displays the HTML tags in the browser window. You can then copyand paste the generated HTML tags from the browser window into an HTML file, typically with an .htm or .html extension.Note: If you are using SQL*Plus, then you can use the SPOOL command to direct the HTML output directly to an HTML file.

Page 377: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 6

Copyright © 2009, Oracle. All rights reserved.H - 6

The DBMS_SCHEDULER Package

The database Scheduler comprises several components to enable jobs to be run. Use the DBMS_SCHEDULER package to create each job with:• A unique job name• A program (“what” should be executed)• A schedule (“when” it should run)

Program

Window

Arguments Arguments

Job class

ScheduleJob

The DBMS_SCHEDULER PackageOracle Database provides a collection of subprograms in the DBMS_SCHEDULER package to simplify management and to provide a rich set of functionality for complex scheduling tasks. Collectively, these subprograms are called the Scheduler and can be called from any PL/SQL program. The Scheduler enables database administrators and application developers to control when and where various tasks take place. By ensuring that many routine database tasks occur without manual intervention, you can lower operating costs, implement more reliable routines, and minimize human error.The diagram shows the following architectural components of the Scheduler:

• A job is the combination of a program and a schedule. Arguments required by the program can be provided with the program or the job. All job names have the format [schema.]name. When you create a job, you specify the job name, a program, a schedule, and (optionally) job characteristics that can be provided through a job class.

• A program determines what should be run. Every automated job involves a particular executable, whether it is a PL/SQL block, a stored procedure, a native binary executable, or a shell script. A program provides metadata about a particular executable and may require a list of arguments.

• A schedule specifies when and how many times a job is executed.

Page 378: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 7

The DBMS_SCHEDULER Package (continued)• A job class defines a category of jobs that share common resource usage requirements and

other characteristics. At any given time, each job can belong to only a single job class. A job class has the following attributes:

- A database service name. The jobs in the job class will have an affinity to the particular service specified—that is, the jobs will run on the instances that cater to the specified service.

- A resource consumer group, which classifies a set of user sessions that have common resource-processing requirements. At any given time, a user session or job class can belong to a single resource consumer group. The resource consumer group that the job class associates with determines the resources that are allocated to the job class.

• A window is represented by an interval of time with a well-defined beginning and end, and is used to activate different resource plans at different times.

The slide focuses on the job component as the primary entity. However, a program, a schedule, a window, and a job class are components that can be created as individual entities that can be associated with a job to be executed by the Scheduler. When a job is created, it may contain all the information needed inline—that is, in the call that creates the job. Alternatively, creating a job may reference a program or schedule component that was previously defined. Examples of this are discussed on the next few pages.For more information about the Scheduler, see the online course titled Oracle Database 11g: Configure and Manage Jobs with the Scheduler.

Page 379: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 8

Copyright © 2009, Oracle. All rights reserved.H - 8

Creating a Job

• A job can be created in several ways by using a combination of inline parameters, named Programs, and named Schedules.

• You can create a job with the CREATE_JOB procedure by:– Using inline information with the “what” and the schedule

specified as parameters– Using a named (saved) program and specifying the schedule

inline– Specifying what should be done inline and using a named

Schedule– Using named Program and Schedule components

Creating a JobThe component that causes something to be executed at a specified time is called a job. Use the DBMS_SCHEDULER.CREATE_JOB procedure of the DBMS_SCHEDULER package to create a job, which is in a disabled state by default. A job becomes active and scheduled when it is explicitly enabled. To create a job, you:

• Provide a name in the format [schema.]name• Need the CREATE JOB privilege

Note: A user with the CREATE ANY JOB privilege can create a job in any schema except the SYS schema. Associating a job with a particular class requires the EXECUTE privilege for that class.In simple terms, a job can be created by specifying all the job details—the program to be executed (what) and its schedule (when)—in the arguments of the CREATE_JOB procedure. Alternatively, you can use predefined Program and Schedule components. If you have a named Program and Schedule, then these can be specified or combined with inline arguments for maximum flexibility in the way a job is created.A simple logical check is performed on the schedule information (that is, checking the date parameters when a job is created). The database checks whether the end date is after the start date. If the start date refers to a time in the past, then the start date is changed to the current date.

Page 380: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 9

Copyright © 2009, Oracle. All rights reserved.H - 9

Creating a Job with Inline Parameters

Specify the type of code, code, start time, and frequency of thejob to be run in the arguments of the CREATE_JOB procedure.

-- Schedule a PL/SQL block every hour:

BEGINDBMS_SCHEDULER.CREATE_JOB(job_name => 'JOB_NAME',job_type => 'PLSQL_BLOCK',job_action => 'BEGIN ...; END;',start_date => SYSTIMESTAMP,repeat_interval=>'FREQUENCY=HOURLY;INTERVAL=1',enabled => TRUE);

END;/

Creating a Job with Inline ParametersYou can create a job to run a PL/SQL block, stored procedure, or external program by using the DBMS_SCHEDULER.CREATE_JOB procedure. The CREATE_JOB procedure can be used directly without requiring you to create Program or Schedule components. The example in the slide shows how you can specify all the job details inline. The parameters of the CREATE_JOB procedure define “what” is to be executed, the schedule, and other job attributes. The following parameters define what is to be executed:

• The job_type parameter can be one of the following three values:- PLSQL_BLOCK for any PL/SQL block or SQL statement. This type of job cannot

accept arguments.- STORED_PROCEDURE for any stored stand-alone or packaged procedure. The

procedures can accept arguments that are supplied with the job.- EXECUTABLE for an executable command-line operating system application

• The schedule is specified by using the following parameters:- The start_date accepts a time stamp, and the repeat_interval is string-

specified as a calendar or PL/SQL expression. An end_date can be specified.Note: String expressions that are specified for repeat_interval are discussed later. The example specifies that the job should run every hour.

Page 381: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 10

Copyright © 2009, Oracle. All rights reserved.H - 10

Creating a Job Using a Program

• Use CREATE_PROGRAM to create a program:

• Use overloaded CREATE_JOB procedure with its program_name parameter:

BEGINDBMS_SCHEDULER.CREATE_JOB('JOB_NAME',program_name => 'PROG_NAME',start_date => SYSTIMESTAMP,repeat_interval => 'FREQ=DAILY',enabled => TRUE);

END;

BEGINDBMS_SCHEDULER.CREATE_PROGRAM(program_name => 'PROG_NAME',program_type => 'PLSQL_BLOCK',program_action => 'BEGIN ...; END;');

END;

Creating a Job Using a ProgramThe DBMS_SCHEDULER.CREATE_PROGRAM procedure defines a program that must be assigned a unique name. Creating the program separately for a job enables you to:

• Define the action once and then reuse this action within multiple jobs• Change the schedule for a job without having to re-create the PL/SQL block• Change the program executed without changing all the jobs

The program action string specifies a procedure, executable name, or PL/SQL block depending on the value of the program_type parameter, which can be:• PLSQL_BLOCK to execute an anonymous block or SQL statement• STORED_PROCEDURE to execute a stored procedure, such as PL/SQL, Java, or C• EXECUTABLE to execute operating system command-line programs

The example shown in the slide demonstrates calling an anonymous PL/SQL block. You can also call an external procedure within a program, as in the following example:

DBMS_SCHEDULER.CREATE_PROGRAM(program_name => 'GET_DATE',

program_action => '/usr/local/bin/date',program_type => 'EXECUTABLE');

To create a job with a program, specify the program name in the program_name argument in the call to the DBMS_SCHEDULER.CREATE_JOB procedure, as shown in the slide.

Page 382: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 11

Copyright © 2009, Oracle. All rights reserved.H - 11

Creating a Job for a Program with Arguments

• Create a program:

• Define an argument:

• Create a job specifying the number of arguments:

DBMS_SCHEDULER.DEFINE_PROGRAM_ARGUMENT(program_name => 'PROG_NAME',argument_name => 'DEPT_ID',argument_position=> 1, argument_type=> 'NUMBER',default_value => '50');

DBMS_SCHEDULER.CREATE_PROGRAM(program_name => 'PROG_NAME',program_type => 'STORED_PROCEDURE',program_action => 'EMP_REPORT');

DBMS_SCHEDULER.CREATE_JOB('JOB_NAME', program_name=> 'PROG_NAME', start_date => SYSTIMESTAMP,repeat_interval => 'FREQ=DAILY',number_of_arguments => 1, enabled => TRUE);

Creating a Job for a Program with ArgumentsPrograms, such as PL/SQL or external procedures, may require input arguments. Using the DBMS_SCHEDULER.DEFINE_PROGRAM_ARGUMENT procedure, you can define an argument for an existing program. The DEFINE_PROGRAM_ARGUMENT procedure parameters include the following:• program_name specifies an existing program that is to be altered.• argument_name specifies a unique argument name for the program.• argument_position specifies the position in which the argument is passed when the

program is called.• argument_type specifies the data type of the argument value that is passed to the

called program.• default_value specifies a default value that is supplied to the program if the job that

schedules the program does not provide a value.The slide shows how to create a job executing a program with one argument. The program argument default value is 50. To change the program argument value for a job, use:

DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(job_name => 'JOB_NAME',argument_name => 'DEPT_ID', argument_value => '80');

Page 383: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 12

Copyright © 2009, Oracle. All rights reserved.H - 12

Creating a Job Using a Schedule

• Use CREATE_SCHEDULE to create a schedule:

• Use CREATE_JOB by referencing the schedule in the schedule_name parameter:BEGIN

DBMS_SCHEDULER.CREATE_JOB('JOB_NAME',schedule_name => 'SCHED_NAME',job_type => 'PLSQL_BLOCK',job_action => 'BEGIN ...; END;',enabled => TRUE);

END;

BEGINDBMS_SCHEDULER.CREATE_SCHEDULE('SCHED_NAME',start_date => SYSTIMESTAMP,repeat_interval => 'FREQ=DAILY',end_date => SYSTIMESTAMP +15);

END;

Creating a Job Using a ScheduleYou can create a common schedule that can be applied to different jobs without having to specify the schedule details each time. The following are the benefits of creating a schedule:

• It is reusable and can be assigned to different jobs.• Changing the schedule affects all jobs using the schedule. The job schedules are changed

once, not multiple times.A schedule is precise to only the nearest second. Although the TIMESTAMP data type is more accurate, the Scheduler rounds off anything with a higher precision to the nearest second.The start and end times for a schedule are specified by using the TIMESTAMP data type. The end_date for a saved schedule is the date after which the schedule is no longer valid. The schedule in the example is valid for 15 days after using it with a specified job.The repeat_interval for a saved schedule must be created by using a calendaring expression. A NULL value for repeat_interval specifies that the job runs only once.Note: You cannot use PL/SQL expressions to express the repeat interval for a saved schedule.

Page 384: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 13

Copyright © 2009, Oracle. All rights reserved.H - 13

Setting the Repeat Interval for a Job

• Using a calendaring expression:

• Using a PL/SQL expression:

repeat_interval=> 'FREQ=HOURLY; INTERVAL=4'repeat_interval=> 'FREQ=DAILY'repeat_interval=> 'FREQ=MINUTELY;INTERVAL=15'repeat_interval=> 'FREQ=YEARLY;

BYMONTH=MAR,JUN,SEP,DEC;BYMONTHDAY=15'

repeat_interval=> 'SYSDATE + 36/24'repeat_interval=> 'SYSDATE + 1'repeat_interval=> 'SYSDATE + 15/(24*60)'

Creating a Job Using a ScheduleYou can create a common schedule that can be applied to different jobs without having to specify the schedule details each time. The following are the benefits of creating a schedule:

• It is reusable and can be assigned to different jobs.• Changing the schedule affects all jobs using the schedule. The job schedules are changed

once, not multiple times.A schedule is precise to only the nearest second. Although the TIMESTAMP data type is more accurate, the Scheduler rounds off anything with a higher precision to the nearest second.The start and end times for a schedule are specified by using the TIMESTAMP data type. The end_date for a saved schedule is the date after which the schedule is no longer valid. The schedule in the example is valid for 15 days after being used with a specified job.The repeat_interval for a saved schedule must be created by using a calendaring expression. A NULL value for repeat_interval specifies that the job runs only once.Note: You cannot use PL/SQL expressions to express the repeat interval for a saved schedule.

Page 385: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 14

Copyright © 2009, Oracle. All rights reserved.H - 14

Creating a Job Using a Named Program and Schedule

• Create a named program called PROG_NAME by using the CREATE_PROGRAM procedure.

• Create a named schedule called SCHED_NAME by using the CREATE_SCHEDULE procedure.

• Create a job referencing the named program and schedule:

BEGINDBMS_SCHEDULER.CREATE_JOB('JOB_NAME',program_name => 'PROG_NAME',schedule_name => 'SCHED_NAME',enabled => TRUE);

END;/

Creating a Job Using a Named Program and ScheduleThe example in the slide shows the final form for using the DBMS_SCHEDULER.CREATE_JOB procedure. In this example, the named program (PROG_NAME) and schedule (SCHED_NAME) are specified in their respective parameters in the call to the DBMS_SCHEDULER.CREATE_JOB procedure. With this example, you can see how easy it is to create jobs by using a predefined program and schedule.Some jobs and schedules can be too complex to cover in this course. For example, you can create windows for recurring time plans and associate a resource plan with a window. A resource plan defines attributes about the resources required during the period defined by execution window.For more information, refer to the online course titled Oracle Database 11g: Configure and Manage Jobs with the Scheduler.

Page 386: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 15

Copyright © 2009, Oracle. All rights reserved.H - 15

Managing Jobs

• Run a job:

• Stop a job:

• Drop a job even if it is currently running:

DBMS_SCHEDULER.RUN_JOB('SCHEMA.JOB_NAME');

DBMS_SCHEDULER.STOP_JOB('SCHEMA.JOB_NAME');

DBMS_SCHEDULER.DROP_JOB('JOB_NAME', TRUE);

Managing JobsAfter a job has been created, you can:

• Run the job by calling the RUN_JOB procedure specifying the name of the job. The job is immediately executed in your current session.

• Stop the job by using the STOP_JOB procedure. If the job is running currently, it is stopped immediately. The STOP_JOB procedure has two arguments:- job_name: Is the name of the job to be stopped- force: Attempts to gracefully terminate a job. If this fails and force is set to

TRUE, then the job slave is terminated. (Default value is FALSE.) To use force, you must have the MANAGE SCHEDULER system privilege.

• Drop the job with the DROP_JOB procedure. This procedure has two arguments:- job_name: Is the name of the job to be dropped- force: Indicates whether the job should be stopped and dropped if it is currently

running (Default value is FALSE.)If the DROP_JOB procedure is called and the job specified is currently running, then the command fails unless the force option is set to TRUE. If the force option is set to TRUE, then any instance of the job that is running is stopped and the job is dropped.

Note: To run, stop, or drop a job that belongs to another user, you need ALTER privileges on that job or the CREATE ANY JOB system privilege.

Page 387: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 16

Copyright © 2009, Oracle. All rights reserved.H - 16

Data Dictionary Views

• [DBA | ALL | USER]_SCHEDULER_JOBS• [DBA | ALL | USER]_SCHEDULER_RUNNING_JOBS• [DBA | ALL]_SCHEDULER_JOB_CLASSES• [DBA | ALL | USER]_SCHEDULER_JOB_LOG• [DBA | ALL | USER]_SCHEDULER_JOB_RUN_DETAILS• [DBA | ALL | USER]_SCHEDULER_PROGRAMS

Data Dictionary ViewsThe DBA_SCHEDULER_JOB_LOG view shows all completed job instances, both successful and failed.To view the state of your jobs, use the following query:

SELECT job_name, program_name, job_type, state FROM USER_SCHEDULER_JOBS;

To determine which instance a job is running on, use the following query:SELECT owner, job_name, running_instance, resource_consumer_group FROM DBA_SCHEDULER_RUNNING_JOBS;

To determine information about how a job ran, use the following query:SELECT job_name, instance_id, req_start_date, actual_start_date, statusFROM ALL_SCHEDULER_JOB_RUN_DETAILS;

To determine the status of your jobs, use the following query:SELECT job_name, status, error#, run_duration, cpu_usedFROM USER_SCHEDULER_JOB_RUN_DETAILS;

Page 388: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units H - 17

Copyright © 2009, Oracle. All rights reserved.H - 17

Summary

In this appendix, you should have learned how to:• Use the HTP package to generate a simple Web page• Call the DBMS_SCHEDULER package to schedule PL/SQL

code for execution

SummaryThis lesson covers a small subset of packages provided with the Oracle database. You have extensively used DBMS_OUTPUT for debugging purposes and displaying procedurally generated information on the screen in SQL*Plus.In this lesson, you should have learned how to schedule PL/SQL and external code for execution with the DBMS_SCHEDULER package.Note: For more information about all PL/SQL packages and types, refer to PL/SQLPackages and Types Reference.

Page 389: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2
Page 390: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Appendix APAdditional Practices and Solutions

Page 391: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-2

Table of Contents

Practice 1............................................................................................................................. 3 Practice 1-1: Creating a New SQL Developer Database Connection............................. 4 Practice 1-2: Adding a New Job to the JOBS Table....................................................... 6 Practice 1-3: Adding a New Row to the JOB_HISTORY Table.................................... 7 Practice 1-4: Updating the Minimum and Maximum Salaries for a Job ........................ 8 Practice 1-5: Monitoring Employees Salaries................................................................. 9 Practice 1-6: Retrieving the Total Number of Years of Service for an Employee ....... 10 Practice 1-7: Retrieving the Total Number of Different Jobs for an Employee ........... 11 Practice 1-8: Creating a New Package that Contains the Newly Created Procedures and Functions....................................................................................................................... 12 Practice 1-9: Creating a Trigger to Ensure that the Employees� Salaries Are Within the Acceptable Range ......................................................................................................... 13 Practice Solutions: 1-1: Creating a New SQL Developer Database Connection.......... 14 Practice Solutions 1-2: Adding a New Job to the JOBS Table..................................... 16 Practice Solutions 1-3: Adding a New Row to the JOB_HISTORY Table.................. 19 Practice Solution 1-4: Updating the Minimum and Maximum Salaries for a Job........ 23 Practice Solutions 1-5: Monitoring Employees Salaries .............................................. 28 Practice Solutions 1-6: Retrieving the Total Number of Years of Service for an Employee ...................................................................................................................... 33 Practice Solutions 1-7: Retrieving the Total Number of Different Jobs for an Employee....................................................................................................................................... 37 Practice Solutions 1-8: Creating a New Package that Contains the Newly Created Procedures and Functions ............................................................................................. 40 Practice Solution 1-9: Creating a Trigger to Ensure that the Employees Salaries are Within the Acceptable Range ....................................................................................... 47

Practice 2........................................................................................................................... 51 Practice 2-1: Creating the VIDEO_PKG Package........................................................ 52 Practice Solutions 2-1: Creating the VIDEO_PKG Package........................................ 54

Page 392: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-3

Practice 1

The additional practices are provided as a supplement to the course Oracle Database 11g: Develop PL/SQL Program Units. In these practices, you apply the concepts that you learned in the course. The additional practices comprise two lessons. Lesson 1 provides supplemental exercises to create stored procedures, functions, packages, and triggers, and to use the Oracle-supplied packages with SQL Developer or SQL*Plus as the development environment. The tables used in this portion of the additional practice include ÛÓÐÔÑÇÛÛÍ, ÖÑÞÍ, ÖÑÞÁØ×ÍÌÑÎÇ, and ÜÛÐßÎÌÓÛÒÌÍ. An entity relationship diagram is provided at the start of each practice. Each entity relationship diagram displays the table entities and their relationships. More detailed definitions of the tables and the data contained in them is provided in the appendix titled �Additional Practices: Table Descriptions and Data.� The Human Resources (HR) Schema Entity Relationship Diagram

Page 393: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-4

Practice 1-1: Creating a New SQL Developer Database Connection

In this practice, you start SQL Developer using your connection information and create a new database connection. Start up SQL Developer using the user ID and password that are provided to you by the instructor such as ±®¿êî.

1) Create a database connection using the following information:

a) Connection Name: Ê·¼»±Ý±³°¿²§

b) Username: ±®¿êî

c) Password: ±®¿êî

d) Hostname: Enter the host name for your PC e) Port: 1521

f) SID: ÑÎÝÔ

2) Test the new connection. If the Status is Success, connect to the database using this new connection:

a) Double-click the Ê·¼»±Ý±³°¿²§ icon on the Connections tabbed page.

b) Click the Test button in the New/Select Database Connection window. If the status is Success, click the Connect button.

Page 394: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 1-1: Creating a New SQL Developer Database Connection (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-5

Page 395: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-6

Practice 1-2: Adding a New Job to the ÖÑÞÍ Table

In this practice, you create a subprogram to add a new job into the ÖÑÞÍ table.

1) Create a stored procedure called ÒÛÉÁÖÑÞ to enter a new order into the ÖÑÞÍ table. The procedure should accept three parameters. The first and second parameters supply a job ID and a job title. The third parameter supplies the minimum salary. Use the maximum salary for the new job as twice the minimum salary supplied for the job ID.

2) Enable ÍÛÎÊÛÎÑËÌÐËÌ, and then invoke the procedure to add a new job with job ID 'ÍÇÁßÒßÔ', job title 'ͧ­¬»³ ß²¿´§­¬', and minimum salary of 6000.

3) Check whether a row was added and note the new job ID for use in the next exercise. Commit the changes.

Page 396: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-7

Practice 1-3: Adding a New Row to the ÖÑÞÁØ×ÍÌÑÎÇ Table

In this practice, you add a new row to the ÖÑÞÁØ×ÍÌÑÎÇ table for an existing employee.

1) Create a stored procedure called ßÜÜÁÖÑÞÁØ×ÍÌ to add a new row into the ÖÑÞÁØ×ÍÌÑÎÇ table for an employee who is changing his job to the new job ID ('ÍÇÁßÒßÔ') that you created in exercise 1 b.

a) The procedure should provide two parameters, one for the employee ID who is changing the job, and the second for the new job ID.

b) Read the employee ID from the ÛÓÐÔÑÇÛÛÍ table and insert it into the ÖÑÞÁØ×ÍÌÑÎÇ table.

c) Make the hire date of this employee as start date and today�s date as end date for this row in the ÖÑÞÁØ×ÍÌÑÎÇ table.

d) Change the hire date of this employee in the ÛÓÐÔÑÇÛÛÍ table to today�s date.

e) Update the job ID of this employee to the job ID passed as parameter (use the 'ÍÇÁßÒßÔ' job ID) and salary equal to the minimum salary for that job ID + 500. Note: Include exception handling to handle an attempt to insert a nonexistent employee.

2) Disable all triggers on the ÛÓÐÔÑÇÛÛÍ, ÖÑÞÍ, and ÖÑÞÁØ×ÍÌÑÎÇ tables before invoking the ßÜÜÁÖÑÞÁØ×ÍÌ procedure.

3) Enable ÍÛÎÊÛÎÑËÌÐËÌ, and then execute the procedure with employee ID 106 and job ID 'ÍÇÁßÒßÔ' as parameters.

4) Query the ÖÑÞÁØ×ÍÌÑÎÇ and ÛÓÐÔÑÇÛÛÍ tables to view your changes for employee 106, and then commit the changes.

5) Reenable the triggers on the ÛÓÐÔÑÇÛÛÍ, ÖÑÞÍ, and ÖÑÞÁØ×ÍÌÑÎÇ tables.

Page 397: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-8

Practice 1-4: Updating the Minimum and Maximum Salaries for a Job

In this practice, you create a program to update the minimum and maximum salaries for a job in the ÖÑÞÍ table.

1) Create a stored procedure called ËÐÜÁÖÑÞÍßÔ to update the minimum and maximum salaries for a specific job ID in the ÖÑÞÍ table. The procedure should provide three parameters: the job ID, a new minimum salary, and a new maximum salary. Add exception handling to account for an invalid job ID in the ÖÑÞÍ table. Raise an exception if the maximum salary supplied is less than the minimum salary, and provide a message that will be displayed if the row in the ÖÑÞÍ table is locked. Hint: The resource locked/busy error number is �54.

2) Enable ÍÛÎÊÛÎÑËÌÐËÌ, and then execute the ËÐÜÁÖÑÞÍßÔ procedure by using a job ID of 'ÍÇÁßÒßÔ', a minimum salary of 7000 and a maximum salary of 140. Note: This should generate an exception message.

3) Disable triggers on the ÛÓÐÔÑÇÛÛÍ and ÖÑÞÍ tables.

4) Execute the ËÐÜÁÖÑÞÍßÔ procedure using a job ID of 'ÍÇÁßÒßÔ', a minimum salary of 7000, and a maximum salary of 14000.

5) Query the ÖÑÞÍ table to view your changes, and then commit the changes.

6) Enable the triggers on the ÛÓÐÔÑÇÛÛÍ and ÖÑÞÍ tables.

Page 398: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-9

Practice 1-5: Monitoring Employees Salaries

In this practice, you create a procedure to monitor whether employees have exceeded their average salaries for their job type.

1) Disable the ÍÛÝËÎÛÁÛÓÐÔÑÇÛÛÍ trigger.

2) In the ÛÓÐÔÑÇÛÛÍ table, add an ÛÈÝÛÛÜÁßÊÙÍßÔ column to store up to three characters and a default value of ÒÑ. Use a check constraint to allow the values ÇÛÍ or ÒÑ.

3) Create a stored procedure called ÝØÛÝÕÁßÊÙÍßÔ that checks whether each employee�s salary exceeds the average salary for the ÖÑÞÁ×Ü.

a) The average salary for a job is calculated from the information in the ÖÑÞÍ table.

b) If the employee�s salary exceeds the average for his or her job, then update the ÛÈÝÛÛÜÁßÊÙÍßÔ column in the ÛÓÐÔÑÇÛÛÍ table to a value of ÇÛÍ; otherwise, set the value to ÒÑ.

c) Use a cursor to select the employee�s rows using the ÚÑÎ ËÐÜßÌÛ option in the query.

d) Add exception handling to account for a record being locked. Hint: The resource locked/busy error number is �ëì.

e) Write and use a local function called ÙÛÌÁÖÑÞÁßÊÙÍßÔ to determine the average salary for a job ID specified as a parameter.

4) Execute the ÝØÛÝÕÁßÊÙÍßÔ procedure. To view the results of your modifications, write a query to display the employee�s ID, job, the average salary for the job, the employee�s salary and the »¨½»»¼Á¿ª¹­¿´ indicator column for employees whose salaries exceed the average for their job, and finally commit the changes. Note: These exercises can be used for extra practice when discussing how to create functions.

Page 399: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-10

Practice 1-6: Retrieving the Total Number of Years of Service for an Employee

In this practice, you create a subprogram to retrieve the number of years of service for a specific employee.

1) Create a stored function called ÙÛÌÁÇÛßÎÍÁÍÛÎÊ×ÝÛ to retrieve the total number of years of service for a specific employee. The function should accept the employee ID as a parameter and return the number of years of service. Add error handling to account for an invalid employee ID.

2) Invoke the ÙÛÌÁÇÛßÎÍÁÍÛÎÊ×ÝÛ function in a call to ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ for an employee with ID 999.

3) Display the number of years of service for employee 106 with ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ invoking the ÙÛÌÁÇÛßÎÍÁÍÛÎÊ×ÝÛ function. Make sure to enable ÍÛÎÊÛÎÑËÌÐËÌ.

4) Query the ÖÑÞÁØ×ÍÌÑÎÇ and ÛÓÐÔÑÇÛÛÍ tables for the specified employee to verify that the modifications are accurate. The values represented in the results on this page may differ from those that you get when you run these queries.

Page 400: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-11

Practice 1-7: Retrieving the Total Number of Different Jobs for an Employee

In this practice, you create a program to retrieve the number of different jobs that an employee worked on during his or her service.

1) Create a stored function called ÙÛÌÁÖÑÞÁÝÑËÒÌ to retrieve the total number of different jobs on which an employee worked.

a) The function should accept the employee ID in a parameter, and return the number of different jobs that the employee worked on until now, including the present job.

b) Add exception handling to account for an invalid employee ID. Hint: Use the distinct job IDs from the ÖÑÞÁØ×ÍÌÑÎÇ table, and exclude the current job ID, if it is one of the job IDs on which the employee has already worked.

c) Write a ËÒ×ÑÒ of two queries and count the rows retrieved into a PL/SQL table.

d) Use a ÚÛÌÝØ with ÞËÔÕ ÝÑÔÔÛÝÌ ×ÒÌÑ to obtain the unique jobs for the employee.

2) Invoke the function for the employee with the ID of 176. Make sure to enable ÍÛÎÊÛÎÑËÌÐËÌ.

Note: These exercises can be used for extra practice when discussing how to create packages.

Page 401: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-12

Practice 1-8: Creating a New Package that Contains the Newly Created Procedures and Functions

In this practice, you create a package called ÛÓÐÖÑÞÁÐÕÙ that contains your ÒÛÉÁÖÑÞ, ßÜÜÁÖÑÞÁØ×ÍÌ, ËÐÜÁÖÑÞÍßÔ procedures, as well as your ÙÛÌÁÇÛßÎÍÁÍÛÎÊ×ÝÛ and ÙÛÌÁÖÑÞÁÝÑËÒÌ functions.

1) Create the package specification with all the subprogram constructs as public. Move any subprogram local-defined types into the package specification.

2) Create the package body with the subprogram implementation; remember to remove, from the subprogram implementations, any types that you moved into the package specification.

3) Invoke your ÛÓÐÖÑÞÁÐÕÙòÒÛÉÁÖÑÞ procedure to create a new job with the ID ÐÎÁÓßÒ, the job title Public Relations Manager, and the salary 6250. Make sure to enable ÍÛÎÊÛÎÑËÌÐËÌ.

4) Invoke your ÛÓÐÖÑÞÁÐÕÙòßÜÜÁÖÑÞÁØ×ÍÌ procedure to modify the job of employee ID 110 to job ID ÐÎÁÓßÒ. Note: You need to disable the ËÐÜßÌÛÁÖÑÞÁØ×ÍÌÑÎÇ trigger before you execute the ßÜÜÁÖÑÞÁØ×ÍÌ procedure, and reenable the trigger after you have executed the procedure.

5) Query the ÖÑÞÍ, ÖÑÞÁØ×ÍÌÑÎÇ, and ÛÓÐÔÑÇÛÛÍ tables to verify the results.

Note: These exercises can be used for extra practice when discussing how to create database triggers.

Page 402: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-13

Practice 1-9: Creating a Trigger to Ensure that the Employees� Salaries Are Within the Acceptable Range

In this practice, you create a trigger to ensure that the minimum and maximum salaries of a job are never modified such that the salary of an existing employee with that job ID is out of the new range specified for the job.

1) Create a trigger called ÝØÛÝÕÁÍßÔÁÎßÒÙÛ that is fired before every row that is updated in the Ó×ÒÁÍßÔßÎÇ and ÓßÈÁÍßÔßÎÇ columns in the ÖÑÞÍ table.

a) For any minimum or maximum salary value that is changed, check whether the salary of any existing employee with that job ID in the ÛÓÐÔÑÇÛÛÍ table falls within the new range of salaries specified for this job ID.

b) Include exception handling to cover a salary range change that affects the record of any existing employee.

2) Test the trigger using the ÍÇÁßÒßÔ job, setting the new minimum salary to 5000, and the new maximum salary to 7000. Before you make the change, write a query to display the current salary range for the ÍÇÁßÒßÔ job ID, and another query to display the employee ID, last name, and salary for the same job ID. After the update, query the change (if any) to the ÖÑÞÍ table for the specified job ID.

3) Using the ÍÇÁßÒßÔ job, set the new minimum salary to 7,000, and the new maximum salary to 18000. Explain the results.

Page 403: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-14

Practice Solutions: 1-1: Creating a New SQL Developer Database Connection

In this practice, you start SQL Developer using your connection information and create a new database connection. 1) Start up SQL Developer using the user ID and password that are provided to you by

the instructor such as ±®¿êî.

Click the SQL Developer icon on your desktop.

2) Create a database connection using the following information:

a) Connection Name: Ê·¼»±Ý±³°¿²§

b) Username: ±®¿êî

c) Password: ±®¿êî

d) Hostname: Enter the host name for your PC e) Port: 1521

f) SID: ÑÎÝÔ

Right-click the Connections icon on the Connections tabbed page, and then select the New Connection option from the shortcut menu. The New/Select Database Connection window is displayed. Use the preceding information provided to create the new database connection. Note: To display the properties of the newly created connection, right-click the connection name, and then select Properties from the shortcut menu. Substitute the username, password, host name, and service name with the appropriate information as provided by your instructor. The following is a sample of the newly created database connection for student ±®¿êî:

3) Test the new connection. If the status is Success, connect to the database using this new connection:

a) Double-click the Ê·¼»±Ý±³°¿²§ icon on the Connections tabbed page.

b) Click the Test button in the New/Select Database Connection window. If the status is Success, click the Connect button.

Page 404: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions: 1-1: Creating a New SQL Developer Database Connection (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-15

Page 405: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-16

Practice Solutions 1-2: Adding a New Job to the ÖÑÞÍ Table

In this practice, you create a subprogram to add a new job into the ÖÑÞÍ table.

1) Create a stored procedure called ÒÛÉÁÖÑÞ to enter a new order into the ÖÑÞÍ table. The procedure should accept three parameters. The first and second parameters supply a job ID and a job title. The third parameter supplies the minimum salary. Use the maximum salary for the new job as twice the minimum salary supplied for the job ID.

Open the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðîÁðïò­¯´ script. Click the Run Script (F5) icon on the SQL Worksheet toolbar to create and compile the procedure. When prompted to select a connection, select the new Ê·¼»±Ý±³°¿²§ connection. The code, connection prompt, and the results are displayed as follows: ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ ²»©Á¶±¾ø °Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼ûÌÇÐÛô °Á¬·¬´» ×Ò ¶±¾­ò¶±¾Á¬·¬´»ûÌÇÐÛô ªÁ³·²­¿´ ×Ò ¶±¾­ò³·²Á­¿´¿®§ûÌÇÐÛ÷ ×Í ªÁ³¿¨­¿´ ¶±¾­ò³¿¨Á­¿´¿®§ûÌÇÐÛ æã î ö ªÁ³·²­¿´å ÞÛÙ×Ò ×ÒÍÛÎÌ ×ÒÌÑ ¶±¾­ø¶±¾Á·¼ô ¶±¾Á¬·¬´»ô ³·²Á­¿´¿®§ô ³¿¨Á­¿´¿®§÷ ÊßÔËÛÍ ø°Á¶±¾·¼ô °Á¬·¬´»ô ªÁ³·²­¿´ô ªÁ³¿¨­¿´÷å ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ øùÒ»© ®±© ¿¼¼»¼ ¬± ÖÑÞÍ ¬¿¾´»æù÷å ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ ø°Á¶±¾·¼ ¤¤ ù ù ¤¤ °Á¬·¬´» ¤¤ù ù¤¤ ªÁ³·²­¿´ ¤¤ ù ù ¤¤ ªÁ³¿¨­¿´÷å ÛÒÜ ²»©Á¶±¾å ñ ÍØÑÉ ÛÎÎÑÎÍ

2) Enable ÍÛÎÊÛÎÑËÌÐËÌ, and then invoke the procedure to add a new job with job ID 'ÍÇÁßÒßÔ', job title 'ͧ­¬»³ ß²¿´§­¬', and minimum salary of 6000.

Page 406: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-2: Adding a New Job to the ÖÑÞÍ Table (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-17

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðîÁðîò­¯´ script. When prompted to select a connection, select the new Ê·¼»±Ý±³°¿²§ connection. The code, connection prompt, and the results are displayed as follows:

ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ ÛÈÛÝËÌÛ ²»©Á¶±¾ øùÍÇÁßÒßÔùô ùͧ­¬»³ ß²¿´§­¬ùô êððð÷

3) Check whether a row was added and note the new job ID for use in the next exercise. Commit the changes.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðîÁðíò­¯´ script. The code, connection prompt, and the results are displayed as follows:

ÍÛÔÛÝÌ ö ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã ùÍÇÁßÒßÔùå ÝÑÓÓ×Ìå

Page 407: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-2: Adding a New Job to the ÖÑÞÍ Table (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-18

Page 408: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-19

Practice Solutions 1-3: Adding a New Row to the ÖÑÞÁØ×ÍÌÑÎÇ Table

In this practice, you add a new row to the ÖÑÞÁØ×ÍÌÑÎÇ table for an existing employee.

1) Create a stored procedure called ßÜÜÁÖÑÞÁØ×ÍÌ to add a new row into the ÖÑÞÁØ×ÍÌÑÎÇ table for an employee who is changing his job to the new job ID ('ÍÇÁßÒßÔ') that you created in exercise 1 b.

a) The procedure should provide two parameters, one for the employee ID who is changing the job, and the second for the new job ID.

b) Read the employee ID from the ÛÓÐÔÑÇÛÛÍ table and insert it into the ÖÑÞÁØ×ÍÌÑÎÇ table.

c) Make the hire date of this employee as start date and today�s date as end date for this row in the ÖÑÞÁØ×ÍÌÑÎÇ table.

d) Change the hire date of this employee in the ÛÓÐÔÑÇÛÛÍ table to today�s date.

e) Update the job ID of this employee to the job ID passed as parameter (use the 'ÍÇÁßÒßÔ' job ID) and salary equal to the minimum salary for that job ID + 500. Note: Include exception handling to handle an attempt to insert a nonexistent employee.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðíÁðïò­¯´ script. The code, connection prompt, and the results are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ ¿¼¼Á¶±¾Á¸·­¬ø °Á»³°Á·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á²»©Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ×ÒÍÛÎÌ ×ÒÌÑ ¶±¾Á¸·­¬±®§ ÍÛÔÛÝÌ »³°´±§»»Á·¼ô ¸·®»Á¼¿¬»ô ÍÇÍÜßÌÛô ¶±¾Á·¼ô ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ËÐÜßÌÛ »³°´±§»»­ ÍÛÌ ¸·®»Á¼¿¬» ã ÍÇÍÜßÌÛô ¶±¾Á·¼ ã °Á²»©Á¶±¾·¼ô ­¿´¿®§ ã øÍÛÔÛÝÌ ³·²Á­¿´¿®§ õ ëðð ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã °Á²»©Á¶±¾·¼÷ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ øùß¼¼»¼ »³°´±§»» ù ¤¤ °Á»³°Á·¼ ¤¤ ù ¼»¬¿·´­ ¬± ¬¸» ÖÑÞÁØ×ÍÌÑÎÇ ¬¿¾´»ù÷å ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ øùË°¼¿¬»¼ ½«®®»²¬ ¶±¾ ±º »³°´±§»» ù ¤¤

Page 409: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-3: Adding a New Row to the ÖÑÞÁØ×ÍÌÑÎÇ Table (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-20

°Á»³°Á·¼¤¤ ù ¬± ù¤¤ °Á²»©Á¶±¾·¼÷å

ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðððïô ùÛ³°´±§»» ¼±»­ ²±¬ »¨·­¬ÿù÷å ÛÒÜ ¿¼¼Á¶±¾Á¸·­¬å ñ ÍØÑÉ ÛÎÎÑÎÍ

2) Disable all triggers on the ÛÓÐÔÑÇÛÛÍ, ÖÑÞÍ, and ÖÑÞÁØ×ÍÌÑÎÇ tables before invoking the ßÜÜÁÖÑÞÁØ×ÍÌ procedure.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðíÁðîò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ßÔÌÛÎ ÌßÞÔÛ »³°´±§»»­ Ü×ÍßÞÔÛ ßÔÔ ÌÎ×ÙÙÛÎÍå ßÔÌÛÎ ÌßÞÔÛ ¶±¾­ Ü×ÍßÞÔÛ ßÔÔ ÌÎ×ÙÙÛÎÍå ßÔÌÛÎ ÌßÞÔÛ ¶±¾Á¸·­¬±®§ Ü×ÍßÞÔÛ ßÔÔ ÌÎ×ÙÙÛÎÍå

Page 410: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-3: Adding a New Row to the ÖÑÞÁØ×ÍÌÑÎÇ Table (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-21

3) Enable ÍÛÎÊÛÎÑËÌÐËÌ, and then execute the procedure with employee ID 106 and job ID 'ÍÇÁßÒßÔ' as parameters.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðíÁðíò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ ÛÈÛÝËÌÛ ¿¼¼Á¶±¾Á¸·­¬øïðêô ùÍÇÁßÒßÔù÷

4) Query the ÖÑÞÁØ×ÍÌÑÎÇ and ÛÓÐÔÑÇÛÛÍ tables to view your changes for employee 106, and then commit the changes.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðíÁðìò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÍÛÔÛÝÌ ö ÚÎÑÓ ¶±¾Á¸·­¬±®§ ÉØÛÎÛ »³°´±§»»Á·¼ ã ïðêå ÍÛÔÛÝÌ ¶±¾Á·¼ô ­¿´¿®§ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã ïðêå ÝÑÓÓ×Ìå

Page 411: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-3: Adding a New Row to the ÖÑÞÁØ×ÍÌÑÎÇ Table (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-22

5) Reenable the triggers on the ÛÓÐÔÑÇÛÛÍ, ÖÑÞÍ and ÖÑÞÁØ×ÍÌÑÎÇ tables.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðíÁðëò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ßÔÌÛÎ ÌßÞÔÛ »³°´±§»»­ ÛÒßÞÔÛ ßÔÔ ÌÎ×ÙÙÛÎÍå ßÔÌÛÎ ÌßÞÔÛ ¶±¾­ ÛÒßÞÔÛ ßÔÔ ÌÎ×ÙÙÛÎÍå ßÔÌÛÎ ÌßÞÔÛ ¶±¾Á¸·­¬±®§ ÛÒßÞÔÛ ßÔÔ ÌÎ×ÙÙÛÎÍå

Page 412: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-23

Practice Solution 1-4: Updating the Minimum and Maximum Salaries for a Job

In this practice, you create a program to update the minimum and maximum salaries for a job in the ÖÑÞÍ table.

1) Create a stored procedure called ËÐÜÁÖÑÞÍßÔ to update the minimum and maximum salaries for a specific job ID in the ÖÑÞÍ table. The procedure should provide three parameters: the job ID, a new minimum salary, and a new maximum salary. Add exception handling to account for an invalid job ID in the ÖÑÞÍ table. Raise an exception if the maximum salary supplied is less than the minimum salary, and provide a message that will be displayed if the row in the ÖÑÞÍ table is locked. Hint: The resource locked/busy error number is �54.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðìÁðïò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ «°¼Á¶±¾­¿´ø °Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼û¬§°»ô °Á²»©Á³·²­¿´ ×Ò ¶±¾­ò³·²Á­¿´¿®§û¬§°»ô °Á²»©Á³¿¨­¿´ ×Ò ¶±¾­ò³¿¨Á­¿´¿®§û¬§°»÷ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå »Á®»­±«®½»Á¾«­§ ÛÈÝÛÐÌ×ÑÒå »Á­¿´Á»®®±® ÛÈÝÛÐÌ×ÑÒå ÐÎßÙÓß ÛÈÝÛÐÌ×ÑÒÁ×Ò×Ì ø»Á®»­±«®½»Á¾«­§ ô óëì÷å ÞÛÙ×Ò ×Ú ø°Á²»©Á³¿¨­¿´ ä °Á²»©Á³·²­¿´÷ ÌØÛÒ Îß×ÍÛ »Á­¿´Á»®®±®å ÛÒÜ ×Úå ÍÛÔÛÝÌ ï ×ÒÌÑ ªÁ¼«³³§ ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã °Á¶±¾·¼ ÚÑÎ ËÐÜßÌÛ ÑÚ ³·²Á­¿´¿®§ ÒÑÉß×Ìå ËÐÜßÌÛ ¶±¾­ ÍÛÌ ³·²Á­¿´¿®§ ã °Á²»©Á³·²­¿´ô ³¿¨Á­¿´¿®§ ã °Á²»©Á³¿¨­¿´ ÉØÛÎÛ ¶±¾Á·¼ ã °Á¶±¾·¼å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ »Á®»­±«®½»Á¾«­§ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðððïô ùÖ±¾ ·²º±®³¿¬·±² ·­ ½«®®»²¬´§ ´±½µ»¼ô ¬®§ ´¿¬»®òù÷å ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðððïô ù̸·­ ¶±¾ ×Ü ¼±»­ ²±¬ »¨·­¬ù÷å ÉØÛÒ »Á­¿´Á»®®±® ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðððïô ùÜ¿¬¿ »®®±®æ Ó¿¨ ­¿´¿®§ ­¸±«´¼ ¾» ³±®» ¬¸¿² ³·² ­¿´¿®§ù÷å ÛÒÜ «°¼Á¶±¾­¿´å

Page 413: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solution 1-4: Updating the Minimum and Maximum Salaries for a Job (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-24

ñ ÍØÑÉ ÛÎÎÑÎÍ

2) Enable ÍÛÎÊÛÎÑËÌÐËÌ, and then execute the ËÐÜÁÖÑÞÍßÔ procedure by using a job ID of 'ÍÇÁßÒßÔ', a minimum salary of 7000 and a maximum salary of 140. Note: This should generate an exception message.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðìÁðîò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ ÛÈÛÝËÌÛ «°¼Á¶±¾­¿´øùÍÇÁßÒßÔùô éðððô ïìð÷

Page 414: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solution 1-4: Updating the Minimum and Maximum Salaries for a Job (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-25

3) Disable triggers on the ÛÓÐÔÑÇÛÛÍ and ÖÑÞÍ tables.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðìÁðíò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ßÔÌÛÎ ÌßÞÔÛ »³°´±§»»­ Ü×ÍßÞÔÛ ßÔÔ ÌÎ×ÙÙÛÎÍå ßÔÌÛÎ ÌßÞÔÛ ¶±¾­ Ü×ÍßÞÔÛ ßÔÔ ÌÎ×ÙÙÛÎÍå

4) Execute the ËÐÜÁÖÑÞÍßÔ procedure using a job ID of 'ÍÇÁßÒßÔ', a minimum salary of 7000, and a maximum salary of 14000.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðìÁðìò­¯´ script. The code, the connection prompt, and the results are displayed as follows: ÛÈÛÝËÌÛ «°¼Á¶±¾­¿´øùÍÇÁßÒßÔùô éðððô ïìððð÷

Page 415: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solution 1-4: Updating the Minimum and Maximum Salaries for a Job (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-26

5) Query the ÖÑÞÍ table to view your changes, and then commit the changes.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðìÁðëò­¯´ script. The code, the connection prompt, and the results are displayed as follows: ÍÛÔÛÝÌ ö ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã ùÍÇÁßÒßÔùå

6) Enable the triggers on the ÛÓÐÔÑÇÛÛÍ and ÖÑÞÍ tables.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðìÁðêò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

Page 416: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solution 1-4: Updating the Minimum and Maximum Salaries for a Job (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-27

ßÔÌÛÎ ÌßÞÔÛ »³°´±§»»­ ÛÒßÞÔÛ ßÔÔ ÌÎ×ÙÙÛÎÍå ßÔÌÛÎ ÌßÞÔÛ ¶±¾­ ÛÒßÞÔÛ ßÔÔ ÌÎ×ÙÙÛÎÍå

Page 417: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-28

Practice Solutions 1-5: Monitoring Employees Salaries

In this practice, you create a procedure to monitor whether employees have exceeded their average salaries for their job type.

1) Disable the ÍÛÝËÎÛÁÛÓÐÔÑÇÛÛÍ trigger.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðëÁðïò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ßÔÌÛÎ ÌÎ×ÙÙÛÎ ­»½«®»Á»³°´±§»»­ Ü×ÍßÞÔÛå

2) In the ÛÓÐÔÑÇÛÛÍ table, add an ÛÈÝÛÛÜÁßÊÙÍßÔ column to store up to three characters and a default value of ÒÑ. Use a check constraint to allow the values ÇÛÍ or ÒÑ.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðëÁðîò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ßÔÌÛÎ ÌßÞÔÛ »³°´±§»»­ ø ßÜÜ ø»¨½»»¼Á¿ª¹­¿´ ÊßÎÝØßÎîøí÷ ÜÛÚßËÔÌ ùÒÑù ÝÑÒÍÌÎß×ÒÌ »³°´±§»»­Á»¨½»»¼Á¿ª¹­¿´Á½µ ÝØÛÝÕ ø»¨½»»¼Á¿ª¹­¿´ ×Ò øùÇÛÍùô ùÒÑù÷÷÷å

Page 418: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-5: Monitoring Employees Salaries (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-29

3) Create a stored procedure called ÝØÛÝÕÁßÊÙÍßÔ that checks whether each employee�s salary exceeds the average salary for the ÖÑÞÁ×Ü.

a) The average salary for a job is calculated from the information in the ÖÑÞÍ table.

b) If the employee�s salary exceeds the average for his or her job, then update the ÛÈÝÛÛÜÁßÊÙÍßÔ column in the ÛÓÐÔÑÇÛÛÍ table to a value of ÇÛÍ; otherwise, set the value to ÒÑ.

c) Use a cursor to select the employee�s rows using the ÚÑÎ ËÐÜßÌÛ option in the query.

d) Add exception handling to account for a record being locked. Hint: The resource locked/busy error number is �ëì.

e) Write and use a local function called ÙÛÌÁÖÑÞÁßÊÙÍßÔ to determine the average salary for a job ID specified as a parameter.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðëÁðíò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ ½¸»½µÁ¿ª¹­¿´ ×Í »³°Á»¨½»»¼Á¿ª¹­¿´Á¬§°» »³°´±§»»­ò»¨½»»¼Á¿ª¹­¿´û¬§°»å ÝËÎÍÑÎ ½Á»³°Á½­® ×Í ÍÛÔÛÝÌ »³°´±§»»Á·¼ô ¶±¾Á·¼ô ­¿´¿®§ ÚÎÑÓ »³°´±§»»­ ÚÑÎ ËÐÜßÌÛå »Á®»­±«®½»Á¾«­§ ÛÈÝÛÐÌ×ÑÒå ÐÎßÙÓß ÛÈÝÛÐÌ×ÑÒÁ×Ò×Ìø»Á®»­±«®½»Á¾«­§ô óëì÷å ÚËÒÝÌ×ÑÒ ¹»¬Á¶±¾Á¿ª¹­¿´ ø¶±¾·¼ ÊßÎÝØßÎî÷ ÎÛÌËÎÒ ÒËÓÞÛÎ ×Í ¿ª¹Á­¿´ »³°´±§»»­ò­¿´¿®§û¬§°»å ÞÛÙ×Ò ÍÛÔÛÝÌ ø³¿¨Á­¿´¿®§ õ ³·²Á­¿´¿®§÷ñî ×ÒÌÑ ¿ª¹Á­¿´ ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã ¶±¾·¼å

Page 419: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-5: Monitoring Employees Salaries (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-30

ÎÛÌËÎÒ ¿ª¹Á­¿´å ÛÒÜå

ÞÛÙ×Ò ÚÑÎ »³°®»½ ×Ò ½Á»³°Á½­® ÔÑÑÐ »³°Á»¨½»»¼Á¿ª¹­¿´Á¬§°» æã ùÒÑùå ×Ú »³°®»½ò­¿´¿®§ âã ¹»¬Á¶±¾Á¿ª¹­¿´ø»³°®»½ò¶±¾Á·¼÷ ÌØÛÒ »³°Á»¨½»»¼Á¿ª¹­¿´Á¬§°» æã ùÇÛÍùå ÛÒÜ ×Úå ËÐÜßÌÛ »³°´±§»»­ ÍÛÌ »¨½»»¼Á¿ª¹­¿´ ã »³°Á»¨½»»¼Á¿ª¹­¿´Á¬§°» ÉØÛÎÛ ÝËÎÎÛÒÌ ÑÚ ½Á»³°Á½­®å ÛÒÜ ÔÑÑÐå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ »Á®»­±«®½»Á¾«­§ ÌØÛÒ ÎÑÔÔÞßÝÕå Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðððïô ùλ½±®¼ ·­ ¾«­§ô ¬®§ ´¿¬»®òù÷å ÛÒÜ ½¸»½µÁ¿ª¹­¿´å ñ ÍØÑÉ ÛÎÎÑÎÍ

4) Execute the ÝØÛÝÕÁßÊÙÍßÔ procedure. To view the results of your modifications, write a query to display the employee�s ID, job, the average salary for the job, the employee�s salary and the »¨½»»¼Á¿ª¹­¿´ indicator column for employees whose salaries exceed the average for their job, and finally commit the changes. Note: These exercises can be used for extra practice when discussing how to create functions.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðëÁðìò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

Page 420: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-5: Monitoring Employees Salaries (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-31

ÛÈÛÝËÌÛ ½¸»½µÁ¿ª¹­¿´ ÍÛÔÛÝÌ »ò»³°´±§»»Á·¼ô »ò¶±¾Á·¼ô ø¶ò³¿¨Á­¿´¿®§ó¶ò³·²Á­¿´¿®§ñî÷ ¶±¾Á¿ª¹­¿´ô »ò­¿´¿®§ô »ò»¨½»»¼Á¿ª¹­¿´ ¿ª¹Á»¨½»»¼»¼ ÚÎÑÓ »³°´±§»»­ »ô ¶±¾­ ¶ ÉØÛÎÛ »ò¶±¾Á·¼ ã ¶ò¶±¾Á·¼ ¿²¼ »ò»¨½»»¼Á¿ª¹­¿´ ã ùÇÛÍùå ÝÑÓÓ×Ìå

Page 421: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-5: Monitoring Employees Salaries (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-32

Page 422: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-33

Practice Solutions 1-6: Retrieving the Total Number of Years of Service for an Employee

In this practice, you create a subprogram to retrieve the number of years of service for a specific employee.

1) Create a stored function called ÙÛÌÁÇÛßÎÍÁÍÛÎÊ×ÝÛ to retrieve the total number of years of service for a specific employee. The function should accept the employee ID as a parameter and return the number of years of service. Add error handling to account for an invalid employee ID.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðêÁðïò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÚËÒÝÌ×ÑÒ ¹»¬Á§»¿®­Á­»®ª·½»ø °Á»³°Á»³°·¼Á¬§°» ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÒËÓÞÛÎ ×Í ÝËÎÍÑÎ ½Á¶±¾¸Á½­® ×Í ÍÛÔÛÝÌ ÓÑÒÌØÍÁÞÛÌÉÛÛÒø»²¼Á¼¿¬»ô ­¬¿®¬Á¼¿¬»÷ñïî ªÁ§»¿®­Á·²Á¶±¾ ÚÎÑÓ ¶±¾Á¸·­¬±®§ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á»³°·¼Á¬§°»å ªÁ§»¿®­Á­»®ª·½» ÒËÓÞÛÎøî÷ æã ðå ªÁ§»¿®­Á·²Á¶±¾ ÒËÓÞÛÎøî÷ æã ðå ÞÛÙ×Ò ÚÑÎ ¶±¾¸Á®»½ ×Ò ½Á¶±¾¸Á½­® ÔÑÑÐ ÛÈ×Ì ÉØÛÒ ½Á¶±¾¸Á½­®ûÒÑÌÚÑËÒÜå ªÁ§»¿®­Á­»®ª·½» æã ªÁ§»¿®­Á­»®ª·½» õ ¶±¾¸Á®»½òªÁ§»¿®­Á·²Á¶±¾å ÛÒÜ ÔÑÑÐå ÍÛÔÛÝÌ ÓÑÒÌØÍÁÞÛÌÉÛÛÒøÍÇÍÜßÌÛô ¸·®»Á¼¿¬»÷ñïî ×ÒÌÑ ªÁ§»¿®­Á·²Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á»³°·¼Á¬§°»å ªÁ§»¿®­Á­»®ª·½» æã ªÁ§»¿®­Á­»®ª·½» õ ªÁ§»¿®­Á·²Á¶±¾å ÎÛÌËÎÒ ÎÑËÒÜøªÁ§»¿®­Á­»®ª·½»÷å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðíìèô ùÛ³°´±§»» ©·¬¸ ×Ü ù¤¤ °Á»³°Á»³°·¼Á¬§°» ¤¤ù ¼±»­ ²±¬ »¨·­¬òù÷å ÎÛÌËÎÒ ÒËÔÔå ÛÒÜ ¹»¬Á§»¿®­Á­»®ª·½»å ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 423: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-6: Retrieving the Total Number of Years of Service for an Employee (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-34

2) Invoke the ÙÛÌÁÇÛßÎÍÁÍÛÎÊ×ÝÛ function in a call to ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ for an employee with ID 999.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðêÁðîò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÛÈÛÝËÌÛ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø¹»¬Á§»¿®­Á­»®ª·½» øççç÷÷

3) Display the number of years of service for employee 106 with ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ invoking the ÙÛÌÁÇÛßÎÍÁÍÛÎÊ×ÝÛ function. Make sure to enable ÍÛÎÊÛÎÑËÌÐËÌ.

Page 424: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-6: Retrieving the Total Number of Years of Service for an Employee (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-35

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðêÁðíò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ ø ùÛ³°´±§»» ïðê ¸¿­ ©±®µ»¼ ù ¤¤ ¹»¬Á§»¿®­Á­»®ª·½»øïðê÷ ¤¤ ù §»¿®­ù÷å ÛÒÜå ñ

4) Query the ÖÑÞÁØ×ÍÌÑÎÇ and ÛÓÐÔÑÇÛÛÍ tables for the specified employee to verify that the modifications are accurate. The values represented in the results on this page may differ from those you get when you run these queries.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðêÁðìò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÍÛÔÛÝÌ »³°´±§»»Á·¼ô ¶±¾Á·¼ô ÓÑÒÌØÍÁÞÛÌÉÛÛÒø»²¼Á¼¿¬»ô ­¬¿®¬Á¼¿¬»÷ñïî ¼«®¿¬·±² ÚÎÑÓ ¶±¾Á¸·­¬±®§å ÍÛÔÛÝÌ ¶±¾Á·¼ô ÓÑÒÌØÍÁÞÛÌÉÛÛÒøÍÇÍÜßÌÛô ¸·®»Á¼¿¬»÷ñïî ¼«®¿¬·±² ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã ïðêå

Page 425: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-6: Retrieving the Total Number of Years of Service for an Employee (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-36

Page 426: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-37

Practice Solutions 1-7: Retrieving the Total Number of Different Jobs for an Employee

In this practice, you create a program to retrieve the number of different jobs that an employee worked on during his or her service.

1) Create a stored function called ÙÛÌÁÖÑÞÁÝÑËÒÌ to retrieve the total number of different jobs on which an employee worked.

a) The function should accept the employee ID in a parameter, and return the number of different jobs that the employee worked on until now, including the present job.

b) Add exception handling to account for an invalid employee ID. Hint: Use the distinct job IDs from the ÖÑÞÁØ×ÍÌÑÎÇ table, and exclude the current job ID if it is one of the job IDs on which the employee has already worked.

c) Write a ËÒ×ÑÒ of two queries and count the rows retrieved into a PL/SQL table.

d) Use a ÚÛÌÝØ with ÞËÔÕ ÝÑÔÔÛÝÌ ×ÒÌÑ to obtain the unique jobs for the employee.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðéÁðïò­¯´ script. The code, the connection prompt, and the results are displayed as follows: ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÚËÒÝÌ×ÑÒ ¹»¬Á¶±¾Á½±«²¬ø °Á»³°Á»³°·¼Á¬§°» ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÒËÓÞÛÎ ×Í ÌÇÐÛ ¶±¾­Á¬¿¾´»Á¬§°» ×Í ÌßÞÔÛ ÑÚ ¶±¾­ò¶±¾Á·¼û¬§°»å ªÁ¶±¾¬¿¾ ¶±¾­Á¬¿¾´»Á¬§°»å ÝËÎÍÑÎ ½Á»³°¶±¾Á½­® ×Í ÍÛÔÛÝÌ ¶±¾Á·¼ ÚÎÑÓ ¶±¾Á¸·­¬±®§ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á»³°·¼Á¬§°» ËÒ×ÑÒ ÍÛÔÛÝÌ ¶±¾Á·¼ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á»³°·¼Á¬§°»å ÞÛÙ×Ò ÑÐÛÒ ½Á»³°¶±¾Á½­®å ÚÛÌÝØ ½Á»³°¶±¾Á½­® ÞËÔÕ ÝÑÔÔÛÝÌ ×ÒÌÑ ªÁ¶±¾¬¿¾å ÝÔÑÍÛ ½Á»³°¶±¾Á½­®å ÎÛÌËÎÒ ªÁ¶±¾¬¿¾ò½±«²¬å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðíìèô ùÛ³°´±§»» ©·¬¸ ×Ü ù¤¤ °Á»³°Á»³°·¼Á¬§°» ¤¤ù ¼±»­ ²±¬ »¨·­¬ÿù÷å ÎÛÌËÎÒ ÒËÔÔå

Page 427: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-7: Retrieving the Total Number of Different Jobs for an Employee (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-38

ÛÒÜ ¹»¬Á¶±¾Á½±«²¬å ñ ÍØÑÉ ÛÎÎÑÎÍ

ÚËÒÝÌ×ÑÒ ¹»¬Á¶±¾Á½±«²¬ø ݱ³°·´»¼ò Ò± Û®®±®­ò

2) Invoke the function for the employee with the ID of 176. Make sure to enable ÍÛÎÊÛÎÑËÌÐËÌ.

Note: These exercises can be used for extra practice when discussing how to create packages. Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðéÁðîò­¯´ script. The code, the connection prompt, and the results are displayed as follows: ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ ÞÛÙ×Ò ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùÛ³°´±§»» ïéê ©±®µ»¼ ±² ù ¤¤ ¹»¬Á¶±¾Á½±«²¬øïéê÷ ¤¤ ù ¼·ºº»®»²¬ ¶±¾­òù÷å ÛÒÜå ñ

Page 428: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-7: Retrieving the Total Number of Different Jobs for an Employee (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-39

Page 429: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-40

Practice Solutions 1-8: Creating a New Package that Contains the Newly Created Procedures and Functions

In this practice, you create a package called ÛÓÐÖÑÞÁÐÕÙ that contains your ÒÛÉÁÖÑÞ, ßÜÜÁÖÑÞÁØ×ÍÌ, ËÐÜÁÖÑÞÍßÔ procedures, as well as your ÙÛÌÁÇÛßÎÍÁÍÛÎÊ×ÝÛ and ÙÛÌÁÖÑÞÁÝÑËÒÌ functions.

1) Create the package specification with all the subprogram constructs as public. Move any subprogram local-defined types into the package specification.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðèÁðïò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ »³°¶±¾Á°µ¹ ×Í ÌÇÐÛ ¶±¾­Á¬¿¾´»Á¬§°» ×Í ÌßÞÔÛ ÑÚ ¶±¾­ò¶±¾Á·¼û¬§°»å ÐÎÑÝÛÜËÎÛ ¿¼¼Á¶±¾Á¸·­¬ø °Á»³°Á·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á²»©Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼ûÌÇÐÛ÷å ÚËÒÝÌ×ÑÒ ¹»¬Á¶±¾Á½±«²¬ø °Á»³°Á·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÒËÓÞÛÎå ÚËÒÝÌ×ÑÒ ¹»¬Á§»¿®­Á­»®ª·½»ø °Á»³°Á·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÒËÓÞÛÎå ÐÎÑÝÛÜËÎÛ ²»©Á¶±¾ø °Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼ûÌÇÐÛô °Á¬·¬´» ×Ò ¶±¾­ò¶±¾Á¬·¬´»ûÌÇÐÛô °Á³·²­¿´ ×Ò ¶±¾­ò³·²Á­¿´¿®§ûÌÇÐÛ÷å ÐÎÑÝÛÜËÎÛ «°¼Á¶±¾­¿´ø °Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼û¬§°»ô °Á²»©Á³·²­¿´ ×Ò ¶±¾­ò³·²Á­¿´¿®§û¬§°»ô °Á²»©Á³¿¨­¿´ ×Ò ¶±¾­ò³¿¨Á­¿´¿®§û¬§°»÷å ÛÒÜ »³°¶±¾Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 430: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-8: Creating a New Package that Contains the Newly Created Procedures and Functions (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-41

2) Create the package body with the subprogram implementation; remember to remove

from the subprogram implementations any types that you moved into the package specification.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðèÁðîò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ »³°¶±¾Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ¿¼¼Á¶±¾Á¸·­¬ø °Á»³°Á·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛô °Á²»©Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼ûÌÇÐÛ÷ ×Í ÞÛÙ×Ò ×ÒÍÛÎÌ ×ÒÌÑ ¶±¾Á¸·­¬±®§ ÍÛÔÛÝÌ »³°´±§»»Á·¼ô ¸·®»Á¼¿¬»ô ÍÇÍÜßÌÛô ¶±¾Á·¼ô ¼»°¿®¬³»²¬Á·¼ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ËÐÜßÌÛ »³°´±§»»­ ÍÛÌ ¸·®»Á¼¿¬» ã ÍÇÍÜßÌÛô ¶±¾Á·¼ ã °Á²»©Á¶±¾·¼ô ­¿´¿®§ ã øÍÛÔÛÝÌ ³·²Á­¿´¿®§ õ ëðð ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã °Á²»©Á¶±¾·¼÷ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ øùß¼¼»¼ »³°´±§»» ù ¤¤ °Á»³°Á·¼ ¤¤ ù ¼»¬¿·´­ ¬± ¬¸» ÖÑÞÁØ×ÍÌÑÎÇ ¬¿¾´»ù÷å ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ øùË°¼¿¬»¼ ½«®®»²¬ ¶±¾ ±º »³°´±§»» ù ¤¤ °Á»³°Á·¼¤¤ ù ¬± ù¤¤ °Á²»©Á¶±¾·¼÷å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðððïô ùÛ³°´±§»» ¼±»­ ²±¬ »¨·­¬ÿù÷å ÛÒÜ ¿¼¼Á¶±¾Á¸·­¬å ÚËÒÝÌ×ÑÒ ¹»¬Á¶±¾Á½±«²¬ø °Á»³°Á·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÒËÓÞÛÎ ×Í ªÁ¶±¾¬¿¾ ¶±¾­Á¬¿¾´»Á¬§°»å ÝËÎÍÑÎ ½Á»³°¶±¾Á½­® ×Í ÍÛÔÛÝÌ ¶±¾Á·¼ ÚÎÑÓ ¶±¾Á¸·­¬±®§ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼ ËÒ×ÑÒ

Page 431: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-8: Creating a New Package that Contains the Newly Created Procedures and Functions (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-42

ÍÛÔÛÝÌ ¶±¾Á·¼ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ÞÛÙ×Ò ÑÐÛÒ ½Á»³°¶±¾Á½­®å ÚÛÌÝØ ½Á»³°¶±¾Á½­® ÞËÔÕ ÝÑÔÔÛÝÌ ×ÒÌÑ ªÁ¶±¾¬¿¾å ÝÔÑÍÛ ½Á»³°¶±¾Á½­®å ÎÛÌËÎÒ ªÁ¶±¾¬¿¾ò½±«²¬å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðíìèô ùÛ³°´±§»» ©·¬¸ ×Ü ù¤¤ °Á»³°Á·¼ ¤¤ù ¼±»­ ²±¬ »¨·­¬ÿù÷å ÎÛÌËÎÒ ðå ÛÒÜ ¹»¬Á¶±¾Á½±«²¬å ÚËÒÝÌ×ÑÒ ¹»¬Á§»¿®­Á­»®ª·½»ø °Á»³°Á·¼ ×Ò »³°´±§»»­ò»³°´±§»»Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÒËÓÞÛÎ ×Í ÝËÎÍÑÎ ½Á¶±¾¸Á½­® ×Í ÍÛÔÛÝÌ ÓÑÒÌØÍÁÞÛÌÉÛÛÒø»²¼Á¼¿¬»ô ­¬¿®¬Á¼¿¬»÷ñïî ªÁ§»¿®­Á·²Á¶±¾ ÚÎÑÓ ¶±¾Á¸·­¬±®§ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ªÁ§»¿®­Á­»®ª·½» ÒËÓÞÛÎøî÷ æã ðå ªÁ§»¿®­Á·²Á¶±¾ ÒËÓÞÛÎøî÷ æã ðå ÞÛÙ×Ò ÚÑÎ ¶±¾¸Á®»½ ×Ò ½Á¶±¾¸Á½­® ÔÑÑÐ ÛÈ×Ì ÉØÛÒ ½Á¶±¾¸Á½­®ûÒÑÌÚÑËÒÜå ªÁ§»¿®­Á­»®ª·½» æã ªÁ§»¿®­Á­»®ª·½» õ ¶±¾¸Á®»½òªÁ§»¿®­Á·²Á¶±¾å ÛÒÜ ÔÑÑÐå ÍÛÔÛÝÌ ÓÑÒÌØÍÁÞÛÌÉÛÛÒøÍÇÍÜßÌÛô ¸·®»Á¼¿¬»÷ñïî ×ÒÌÑ ªÁ§»¿®­Á·²Á¶±¾ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã °Á»³°Á·¼å ªÁ§»¿®­Á­»®ª·½» æã ªÁ§»¿®­Á­»®ª·½» õ ªÁ§»¿®­Á·²Á¶±¾å ÎÛÌËÎÒ ÎÑËÒÜøªÁ§»¿®­Á­»®ª·½»÷å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðíìèô ùÛ³°´±§»» ©·¬¸ ×Ü ù¤¤ °Á»³°Á·¼ ¤¤ù ¼±»­ ²±¬ »¨·­¬òù÷å ÎÛÌËÎÒ ðå ÛÒÜ ¹»¬Á§»¿®­Á­»®ª·½»å ÐÎÑÝÛÜËÎÛ ²»©Á¶±¾ø °Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼ûÌÇÐÛô °Á¬·¬´» ×Ò ¶±¾­ò¶±¾Á¬·¬´»ûÌÇÐÛô °Á³·²­¿´ ×Ò ¶±¾­ò³·²Á­¿´¿®§ûÌÇÐÛ÷ ×Í ªÁ³¿¨­¿´ ¶±¾­ò³¿¨Á­¿´¿®§ûÌÇÐÛ æã î ö °Á³·²­¿´å ÞÛÙ×Ò

Page 432: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-8: Creating a New Package that Contains the Newly Created Procedures and Functions (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-43

×ÒÍÛÎÌ ×ÒÌÑ ¶±¾­ø¶±¾Á·¼ô ¶±¾Á¬·¬´»ô ³·²Á­¿´¿®§ô ³¿¨Á­¿´¿®§÷ ÊßÔËÛÍ ø°Á¶±¾·¼ô °Á¬·¬´»ô °Á³·²­¿´ô ªÁ³¿¨­¿´÷å ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ øùÒ»© ®±© ¿¼¼»¼ ¬± ÖÑÞÍ ¬¿¾´»æù÷å ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛ ø°Á¶±¾·¼ ¤¤ ù ù ¤¤ °Á¬·¬´» ¤¤ù ù¤¤ °Á³·²­¿´ ¤¤ ù ù ¤¤ ªÁ³¿¨­¿´÷å ÛÒÜ ²»©Á¶±¾å ÐÎÑÝÛÜËÎÛ «°¼Á¶±¾­¿´ø °Á¶±¾·¼ ×Ò ¶±¾­ò¶±¾Á·¼û¬§°»ô °Á²»©Á³·²­¿´ ×Ò ¶±¾­ò³·²Á­¿´¿®§û¬§°»ô °Á²»©Á³¿¨­¿´ ×Ò ¶±¾­ò³¿¨Á­¿´¿®§û¬§°»÷ ×Í ªÁ¼«³³§ ÐÔÍÁ×ÒÌÛÙÛÎå »Á®»­±«®½»Á¾«­§ ÛÈÝÛÐÌ×ÑÒå »Á­¿´Á»®®±® ÛÈÝÛÐÌ×ÑÒå ÐÎßÙÓß ÛÈÝÛÐÌ×ÑÒÁ×Ò×Ì ø»Á®»­±«®½»Á¾«­§ ô óëì÷å ÞÛÙ×Ò ×Ú ø°Á²»©Á³¿¨­¿´ ä °Á²»©Á³·²­¿´÷ ÌØÛÒ Îß×ÍÛ »Á­¿´Á»®®±®å ÛÒÜ ×Úå ÍÛÔÛÝÌ ï ×ÒÌÑ ªÁ¼«³³§ ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã °Á¶±¾·¼ ÚÑÎ ËÐÜßÌÛ ÑÚ ³·²Á­¿´¿®§ ÒÑÉß×Ìå ËÐÜßÌÛ ¶±¾­ ÍÛÌ ³·²Á­¿´¿®§ ã °Á²»©Á³·²­¿´ô ³¿¨Á­¿´¿®§ ã °Á²»©Á³¿¨­¿´ ÉØÛÎÛ ¶±¾Á·¼ ã °Á¶±¾·¼å ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ »Á®»­±«®½»Á¾«­§ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ øóîðððïô ùÖ±¾ ·²º±®³¿¬·±² ·­ ½«®®»²¬´§ ´±½µ»¼ô ¬®§ ´¿¬»®òù÷å ÉØÛÒ ÒÑÁÜßÌßÁÚÑËÒÜ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðððïô ù̸·­ ¶±¾ ×Ü ¼±»­ ²±¬ »¨·­¬ù÷å ÉØÛÒ »Á­¿´Á»®®±® ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðððïô ùÜ¿¬¿ »®®±®æ Ó¿¨ ­¿´¿®§ ­¸±«´¼ ¾» ³±®» ¬¸¿² ³·² ­¿´¿®§ù÷å ÛÒÜ «°¼Á¶±¾­¿´å ÛÒÜ »³°¶±¾Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 433: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-8: Creating a New Package that Contains the Newly Created Procedures and Functions (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-44

3) Invoke your ÛÓÐÖÑÞÁÐÕÙòÒÛÉÁÖÑÞ procedure to create a new job with the ID ÐÎÁÓßÒ, the job title Public Relations Manager, and the salary 6250. Make sure to enable ÍÛÎÊÛÎÑËÌÐËÌ.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðèÁðíò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ ÛÈÛÝËÌÛ »³°¶±¾Á°µ¹ò²»©Á¶±¾øùÐÎÁÓßÒùô ùЫ¾´·½ λ´¿¬·±²­ Ó¿²¿¹»®ùô êîëð÷

Page 434: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-8: Creating a New Package that Contains the Newly Created Procedures and Functions (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-45

4) Invoke your ÛÓÐÖÑÞÁÐÕÙòßÜÜÁÖÑÞÁØ×ÍÌ procedure to modify the job of employee ID 110 to job ID ÐÎÁÓßÒ. Note: You need to disable the ËÐÜßÌÛÁÖÑÞÁØ×ÍÌÑÎÇ trigger before you execute the ßÜÜÁÖÑÞÁØ×ÍÌ procedure, and reenable the trigger after you have executed the procedure. Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðèÁðìò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ßÔÌÛÎ ÌÎ×ÙÙÛÎ «°¼¿¬»Á¶±¾Á¸·­¬±®§ Ü×ÍßÞÔÛå ÛÈÛÝËÌÛ »³°¶±¾Á°µ¹ò¿¼¼Á¶±¾Á¸·­¬øïïðô ùÐÎÁÓßÒù÷ ßÔÌÛÎ ÌÎ×ÙÙÛÎ «°¼¿¬»Á¶±¾Á¸·­¬±®§ ÛÒßÞÔÛå

5) Query the ÖÑÞÍ, ÖÑÞÁØ×ÍÌÑÎÇ, and ÛÓÐÔÑÇÛÛÍ tables to verify the results.

Note: These exercises can be used for extra practice when discussing how to create database triggers.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðèÁðëò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÍÛÔÛÝÌ ö ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã ùÐÎÁÓßÒùå ÍÛÔÛÝÌ ö ÚÎÑÓ ¶±¾Á¸·­¬±®§ ÉØÛÎÛ »³°´±§»»Á·¼ ã ïïðå ÍÛÔÛÝÌ ¶±¾Á·¼ô ­¿´¿®§ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ »³°´±§»»Á·¼ ã ïïðå

Page 435: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 1-8: Creating a New Package that Contains the Newly Created Procedures and Functions (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-46

Page 436: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-47

Practice Solution 1-9: Creating a Trigger to Ensure that the Employees Salaries are Within the Acceptable Range

In this practice, you create a trigger to ensure that the minimum and maximum salaries of a job are never modified such that the salary of an existing employee with that job ID is out of the new range specified for the job.

1) Create a trigger called ÝØÛÝÕÁÍßÔÁÎßÒÙÛ that is fired before every row that is updated in the Ó×ÒÁÍßÔßÎÇ and ÓßÈÁÍßÔßÎÇ columns in the ÖÑÞÍ table.

a) For any minimum or maximum salary value that is changed, check whether the salary of any existing employee with that job ID in the ÛÓÐÔÑÇÛÛÍ table falls within the new range of salaries specified for this job ID.

b) Include exception handling to cover a salary range change that affects the record of any existing employee.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðçÁðïò­¯´ script. The code, the connection prompt, and the results are displayed as follows: ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÌÎ×ÙÙÛÎ ½¸»½µÁ­¿´Á®¿²¹» ÞÛÚÑÎÛ ËÐÜßÌÛ ÑÚ ³·²Á­¿´¿®§ô ³¿¨Á­¿´¿®§ ÑÒ ¶±¾­ ÚÑÎ ÛßÝØ ÎÑÉ ÜÛÝÔßÎÛ ªÁ³·²­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛå ªÁ³¿¨­¿´ »³°´±§»»­ò­¿´¿®§ûÌÇÐÛå »Á·²ª¿´·¼Á­¿´®¿²¹» ÛÈÝÛÐÌ×ÑÒå ÞÛÙ×Ò ÍÛÔÛÝÌ Ó×Òø­¿´¿®§÷ô ÓßÈø­¿´¿®§÷ ×ÒÌÑ ªÁ³·²­¿´ô ªÁ³¿¨­¿´ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ¶±¾Á·¼ ã æÒÛÉò¶±¾Á·¼å ×Ú øªÁ³·²­¿´ ä æÒÛÉò³·²Á­¿´¿®§÷ ÑÎ øªÁ³¿¨­¿´ â æÒÛÉò³¿¨Á­¿´¿®§÷ ÌØÛÒ Îß×ÍÛ »Á·²ª¿´·¼Á­¿´®¿²¹»å ÛÒÜ ×Úå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ »Á·²ª¿´·¼Á­¿´®¿²¹» ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðëëðô ùÛ³°´±§»»­ »¨·­¬ ©¸±­» ­¿´¿®§ ·­ ±«¬ ±º ¬¸» ­°»½·º·»¼ ®¿²¹»ò ù¤¤ ù̸»®»º±®» ¬¸» ­°»½·º·»¼ ­¿´¿®§ ®¿²¹» ½¿²²±¬ ¾» «°¼¿¬»¼òù÷å ÛÒÜ ½¸»½µÁ­¿´Á®¿²¹»å ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 437: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solution 1-9: Creating a Trigger to Ensure that the Employees Salaries are Within the Acceptable Range (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-48

2) Test the trigger using the ÍÇÁßÒßÔ job, setting the new minimum salary to 5000, and the new maximum salary to 7000. Before you make the change, write a query to display the current salary range for the ÍÇÁßÒßÔ job ID, and another query to display the employee ID, last name, and salary for the same job ID. After the update, query the change (if any) to the ÖÑÞÍ table for the specified job ID.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðçÁðîò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÍÛÔÛÝÌ ö ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã ùÍÇÁßÒßÔùå ÍÛÔÛÝÌ »³°´±§»»Á·¼ô ´¿­¬Á²¿³»ô ­¿´¿®§ ÚÎÑÓ »³°´±§»»­ ÉØÛÎÛ ¶±¾Á·¼ ã ùÍÇÁßÒßÔùå ËÐÜßÌÛ ¶±¾­ ÍÛÌ ³·²Á­¿´¿®§ ã ëðððô ³¿¨Á­¿´¿®§ ã éððð ÉØÛÎÛ ¶±¾Á·¼ ã ùÍÇÁßÒßÔùå ÍÛÔÛÝÌ ö ÚÎÑÓ ¶±¾­ ÉØÛÎÛ ¶±¾Á·¼ ã ùÍÇÁßÒßÔùå

Page 438: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solution 1-9: Creating a Trigger to Ensure that the Employees Salaries are Within the Acceptable Range (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-49

3) Using the ÍÇÁßÒßÔ job, set the new minimum salary to 7,000, and the new maximum salary to 18000. Explain the results.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðïÁðçÁðíò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ËÐÜßÌÛ ¶±¾­ ÍÛÌ ³·²Á­¿´¿®§ ã éðððô ³¿¨Á­¿´¿®§ ã ïèððð ÉØÛÎÛ ¶±¾Á·¼ ã ùÍÇÁßÒßÔùå

Page 439: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solution 1-9: Creating a Trigger to Ensure that the Employees Salaries are Within the Acceptable Range (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-50

The update fails to change the salary range due to the functionality provided by the ÝØÛÝÕÁÍßÔÁÎßÒÙÛ trigger because employee 106 who has the ÍÇÁßÒßÔ job ID has a salary of êëðð, which is less than the minimum salary for the new salary range specified in the ËÐÜßÌÛ statement.

Page 440: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-51

Practice 2

In this case study, you create a package named Ê×ÜÛÑÁÐÕÙ that contains procedures and functions for a video store application. This application enables customers to become a member of the video store. Any member can rent movies, return rented movies, and reserve movies. Additionally, you create a trigger to ensure that any data in the video tables is modified only during business hours. Create the package by using SQL*Plus and use the ÜÞÓÍÁÑËÌÐËÌ Oracle-supplied package to display messages. The video store database contains the following tables: Ì×ÌÔÛ, Ì×ÌÔÛÁÝÑÐÇ, ÎÛÒÌßÔ, ÎÛÍÛÎÊßÌ×ÑÒ, and ÓÛÓÞÛÎ. The video store database entity relationship diagram

TITLE #* ID * title * description o rating o category o release date

TITLE_COPY #* ID * status

RENTAL #* book date o act ret date o exp ret date

MEMBER #* ID * last name o first name o address o city o phone * join date

RESERVATION #* reservation date

for

the subjectof

available as

a copy

made against

responsiblefor

createdfor

responsiblefor

set up

Page 441: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-52

Practice 2-1: Creating the VIDEO_PKG Package

In this practice, you create a package named Ê×ÜÛÑÁÐÕÙ that contains procedures and functions for a video store application.

1) Load and execute the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«/´¿¾­ñ¾«·´¼ª·¼ïò­¯´ script to create all the required tables and sequences that are needed for this exercise.

2) Load and execute the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«/´¿¾­ñ¾«·´¼ª·¼îò­¯´ script to populate all the tables created through the buildvid1.sql script.

3) Create a package named Ê×ÜÛÑÁÐÕÙ with the following procedures and functions:

a) ÒÛÉÁÓÛÓÞÛÎ: A public procedure that adds a new member to the ÓÛÓÞÛÎ table. For the member ×Ü number, use the sequence ÓÛÓÞÛÎÁ×ÜÁÍÛÏ; for the join date, use ÍÇÍÜßÌÛ. Pass all other values to be inserted into a new row as parameters.

b) ÒÛÉÁÎÛÒÌßÔ: An overloaded public function to record a new rental. Pass the title ID number for the video that a customer wants to rent, and either the customer�s last name or his member ID number into the function. The function should return the due date for the video. Due dates are three days from the date the video is rented. If the status for a movie requested is listed as ßÊß×ÔßÞÔÛ in the Ì×ÌÔÛÁÝÑÐÇ table for one copy of this title, then update this Ì×ÌÔÛÁÝÑÐÇ table and set the status to ÎÛÒÌÛÜ. If there is no copy available, the function must return ÒËÔÔ. Then, insert a new record into the ÎÛÒÌßÔ table identifying the booked date as today�s date, the copy ID number, the member ID number, the title ID number, and the expected return date. Be aware of multiple customers with the same last name. In this case, have the function return ÒËÔÔ, and display a list of the customers� names that match and their ID numbers.

c) ÎÛÌËÎÒÁÓÑÊ×Û: A public procedure that updates the status of a video (available, rented, or damaged) and sets the return date. Pass the title ID, the copy ID, and the status to this procedure. Check whether there are reservations for that title and display a message if it is reserved. Update the ÎÛÒÌßÔ table and set the actual return date to today�s date. Update the status in the Ì×ÌÔÛÁÝÑÐÇ table based on the status parameter passed into the procedure.

d) ÎÛÍÛÎÊÛÁÓÑÊ×Û: A private procedure that executes only if all the video copies requested in the ÒÛÉÁÎÛÒÌßÔ procedure have a status of ÎÛÒÌÛÜ. Pass the member ID number and the title ID number to this procedure. Insert a new record into the ÎÛÍÛÎÊßÌ×ÑÒ table and record the reservation date, member ID number, and title ID number. Print a message indicating that a movie is reserved and its expected date of return.

Page 442: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-53

e) ÛÈÝÛÐÌ×ÑÒÁØßÒÜÔÛÎ: A private procedure that is called from the exception handler of the public programs. Pass the SQLCODE number to this procedure, and the name of the program (as a text string) where the error occurred. Use Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ to raise a customized error. Start with a unique key violation (-1) and foreign key violation (-2292). Allow the exception handler to raise a generic error for any other errors.

4) Use the following scripts located in the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«/­±´² directory to test your routines:

a) Add two members using ­±´Á¿°ÁðîÁðïÁðìÁ¿ò­¯´.

b) Add new video rentals using ­±´Á¿°ÁðîÁðïÁðìÁ¾ò­¯´.

c) Return movies using the ­±´Á¿°ÁðîÁðïÁðìÁ½ò­¯´.

5) The business hours for the video store are 8:00 AM through 10:00 PM, Sunday through Friday, and 8:00 AM through 12:00 PM on Saturday. To ensure that the tables can be modified only during these hours, create a stored procedure that is called by triggers on the tables.

a) Create a stored procedure called Ì×ÓÛÁÝØÛÝÕ that checks the current time against business hours. If the current time is not within business hours, use the Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ procedure to give an appropriate message.

b) Create a trigger on each of the five tables. Fire the trigger before data is inserted, updated, and deleted from the tables. Call your Ì×ÓÛÁÝØÛÝÕ procedure from each of these triggers.

c) Test your triggers. Note: In order for your trigger to fail, you may need to change the time to be outside the range of your current time in class. For example, while testing, you may want valid video hours in your trigger to be from 6:00 PM through 8:00 AM.

Page 443: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Oracle Database 11g: Develop PL/SQL Program Units AP-54

Practice Solutions 2-1: Creating the VIDEO_PKG Package

In this practice, you create a package named Ê×ÜÛÑÁÐÕÙ that contains procedures and functions for a video store application.

1) Load and execute the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ´¿¾­ñ¾«·´¼ª·¼ïò­¯´ script to create all the required tables and sequences that are needed for this exercise.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ´¿¾­ñ¾«·´¼ª·¼ïò­¯´ script. The code, the connection prompt, and the results are displayed as follows: ÍÛÌ ÛÝØÑ ÑÚÚ ñö ͽ®·°¬ ¬± ¾«·´¼ ¬¸» Ê·¼»± ß°°´·½¿¬·±² øﮬ ï ó ¾«·´¼ª·¼ïò­¯´÷ º±® ¬¸» Ñ®¿½´» ײ¬®±¼«½¬·±² ¬± Ñ®¿½´» ©·¬¸ Ю±½»¼«®» Þ«·´¼»® ½±«®­»ò Ý®»¿¬»¼ ¾§æ Ü»¾¾§ Õ®¿³»® Ý®»¿¬·±² ¼¿¬»æ ïîñïðñçë Ô¿­¬ «°¿¬»¼æ îñïíñçê Ó±¼·º·»¼ ¾§ Ò¿¹¿ª¿´´· п¬¿¾¿´´¿ ±² îêóßÐÎóîððï Ú±® ¬¸» ½±«®­» ײ¬®±¼«½¬·±² ¬± Ñ®¿½´»ç·æ ÐÔñÍÏÔ Ì¸·­ °¿®¬ ±º ¬¸» ­½®·°¬ ½®»¿¬»­ ¬¿¾´»­ ¿²¼ ­»¯«»²½»­ ¬¸¿¬ ¿®» «­»¼ ¾§ ﮬ Þ ±º ¬¸» ß¼¼·¬·±²¿´ Ю¿½¬·½»­ ±º ¬¸» ½±«®­»ò ×¹²±®» ¬¸» »®®±®­ ©¸·½¸ ¿°°»¿® ¼«» ¬± ¼®±°°·²¹ ±º ¬¿¾´»ò öñ ÜÎÑÐ ÌßÞÔÛ ®»²¬¿´ ÝßÍÝßÜÛ ÝÑÒÍÌÎß×ÒÌÍå ÜÎÑÐ ÌßÞÔÛ ®»­»®ª¿¬·±² ÝßÍÝßÜÛ ÝÑÒÍÌÎß×ÒÌÍå ÜÎÑÐ ÌßÞÔÛ ¬·¬´»Á½±°§ ÝßÍÝßÜÛ ÝÑÒÍÌÎß×ÒÌÍå ÜÎÑÐ ÌßÞÔÛ ¬·¬´» ÝßÍÝßÜÛ ÝÑÒÍÌÎß×ÒÌÍå ÜÎÑÐ ÌßÞÔÛ ³»³¾»® ÝßÍÝßÜÛ ÝÑÒÍÌÎß×ÒÌÍå ÐÎÑÓÐÌ Ð´»¿­» ©¿·¬ ©¸·´» ¬¿¾´»­ ¿®» ½®»¿¬»¼òòòò ÝÎÛßÌÛ ÌßÞÔÛ ÓÛÓÞÛÎ ø³»³¾»®Á·¼ ÒËÓÞÛÎ øïð÷ ÝÑÒÍÌÎß×ÒÌ ³»³¾»®Á·¼Á°µ ÐÎ×ÓßÎÇ ÕÛÇ ô ´¿­¬Á²¿³» ÊßÎÝØßÎîøîë÷ ÝÑÒÍÌÎß×ÒÌ ³»³¾»®Á´¿­¬Á²² ÒÑÌ ÒËÔÔ ô º·®­¬Á²¿³» ÊßÎÝØßÎîøîë÷ ô ¿¼¼®»­­ ÊßÎÝØßÎîøïðð÷ ô ½·¬§ ÊßÎÝØßÎîøíð÷ ô °¸±²» ÊßÎÝØßÎîøîë÷ ô ¶±·²Á¼¿¬» ÜßÌÛ ÜÛÚßËÔÌ ÍÇÍÜßÌÛ ÝÑÒÍÌÎß×ÒÌ ¶±·²Á¼¿¬»Á²² ÒÑÌ ÒËÔÔ÷ ñ ÝÎÛßÌÛ ÌßÞÔÛ Ì×ÌÔÛ ø¬·¬´»Á·¼ ÒËÓÞÛÎøïð÷ ÝÑÒÍÌÎß×ÒÌ ¬·¬´»Á·¼Á°µ ÐÎ×ÓßÎÇ ÕÛÇ ô ¬·¬´» ÊßÎÝØßÎîøêð÷

Page 444: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-55

ÝÑÒÍÌÎß×ÒÌ ¬·¬´»Á²² ÒÑÌ ÒËÔÔ ô ¼»­½®·°¬·±² ÊßÎÝØßÎîøìðð÷ ÝÑÒÍÌÎß×ÒÌ ¬·¬´»Á¼»­½Á²² ÒÑÌ ÒËÔÔ ô ®¿¬·²¹ ÊßÎÝØßÎîøì÷ ÝÑÒÍÌÎß×ÒÌ ¬·¬´»Á®¿¬·²¹Á½µ ÝØÛÝÕ ø®¿¬·²¹ ×Ò øùÙùôùÐÙùôùÎùôùÒÝïéùôùÒÎù÷÷ ô ½¿¬»¹±®§ ÊßÎÝØßÎîøîð÷ ÜÛÚßËÔÌ ùÜÎßÓßù ÝÑÒÍÌÎß×ÒÌ ¬·¬´»Á½¿¬»¹Á½µ ÝØÛÝÕ ø½¿¬»¹±®§ ×Ò øùÜÎßÓßùôùÝÑÓÛÜÇùôùßÝÌ×ÑÒùô ùÝØ×ÔÜùôùÍÝ×Ú×ùôùÜÑÝËÓÛÒÌßÎÇù÷÷ ô ®»´»¿­»Á¼¿¬» ÜßÌÛ÷ ñ ÝÎÛßÌÛ ÌßÞÔÛ Ì×ÌÔÛÁÝÑÐÇ ø½±°§Á·¼ ÒËÓÞÛÎøïð÷ ô ¬·¬´»Á·¼ ÒËÓÞÛÎøïð÷ ÝÑÒÍÌÎß×ÒÌ ½±°§Á¬·¬´»Á·¼Áºµ ÎÛÚÛÎÛÒÝÛÍ ¬·¬´»ø¬·¬´»Á·¼÷ ô ­¬¿¬«­ ÊßÎÝØßÎîøïë÷ ÝÑÒÍÌÎß×ÒÌ ½±°§Á­¬¿¬«­Á²² ÒÑÌ ÒËÔÔ ÝÑÒÍÌÎß×ÒÌ ½±°§Á­¬¿¬«­Á½µ ÝØÛÝÕ ø­¬¿¬«­ ×Ò øùßÊß×ÔßÞÔÛùô ùÜÛÍÌÎÑÇÛÜùô ùÎÛÒÌÛÜùô ùÎÛÍÛÎÊÛÜù÷÷ ô ÝÑÒÍÌÎß×ÒÌ ½±°§Á¬·¬´»Á·¼Á°µ ÐÎ×ÓßÎÇ ÕÛÇø½±°§Á·¼ô ¬·¬´»Á·¼÷÷ ñ ÝÎÛßÌÛ ÌßÞÔÛ ÎÛÒÌßÔ ø¾±±µÁ¼¿¬» ÜßÌÛ ÜÛÚßËÔÌ ÍÇÍÜßÌÛ ô ½±°§Á·¼ ÒËÓÞÛÎøïð÷ ô ³»³¾»®Á·¼ ÒËÓÞÛÎøïð÷ ÝÑÒÍÌÎß×ÒÌ ®»²¬¿´Á³¾®Á·¼Áºµ ÎÛÚÛÎÛÒÝÛÍ ³»³¾»®ø³»³¾»®Á·¼÷ ô ¬·¬´»Á·¼ ÒËÓÞÛÎøïð÷ ô ¿½¬Á®»¬Á¼¿¬» ÜßÌÛ ô »¨°Á®»¬Á¼¿¬» ÜßÌÛ ÜÛÚßËÔÌ ÍÇÍÜßÌÛõî ô ÝÑÒÍÌÎß×ÒÌ ®»²¬¿´Á½±°§Á¬·¬´»Á·¼Áºµ ÚÑÎÛ×ÙÒ ÕÛÇ ø½±°§Á·¼ô ¬·¬´»Á·¼÷ ÎÛÚÛÎÛÒÝÛÍ ¬·¬´»Á½±°§ø½±°§Á·¼ô¬·¬´»Á·¼÷ ô ÝÑÒÍÌÎß×ÒÌ ®»²¬¿´Á·¼Á°µ ÐÎ×ÓßÎÇ ÕÛÇø¾±±µÁ¼¿¬»ô ½±°§Á·¼ô ¬·¬´»Á·¼ô ³»³¾»®Á·¼÷÷ ñ ÝÎÛßÌÛ ÌßÞÔÛ ÎÛÍÛÎÊßÌ×ÑÒ ø®»­Á¼¿¬» ÜßÌÛ ô ³»³¾»®Á·¼ ÒËÓÞÛÎøïð÷ ô ¬·¬´»Á·¼ ÒËÓÞÛÎøïð÷ ô ÝÑÒÍÌÎß×ÒÌ ®»­Á·¼Á°µ ÐÎ×ÓßÎÇ ÕÛÇø®»­Á¼¿¬»ô ³»³¾»®Á·¼ô ¬·¬´»Á·¼÷÷ ñ ÐÎÑÓÐÌ Ì¿¾´»­ ½®»¿¬»¼ò ÜÎÑÐ ÍÛÏËÛÒÝÛ ¬·¬´»Á·¼Á­»¯å ÜÎÑÐ ÍÛÏËÛÒÝÛ ³»³¾»®Á·¼Á­»¯å

Page 445: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-56

ÐÎÑÓÐÌ Ý®»¿¬·²¹ Í»¯«»²½»­òòò ÝÎÛßÌÛ ÍÛÏËÛÒÝÛ ³»³¾»®Á·¼Á­»¯

ÍÌßÎÌ É×ÌØ ïðï ÒÑÝßÝØÛ ÝÎÛßÌÛ ÍÛÏËÛÒÝÛ ¬·¬´»Á·¼Á­»¯ ÍÌßÎÌ É×ÌØ çî ÒÑÝßÝØÛ ñ ÐÎÑÓÐÌ Í»¯«»²½»­ ½®»¿¬»¼ò ÐÎÑÓÐÌ Î«² ¾«·´¼ª·¼îò­¯´ ²±© ¬± °±°«´¿¬» ¬¸» ¿¾±ª» ¬¿¾´»­ò

Page 446: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-57

2) Load and execute the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«/´¿¾­ñ¾«·´¼ª·¼îò­¯´ script to populate all the tables created through the ¾«·´¼ª·¼ïò­¯´ script.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ´¿¾­ñ¾«·´¼ª·¼îò­¯´ script. The code, the connection prompt, and the results are displayed as follows: ñö ͽ®·°¬ ¬± ¾«·´¼ ¬¸» Ê·¼»± ß°°´·½¿¬·±² øﮬ î ó ¾«·´¼ª·¼îò­¯´÷

Page 447: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-58

̸·­ °¿®¬ ±º ¬¸» ­½®·°¬ °±°«´¿¬»­ ¬¸» ¬¿¾´»­ ¬¸¿¬ ¿®» ½®»¿¬»¼ «­·²¹ ¾«·´¼ª·¼ïò­¯´ ̸»­» ¿®» «­»¼ ¾§ ﮬ Þ ±º ¬¸» ß¼¼·¬·±²¿´ Ю¿½¬·½»­ ±º ¬¸» ½±«®­»ò DZ« ­¸±«´¼ ®«² ¬¸» ­½®·°¬ ¾«·´¼ª·¼ïò­¯´ ¾»º±®» ®«²²·²¹ ¬¸·­ ­½®·°¬ ¬± ½®»¿¬» ¬¸» ¿¾±ª» ¬¿¾´»­ò öñ ×ÒÍÛÎÌ ×ÒÌÑ ³»³¾»® ÊßÔËÛÍ ø³»³¾»®Á·¼Á­»¯òÒÛÈÌÊßÔô ùÊ»´¿­¯«»¦ùô ùÝ¿®³»²ùô ùîèí Õ·²¹ ͬ®»»¬ùô ùÍ»¿¬¬´»ùô ùëèéóççóêêêêùô ùðíóÓßÎóçðù÷å ×ÒÍÛÎÌ ×ÒÌÑ ³»³¾»® ÊßÔËÛÍ ø³»³¾»®Á·¼Á­»¯òÒÛÈÌÊßÔô ùÒ¹¿±ùô ùԿܱ®·­ùô ùë Ó±¼®¿²§ùô ùÞ®¿¬·­´¿ª¿ùô ùëèêóíëëóèèèîùô ùðèóÓßÎóçðù÷å ×ÒÍÛÎÌ ×ÒÌÑ ³»³¾»® ÊßÔËÛÍ ø³»³¾»®Á·¼Á­»¯òÒÛÈÌÊßÔôùÒ¿¹¿§¿³¿ùô ùÓ·¼±®·ùô ùêè Ê·¿ Ý»²¬®¿´»ùô ùÍ¿± п±´±ùô ùîëìóèëîóëéêìùô ùïéóÖËÒóçïù÷å ×ÒÍÛÎÌ ×ÒÌÑ ³»³¾»® ÊßÔËÛÍ ø³»³¾»®Á·¼Á­»¯òÒÛÈÌÊßÔôùÏ«·½µó̱óÍ»»ùôùÓ¿®µùô ùêçîï Õ·²¹ É¿§ùô ùÔ¿¹±­ùô ùêíóëëçóéééùô ùðéóßÐÎóçðù÷å ×ÒÍÛÎÌ ×ÒÌÑ ³»³¾»® ÊßÔËÛÍ ø³»³¾»®Á·¼Á­»¯òÒÛÈÌÊßÔô ùα°»¾«®²ùô ùß«¼®§ùô ùèê ݸ« ͬ®»»¬ùô ùر²¹ Õ±²¹ùô ùìïóëëçóèéùô ùðìóÓßÎóçðù÷å ×ÒÍÛÎÌ ×ÒÌÑ ³»³¾»® ÊßÔËÛÍ ø³»³¾»®Á·¼Á­»¯òÒÛÈÌÊßÔô ùË®¹«¸¿®¬ùô ùÓ±´´§ùô ùíðíë Ô¿«®·»® Þ´ª¼òùô ùÏ«»¾»½ùô ùìïèóëìîóççèèùôùïèóÖßÒóçïù÷å ×ÒÍÛÎÌ ×ÒÌÑ ³»³¾»® ÊßÔËÛÍ ø³»³¾»®Á·¼Á­»¯òÒÛÈÌÊßÔô ùÓ»²½¸«ùô ùα¾»®¬¿ùô ùÞ±«´»ª¿®¼ ¼» É¿¬»®´±± ìïùô ùÞ®«­­»´­ùô ùíîîóëðìóîîîèùô ùïìóÓßÇóçðù÷å ×ÒÍÛÎÌ ×ÒÌÑ ³»³¾»® ÊßÔËÛÍ ø³»³¾»®Á·¼Á­»¯òÒÛÈÌÊßÔô ùÞ·®·ùô ùÞ»²ùô ùíçè Ø·¹¸ ͬòùô ùݱ´«³¾«­ùô ùêïìóìëëóçèêíùô ùðéóßÐÎóçðù÷å ×ÒÍÛÎÌ ×ÒÌÑ ³»³¾»® ÊßÔËÛÍ ø³»³¾»®Á·¼Á­»¯òÒÛÈÌÊßÔô ùÝ¿¬½¸°±´»ùô ùß²¬±·²»¬¬»ùô ùèè ß´º®»¼ ͬòùô ùÞ®·­¾¿²»ùô ùêïêóíççóïìïïùô ùðçóÚÛÞóçîù÷å ÝÑÓÓ×Ìå ×ÒÍÛÎÌ ×ÒÌÑ Ì×ÌÔÛ ø¬·¬´»Á·¼ô ¬·¬´»ô ¼»­½®·°¬·±²ô ®¿¬·²¹ô ½¿¬»¹±®§ô ®»´»¿­»Á¼¿¬»÷ ÊßÔËÛÍ øÌ×ÌÔÛÁ×ÜÁÍÛÏòÒÛÈÌÊßÔô ùÉ·´´·» ¿²¼ ݸ®·­¬³¿­ ̱±ùô ùß´´ ±º É·´´·»ùù­ º®·»²¼­ ³¿¼» ¿ ݸ®·­¬³¿­ ´·­¬ º±® Í¿²¬¿ô ¾«¬ É·´´·» ¸¿­ §»¬ ¬± ½®»¿¬» ¸·­ ±©² ©·­¸ ´·­¬òùô ùÙùô ùÝØ×ÔÜùô ùðëóÑÝÌóçëù÷å ×ÒÍÛÎÌ ×ÒÌÑ Ì×ÌÔÛ ø¬·¬´»Á·¼ô ¬·¬´»ô ¼»­½®·°¬·±²ô ®¿¬·²¹ô ½¿¬»¹±®§ô ®»´»¿­»Á¼¿¬»÷

Page 448: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-59

ÊßÔËÛÍ øÌ×ÌÔÛÁ×ÜÁÍÛÏòÒÛÈÌÊßÔô ùß´·»² ß¹¿·²ùô ùß²±¬¸»® ·²­¬¿´´³»²¬ ±º ­½·»²½» º·½¬·±² ¸·­¬±®§ò Ý¿² ¬¸» ¸»®±·²» ­¿ª» ¬¸» °´¿²»¬ º®±³ ¬¸» ¿´·»² ´·º» º±®³áùô ùÎùô ùÍÝ×Ú×ùô ùïçóÓßÇóçëù÷å ×ÒÍÛÎÌ ×ÒÌÑ Ì×ÌÔÛ ø¬·¬´»Á·¼ô ¬·¬´»ô ¼»­½®·°¬·±²ô ®¿¬·²¹ô ½¿¬»¹±®§ô ®»´»¿­»Á¼¿¬»÷ ÊßÔËÛÍ øÌ×ÌÔÛÁ×ÜÁÍÛÏòÒÛÈÌÊßÔô ù̸» Ù´±¾ùô ùß ³»¬»±® ½®¿­¸»­ ²»¿® ¿ ­³¿´´ ß³»®·½¿² ¬±©² ¿²¼ «²´»¿­¸»­ ½¿®·ª±®±«­ ¹±± ·² ¬¸·­ ½´¿­­·½òùô ùÒÎùô ùÍÝ×Ú×ùô ùïîóßËÙóçëù÷å ×ÒÍÛÎÌ ×ÒÌÑ Ì×ÌÔÛ ø¬·¬´»Á·¼ô ¬·¬´»ô ¼»­½®·°¬·±²ô ®¿¬·²¹ô ½¿¬»¹±®§ô ®»´»¿­»Á¼¿¬»÷ ÊßÔËÛÍ øÌ×ÌÔÛÁ×ÜÁÍÛÏòÒÛÈÌÊßÔô ùÓ§ Ü¿§ Ѻºùô ùÉ·¬¸ ¿ ´·¬¬´» ´«½µ ¿²¼ ¿ ´±¬ ±º ·²¹»²«·¬§ô ¿ ¬»»²¿¹»® ­µ·°­ ­½¸±±´ º±® ¿ ¼¿§ ·² Ò»© DZ®µòùô ùÐÙùô ùÝÑÓÛÜÇùô ùïîóÖËÔóçëù÷å ×ÒÍÛÎÌ ×ÒÌÑ Ì×ÌÔÛ ø¬·¬´»Á·¼ô ¬·¬´»ô ¼»­½®·°¬·±²ô ®¿¬·²¹ô ½¿¬»¹±®§ô ®»´»¿­»Á¼¿¬»÷ ÊßÔËÛÍ øÌ×ÌÔÛÁ×ÜÁÍÛÏòÒÛÈÌÊßÔô ùÓ·®¿½´»­ ±² ×½»ùô ùß ­·¨ó§»¿®ó±´¼ ¸¿­ ¼±«¾¬­ ¿¾±«¬ Í¿²¬¿ Ý´¿«­ò Þ«¬ ­¸» ¼·­½±ª»®­ ¬¸¿¬ ³·®¿½´»­ ®»¿´´§ ¼± »¨·­¬òùô ùÐÙùô ùÜÎßÓßùô ùïîóÍÛÐóçëù÷å ×ÒÍÛÎÌ ×ÒÌÑ Ì×ÌÔÛ ø¬·¬´»Á·¼ô ¬·¬´»ô ¼»­½®·°¬·±²ô ®¿¬·²¹ô ½¿¬»¹±®§ô ®»´»¿­»Á¼¿¬»÷ ÊßÔËÛÍ øÌ×ÌÔÛÁ×ÜÁÍÛÏòÒÛÈÌÊßÔô ùͱ¼¿ Ù¿²¹ùô ùߺ¬»® ¼·­½±ª»®·²¹ ¿ ½¿½¸»¼ ±º ¼®«¹­ô ¿ §±«²¹ ½±«°´» º·²¼ ¬¸»³­»´ª»­ °·¬¬»¼ ¿¹¿·²­¬ ¿ ª·½·±«­ ¹¿²¹òùô ùÒÎùô ùßÝÌ×ÑÒùô ùðïóÖËÒóçëù÷å ×ÒÍÛÎÌ ×ÒÌÑ ¬·¬´» ø¬·¬´»Á·¼ô ¬·¬´»ô ¼»­½®·°¬·±²ô ®¿¬·²¹ô ½¿¬»¹±®§ô ®»´»¿­»Á¼¿¬»÷ ÊßÔËÛÍ øÌ×ÌÔÛÁ×ÜÁÍÛÏòÒÛÈÌÊßÔô ùײ¬»®­¬»´´¿® É¿®­ùô ùÚ«¬«®·­¬·½ ·²¬»®­¬»´´¿® ¿½¬·±² ³±ª·»ò Ý¿² ¬¸» ®»¾»´­ ­¿ª» ¬¸» ¸«³¿²­ º®±³ ¬¸» »ª·´ Û³°·®»áùô ùÐÙùô ùÍÝ×Ú×ùôùðéóÖËÔóééù÷å ÝÑÓÓ×Ìå ×ÒÍÛÎÌ ×ÒÌÑ ¬·¬´»Á½±°§ ÊßÔËÛÍ øïôçîô ùßÊß×ÔßÞÔÛù÷å ×ÒÍÛÎÌ ×ÒÌÑ ¬·¬´»Á½±°§ ÊßÔËÛÍ øïôçíô ùßÊß×ÔßÞÔÛù÷å ×ÒÍÛÎÌ ×ÒÌÑ ¬·¬´»Á½±°§ ÊßÔËÛÍ øîôçíô ùÎÛÒÌÛÜù÷å ×ÒÍÛÎÌ ×ÒÌÑ ¬·¬´»Á½±°§ ÊßÔËÛÍ øïôçìô ùßÊß×ÔßÞÔÛù÷å ×ÒÍÛÎÌ ×ÒÌÑ ¬·¬´»Á½±°§ ÊßÔËÛÍ øïôçëô ùßÊß×ÔßÞÔÛù÷å ×ÒÍÛÎÌ ×ÒÌÑ ¬·¬´»Á½±°§ ÊßÔËÛÍ øîôçëô ùßÊß×ÔßÞÔÛù÷å ×ÒÍÛÎÌ ×ÒÌÑ ¬·¬´»Á½±°§ ÊßÔËÛÍ øíôçëô ùÎÛÒÌÛÜù÷å ×ÒÍÛÎÌ ×ÒÌÑ ¬·¬´»Á½±°§ ÊßÔËÛÍ øïôçêô ùßÊß×ÔßÞÔÛù÷å ×ÒÍÛÎÌ ×ÒÌÑ ¬·¬´»Á½±°§ ÊßÔËÛÍ øïôçéô ùßÊß×ÔßÞÔÛù÷å ×ÒÍÛÎÌ ×ÒÌÑ ¬·¬´»Á½±°§ ÊßÔËÛÍ øïôçèô ùÎÛÒÌÛÜù÷å ×ÒÍÛÎÌ ×ÒÌÑ ¬·¬´»Á½±°§ ÊßÔËÛÍ øîôçèô ùßÊß×ÔßÞÔÛù÷å ÝÑÓÓ×Ìå ×ÒÍÛÎÌ ×ÒÌÑ ®»­»®ª¿¬·±² ÊßÔËÛÍ ø­§­¼¿¬»óïô ïðïô çí÷å ×ÒÍÛÎÌ ×ÒÌÑ ®»­»®ª¿¬·±² ÊßÔËÛÍ ø­§­¼¿¬»óîô ïðêô ïðî÷å ÝÑÓÓ×Ìå

Page 449: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-60

×ÒÍÛÎÌ ×ÒÌÑ ®»²¬¿´ ÊßÔËÛÍ ø­§­¼¿¬»óïô îô ïðïô çíô ²«´´ô ­§­¼¿¬»õï÷å ×ÒÍÛÎÌ ×ÒÌÑ ®»²¬¿´ ÊßÔËÛÍ ø­§­¼¿¬»óîô íô ïðîô çëô ²«´´ô ­§­¼¿¬»÷å

×ÒÍÛÎÌ ×ÒÌÑ ®»²¬¿´ ÊßÔËÛÍ ø­§­¼¿¬»óíô ïô ïðïô çèô ²«´´ô ­§­¼¿¬»óï÷å ×ÒÍÛÎÌ ×ÒÌÑ ®»²¬¿´ ÊßÔËÛÍ ø­§­¼¿¬»óìô ïô ïðêô çéô ­§­¼¿¬»óîô ­§­¼¿¬»óî÷å ×ÒÍÛÎÌ ×ÒÌÑ ®»²¬¿´ ÊßÔËÛÍ ø­§­¼¿¬»óíô ïô ïðïô çîô ­§­¼¿¬»óîô ­§­¼¿¬»óï÷å ÝÑÓÓ×Ìå ÐÎÑÓÐÌ öö Ì¿¾´»­ ¾«·´¬ ¿²¼ ¼¿¬¿ ´±¿¼»¼ öö

Page 450: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-61

3) Create a package named Ê×ÜÛÑÁÐÕÙ with the following procedures and functions:

a) ÒÛÉÁÓÛÓÞÛÎ: A public procedure that adds a new member to the ÓÛÓÞÛÎ table. For the member ×Ü number, use the sequence ÓÛÓÞÛÎÁ×ÜÁÍÛÏ; for the join date, use ÍÇÍÜßÌÛ. Pass all other values to be inserted into a new row as parameters.

b) ÒÛÉÁÎÛÒÌßÔ: An overloaded public function to record a new rental. Pass the title ID number for the video that a customer wants to rent, and either the customer�s last name or his member ID number into the function. The function should return the due date for the video. Due dates are three days from the date the video is rented. If the status for a movie requested is listed as ßÊß×ÔßÞÔÛ in the Ì×ÌÔÛÁÝÑÐÇ table for one copy of this title, then update this Ì×ÌÔÛÁÝÑÐÇ table and set the status to ÎÛÒÌÛÜ. If there is no copy available, the function must return ÒËÔÔ. Then, insert a new record into the ÎÛÒÌßÔ table identifying the booked date as today�s date, the copy ID number, the member ID number, the title ID number, and the expected return date. Be aware of multiple customers with the same last name. In this case, have the function return ÒËÔÔ, and display a list of the customers� names that match and their ID numbers.

c) ÎÛÌËÎÒÁÓÑÊ×Û: A public procedure that updates the status of a video (available, rented, or damaged) and sets the return date. Pass the title ID, the copy ID, and the status to this procedure. Check whether there are reservations for that title and display a message if it is reserved. Update the ÎÛÒÌßÔ table and set the actual return date to today�s date. Update the status in the Ì×ÌÔÛÁÝÑÐÇ table based on the status parameter passed into the procedure.

d) ÎÛÍÛÎÊÛÁÓÑÊ×Û: A private procedure that executes only if all the video copies requested in the ÒÛÉÁÎÛÒÌßÔ procedure have a status of ÎÛÒÌÛÜ. Pass the member ID number and the title ID number to this procedure. Insert a new record into the ÎÛÍÛÎÊßÌ×ÑÒ table and record the reservation date, member ID number, and title ID number. Print a message indicating that a movie is reserved and its expected date of return.

Page 451: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-62

e) ÛÈÝÛÐÌ×ÑÒÁØßÒÜÔÛÎ: A private procedure that is called from the exception handler of the public programs. Pass the SQLCODE number to this procedure, and the name of the program (as a text string) where the error occurred. Use Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ to raise a customized error. Start with a unique key violation (-1) and foreign key violation (-2292). Allow the exception handler to raise a generic error for any other errors.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðîÁðïÁðíò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

Ê×ÜÛÑÁÐÕÙ Package Specification

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ª·¼»±Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ ²»©Á³»³¾»® ø°Á´²¿³» ×Ò ³»³¾»®ò´¿­¬Á²¿³»ûÌÇÐÛô °Áº²¿³» ×Ò ³»³¾»®òº·®­¬Á²¿³»ûÌÇÐÛ ÜÛÚßËÔÌ ÒËÔÔô °Á¿¼¼®»­­ ×Ò ³»³¾»®ò¿¼¼®»­­ûÌÇÐÛ ÜÛÚßËÔÌ ÒËÔÔô °Á½·¬§ ×Ò ³»³¾»®ò½·¬§ûÌÇÐÛ ÜÛÚßËÔÌ ÒËÔÔô °Á°¸±²» ×Ò ³»³¾»®ò°¸±²»ûÌÇÐÛ ÜÛÚßËÔÌ ÒËÔÔ÷å ÚËÒÝÌ×ÑÒ ²»©Á®»²¬¿´ ø°Á³»³¾»®·¼ ×Ò ®»²¬¿´ò³»³¾»®Á·¼ûÌÇÐÛô °Á¬·¬´»·¼ ×Ò ®»²¬¿´ò¬·¬´»Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÜßÌÛå ÚËÒÝÌ×ÑÒ ²»©Á®»²¬¿´ ø°Á³»³¾»®²¿³» ×Ò ³»³¾»®ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¬·¬´»·¼ ×Ò ®»²¬¿´ò¬·¬´»Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÜßÌÛå ÐÎÑÝÛÜËÎÛ ®»¬«®²Á³±ª·» ø°Á¬·¬´»·¼ ×Ò ®»²¬¿´ò¬·¬´»Á·¼ûÌÇÐÛô °Á½±°§·¼ ×Ò ®»²¬¿´ò½±°§Á·¼ûÌÇÐÛô °Á­¬­ ×Ò ¬·¬´»Á½±°§ò­¬¿¬«­ûÌÇÐÛ÷å ÛÒÜ ª·¼»±Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ ª·¼»±Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ »¨½»°¬·±²Á¸¿²¼´»®ø»®®½±¼» ×Ò ÒËÓÞÛÎô ½±²¬»¨¬ ×Ò ÊßÎÝØßÎî÷ ×Í ÞÛÙ×Ò ×Ú »®®½±¼» ã óï ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðððïô ù̸» ²«³¾»® ·­ ¿­­·¹²»¼ ¬± ¬¸·­ ³»³¾»® ·­ ¿´®»¿¼§ ·² «­»ô ù¤¤ ù¬®§ ¿¹¿·²òù÷å ÛÔÍ×Ú »®®½±¼» ã óîîçï ÌØÛÒ

Page 452: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-63

Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðððîô ½±²¬»¨¬ ¤¤ ù ¸¿­ ¿¬¬»³°¬»¼ ¬± «­» ¿ º±®»·¹² µ»§ ª¿´«» ¬¸¿¬ ·­ ·²ª¿´·¼ù÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðçççô ù˲¸¿²¼´»¼ »®®±® ·² ù ¤¤ ½±²¬»¨¬ ¤¤ ùò д»¿­» ½±²¬¿½¬ §±«® ¿°°´·½¿¬·±² ù¤¤ ù¿¼³·²·­¬®¿¬±® ©·¬¸ ¬¸» º±´´±©·²¹ ·²º±®³¿¬·±²æ ù ¤¤ ÝØÎøïí÷ ¤¤ ÍÏÔÛÎÎÓ÷å ÛÒÜ ×Úå ÛÒÜ »¨½»°¬·±²Á¸¿²¼´»®å ÐÎÑÝÛÜËÎÛ ®»­»®ª»Á³±ª·» ø³»³¾»®·¼ ×Ò ®»­»®ª¿¬·±²ò³»³¾»®Á·¼ûÌÇÐÛô ¬·¬´»·¼ ×Ò ®»­»®ª¿¬·±²ò¬·¬´»Á·¼ûÌÇÐÛ÷ ×Í ÝËÎÍÑÎ ®»²¬»¼Á½­® ×Í ÍÛÔÛÝÌ »¨°Á®»¬Á¼¿¬» ÚÎÑÓ ®»²¬¿´ ÉØÛÎÛ ¬·¬´»Á·¼ ã ¬·¬´»·¼ ßÒÜ ¿½¬Á®»¬Á¼¿¬» ×Í ÒËÔÔå ÞÛÙ×Ò ×ÒÍÛÎÌ ×ÒÌÑ ®»­»®ª¿¬·±² ø®»­Á¼¿¬»ô ³»³¾»®Á·¼ô ¬·¬´»Á·¼÷ ÊßÔËÛÍ øÍÇÍÜßÌÛô ³»³¾»®·¼ô ¬·¬´»·¼÷å ÝÑÓÓ×Ìå ÚÑÎ ®»²¬»¼Á®»½ ×Ò ®»²¬»¼Á½­® ÔÑÑÐ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùÓ±ª·» ®»­»®ª»¼ò Û¨°»½¬»¼ ¾¿½µ ±²æ ù ¤¤ ®»²¬»¼Á®»½ò»¨°Á®»¬Á¼¿¬»÷å ÛÈ×Ì ÉØÛÒ ®»²¬»¼Á½­®ûº±«²¼å ÛÒÜ ÔÑÑÐå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÑÌØÛÎÍ ÌØÛÒ »¨½»°¬·±²Á¸¿²¼´»®øÍÏÔÝÑÜÛô ùÎÛÍÛÎÊÛÁÓÑÊ×Ûù÷å ÛÒÜ ®»­»®ª»Á³±ª·»å ÐÎÑÝÛÜËÎÛ ®»¬«®²Á³±ª·»ø ¬·¬´»·¼ ×Ò ®»²¬¿´ò¬·¬´»Á·¼ûÌÇÐÛô ½±°§·¼ ×Ò ®»²¬¿´ò½±°§Á·¼ûÌÇÐÛô ­¬­ ×Ò ¬·¬´»Á½±°§ò­¬¿¬«­ûÌÇÐÛ÷ ×Í ªÁ¼«³³§ ÊßÎÝØßÎîøï÷å ÝËÎÍÑÎ ®»­Á½­® ×Í ÍÛÔÛÝÌ ö ÚÎÑÓ ®»­»®ª¿¬·±² ÉØÛÎÛ ¬·¬´»Á·¼ ã ¬·¬´»·¼å ÞÛÙ×Ò ÍÛÔÛÝÌ ùù ×ÒÌÑ ªÁ¼«³³§ ÚÎÑÓ ¬·¬´» ÉØÛÎÛ ¬·¬´»Á·¼ ã ¬·¬´»·¼å ËÐÜßÌÛ ®»²¬¿´ ÍÛÌ ¿½¬Á®»¬Á¼¿¬» ã ÍÇÍÜßÌÛ ÉØÛÎÛ ¬·¬´»Á·¼ ã ¬·¬´»·¼ ßÒÜ ½±°§Á·¼ ã ½±°§·¼ ßÒÜ ¿½¬Á®»¬Á¼¿¬» ×Í ÒËÔÔå

Page 453: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-64

ËÐÜßÌÛ ¬·¬´»Á½±°§ ÍÛÌ ­¬¿¬«­ ã ËÐÐÛÎø­¬­÷

ÉØÛÎÛ ¬·¬´»Á·¼ ã ¬·¬´»·¼ ßÒÜ ½±°§Á·¼ ã ½±°§·¼å ÚÑÎ ®»­Á®»½ ×Ò ®»­Á½­® ÔÑÑÐ ×Ú ®»­Á½­®ûÚÑËÒÜ ÌØÛÒ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùЫ¬ ¬¸·­ ³±ª·» ±² ¸±´¼ óó ù¤¤ ù®»­»®ª»¼ ¾§ ³»³¾»® ýù ¤¤ ®»­Á®»½ò³»³¾»®Á·¼÷å ÛÒÜ ×Úå ÛÒÜ ÔÑÑÐå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÑÌØÛÎÍ ÌØÛÒ »¨½»°¬·±²Á¸¿²¼´»®øÍÏÔÝÑÜÛô ùÎÛÌËÎÒÁÓÑÊ×Ûù÷å ÛÒÜ ®»¬«®²Á³±ª·»å ÚËÒÝÌ×ÑÒ ²»©Á®»²¬¿´ø ³»³¾»®·¼ ×Ò ®»²¬¿´ò³»³¾»®Á·¼ûÌÇÐÛô ¬·¬´»·¼ ×Ò ®»²¬¿´ò¬·¬´»Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÜßÌÛ ×Í ÝËÎÍÑÎ ½±°§Á½­® ×Í ÍÛÔÛÝÌ ö ÚÎÑÓ ¬·¬´»Á½±°§ ÉØÛÎÛ ¬·¬´»Á·¼ ã ¬·¬´»·¼ ÚÑÎ ËÐÜßÌÛå º´¿¹ ÞÑÑÔÛßÒ æã ÚßÔÍÛå ÞÛÙ×Ò ÚÑÎ ½±°§Á®»½ ×Ò ½±°§Á½­® ÔÑÑÐ ×Ú ½±°§Á®»½ò­¬¿¬«­ ã ùßÊß×ÔßÞÔÛù ÌØÛÒ ËÐÜßÌÛ ¬·¬´»Á½±°§ ÍÛÌ ­¬¿¬«­ ã ùÎÛÒÌÛÜù ÉØÛÎÛ ÝËÎÎÛÒÌ ÑÚ ½±°§Á½­®å ×ÒÍÛÎÌ ×ÒÌÑ ®»²¬¿´ø¾±±µÁ¼¿¬»ô ½±°§Á·¼ô ³»³¾»®Á·¼ô ¬·¬´»Á·¼ô »¨°Á®»¬Á¼¿¬»÷ ÊßÔËÛÍ øÍÇÍÜßÌÛô ½±°§Á®»½ò½±°§Á·¼ô ³»³¾»®·¼ô ¬·¬´»·¼ô ÍÇÍÜßÌÛ õ í÷å º´¿¹ æã ÌÎËÛå ÛÈ×Ìå ÛÒÜ ×Úå ÛÒÜ ÔÑÑÐå ÝÑÓÓ×Ìå ×Ú º´¿¹ ÌØÛÒ ÎÛÌËÎÒ øÍÇÍÜßÌÛ õ í÷å ÛÔÍÛ ®»­»®ª»Á³±ª·»ø³»³¾»®·¼ô ¬·¬´»·¼÷å ÎÛÌËÎÒ ÒËÔÔå ÛÒÜ ×Úå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÑÌØÛÎÍ ÌØÛÒ »¨½»°¬·±²Á¸¿²¼´»®øÍÏÔÝÑÜÛô ùÒÛÉÁÎÛÒÌßÔù÷å ÛÒÜ ²»©Á®»²¬¿´å ÚËÒÝÌ×ÑÒ ²»©Á®»²¬¿´ø ³»³¾»®²¿³» ×Ò ³»³¾»®ò´¿­¬Á²¿³»ûÌÇÐÛô

Page 454: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-65

¬·¬´»·¼ ×Ò ®»²¬¿´ò¬·¬´»Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÜßÌÛ ×Í ÝËÎÍÑÎ ½±°§Á½­® ×Í

ÍÛÔÛÝÌ ö ÚÎÑÓ ¬·¬´»Á½±°§

ÉØÛÎÛ ¬·¬´»Á·¼ ã ¬·¬´»·¼ ÚÑÎ ËÐÜßÌÛå º´¿¹ ÞÑÑÔÛßÒ æã ÚßÔÍÛå ³»³¾»®·¼ ³»³¾»®ò³»³¾»®Á·¼ûÌÇÐÛå ÝËÎÍÑÎ ³»³¾»®Á½­® ×Í ÍÛÔÛÝÌ ³»³¾»®Á·¼ô ´¿­¬Á²¿³»ô º·®­¬Á²¿³» ÚÎÑÓ ³»³¾»® ÉØÛÎÛ ÔÑÉÛÎø´¿­¬Á²¿³»÷ ã ÔÑÉÛÎø³»³¾»®²¿³»÷ ÑÎÜÛÎ ÞÇ ´¿­¬Á²¿³»ô º·®­¬Á²¿³»å ÞÛÙ×Ò ÍÛÔÛÝÌ ³»³¾»®Á·¼ ×ÒÌÑ ³»³¾»®·¼ ÚÎÑÓ ³»³¾»® ÉØÛÎÛ ´±©»®ø´¿­¬Á²¿³»÷ ã ´±©»®ø³»³¾»®²¿³»÷å ÚÑÎ ½±°§Á®»½ ×Ò ½±°§Á½­® ÔÑÑÐ ×Ú ½±°§Á®»½ò­¬¿¬«­ ã ùßÊß×ÔßÞÔÛù ÌØÛÒ ËÐÜßÌÛ ¬·¬´»Á½±°§ ÍÛÌ ­¬¿¬«­ ã ùÎÛÒÌÛÜù ÉØÛÎÛ ÝËÎÎÛÒÌ ÑÚ ½±°§Á½­®å ×ÒÍÛÎÌ ×ÒÌÑ ®»²¬¿´ ø¾±±µÁ¼¿¬»ô ½±°§Á·¼ô ³»³¾»®Á·¼ô ¬·¬´»Á·¼ô »¨°Á®»¬Á¼¿¬»÷ ÊßÔËÛÍ øÍÇÍÜßÌÛô ½±°§Á®»½ò½±°§Á·¼ô ³»³¾»®·¼ô ¬·¬´»·¼ô ÍÇÍÜßÌÛ õ í÷å º´¿¹ æã ÌÎËÛå ÛÈ×Ìå ÛÒÜ ×Úå ÛÒÜ ÔÑÑÐå ÝÑÓÓ×Ìå ×Ú º´¿¹ ÌØÛÒ ÎÛÌËÎÒøÍÇÍÜßÌÛ õ í÷å ÛÔÍÛ ®»­»®ª»Á³±ª·»ø³»³¾»®·¼ô ¬·¬´»·¼÷å ÎÛÌËÎÒ ÒËÔÔå ÛÒÜ ×Úå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÌÑÑÁÓßÒÇÁÎÑÉÍ ÌØÛÒ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø ùÉ¿®²·²¹ÿ Ó±®» ¬¸¿² ±²» ³»³¾»® ¾§ ¬¸·­ ²¿³»òù÷å ÚÑÎ ³»³¾»®Á®»½ ×Ò ³»³¾»®Á½­® ÔÑÑÐ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø³»³¾»®Á®»½ò³»³¾»®Á·¼ ¤¤ ÝØÎøç÷ ¤¤ ³»³¾»®Á®»½ò´¿­¬Á²¿³» ¤¤ ùô ù ¤¤ ³»³¾»®Á®»½òº·®­¬Á²¿³»÷å ÛÒÜ ÔÑÑÐå ÎÛÌËÎÒ ÒËÔÔå ÉØÛÒ ÑÌØÛÎÍ ÌØÛÒ »¨½»°¬·±²Á¸¿²¼´»®øÍÏÔÝÑÜÛô ùÒÛÉÁÎÛÒÌßÔù÷å ÛÒÜ ²»©Á®»²¬¿´å

Page 455: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-66

ÐÎÑÝÛÜËÎÛ ²»©Á³»³¾»®ø ´²¿³» ×Ò ³»³¾»®ò´¿­¬Á²¿³»ûÌÇÐÛô º²¿³» ×Ò ³»³¾»®òº·®­¬Á²¿³»ûÌÇÐÛ ÜÛÚßËÔÌ ÒËÔÔô

¿¼¼®»­­ ×Ò ³»³¾»®ò¿¼¼®»­­ûÌÇÐÛ ÜÛÚßËÔÌ ÒËÔÔô

½·¬§ ×Ò ³»³¾»®ò½·¬§ûÌÇÐÛ ÜÛÚßËÔÌ ÒËÔÔô °¸±²» ×Ò ³»³¾»®ò°¸±²»ûÌÇÐÛ ÜÛÚßËÔÌ ÒËÔÔ÷ ×Í ÞÛÙ×Ò ×ÒÍÛÎÌ ×ÒÌÑ ³»³¾»®ø³»³¾»®Á·¼ô ´¿­¬Á²¿³»ô º·®­¬Á²¿³»ô ¿¼¼®»­­ô ½·¬§ô °¸±²»ô ¶±·²Á¼¿¬»÷ ÊßÔËÛÍø³»³¾»®Á·¼Á­»¯òÒÛÈÌÊßÔô ´²¿³»ô º²¿³»ô ¿¼¼®»­­ô ½·¬§ô °¸±²»ô ÍÇÍÜßÌÛ÷å ÝÑÓÓ×Ìå ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐßÝÕßÙÛ ÞÑÜÇ ª·¼»±Á°µ¹ ×Í ÐÎÑÝÛÜËÎÛ »¨½»°¬·±²Á¸¿²¼´»®ø»®®½±¼» ×Ò ÒËÓÞÛÎô °Á½±²¬»¨¬ ×Ò ÊßÎÝØßÎî÷ ×Í ÞÛÙ×Ò ×Ú »®®½±¼» ã óï ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðððïô ù̸» ²«³¾»® ·­ ¿­­·¹²»¼ ¬± ¬¸·­ ³»³¾»® ·­ ¿´®»¿¼§ ·² «­»ô ù¤¤ ù¬®§ ¿¹¿·²òù÷å ÛÔÍ×Ú »®®½±¼» ã óîîçï ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðððîô °Á½±²¬»¨¬ ¤¤ ù ¸¿­ ¿¬¬»³°¬»¼ ¬± «­» ¿ º±®»·¹² µ»§ ª¿´«» ¬¸¿¬ ·­ ·²ª¿´·¼ù÷å ÛÔÍÛ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðçççô ù˲¸¿²¼´»¼ »®®±® ·² ù ¤¤ °Á½±²¬»¨¬ ¤¤ ùò д»¿­» ½±²¬¿½¬ §±«® ¿°°´·½¿¬·±² ù¤¤ ù¿¼³·²·­¬®¿¬±® ©·¬¸ ¬¸» º±´´±©·²¹ ·²º±®³¿¬·±²æ ù ¤¤ ÝØÎøïí÷ ¤¤ ÍÏÔÛÎÎÓ÷å ÛÒÜ ×Úå ÛÒÜ »¨½»°¬·±²Á¸¿²¼´»®å ÐÎÑÝÛÜËÎÛ ®»­»®ª»Á³±ª·» ø°Á³»³¾»®·¼ ×Ò ®»­»®ª¿¬·±²ò³»³¾»®Á·¼ûÌÇÐÛô °Á¬·¬´»·¼ ×Ò ®»­»®ª¿¬·±²ò¬·¬´»Á·¼ûÌÇÐÛ÷ ×Í ÝËÎÍÑÎ ½Á®»²¬»¼Á½­® ×Í ÍÛÔÛÝÌ »¨°Á®»¬Á¼¿¬» ÚÎÑÓ ®»²¬¿´ ÉØÛÎÛ ¬·¬´»Á·¼ ã °Á¬·¬´»·¼ ßÒÜ ¿½¬Á®»¬Á¼¿¬» ×Í ÒËÔÔå ÞÛÙ×Ò ×ÒÍÛÎÌ ×ÒÌÑ ®»­»®ª¿¬·±² ø®»­Á¼¿¬»ô ³»³¾»®Á·¼ô ¬·¬´»Á·¼÷ ÊßÔËÛÍ øÍÇÍÜßÌÛô °Á³»³¾»®·¼ô °Á¬·¬´»·¼÷å ÝÑÓÓ×Ìå ÚÑÎ ®»²¬»¼Á®»½ ×Ò ½Á®»²¬»¼Á½­® ÔÑÑÐ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùÓ±ª·» ®»­»®ª»¼ò Û¨°»½¬»¼ ¾¿½µ ±²æ ù ¤¤ ®»²¬»¼Á®»½ò»¨°Á®»¬Á¼¿¬»÷å ÛÈ×Ì ÉØÛÒ ½Á®»²¬»¼Á½­®ûº±«²¼å

Page 456: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-67

ÛÒÜ ÔÑÑÐå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÑÌØÛÎÍ ÌØÛÒ »¨½»°¬·±²Á¸¿²¼´»®øÍÏÔÝÑÜÛô ùÎÛÍÛÎÊÛÁÓÑÊ×Ûù÷å

ÛÒÜ ®»­»®ª»Á³±ª·»å

ÐÎÑÝÛÜËÎÛ ®»¬«®²Á³±ª·»ø °Á¬·¬´»·¼ ×Ò ®»²¬¿´ò¬·¬´»Á·¼ûÌÇÐÛô °Á½±°§·¼ ×Ò ®»²¬¿´ò½±°§Á·¼ûÌÇÐÛô °Á­¬­ ×Ò ¬·¬´»Á½±°§ò­¬¿¬«­ûÌÇÐÛ÷ ×Í ªÁ¼«³³§ ÊßÎÝØßÎîøï÷å ÝËÎÍÑÎ ½Á®»­Á½­® ×Í ÍÛÔÛÝÌ ö ÚÎÑÓ ®»­»®ª¿¬·±² ÉØÛÎÛ ¬·¬´»Á·¼ ã °Á¬·¬´»·¼å ÞÛÙ×Ò ÍÛÔÛÝÌ ùù ×ÒÌÑ ªÁ¼«³³§ ÚÎÑÓ ¬·¬´» ÉØÛÎÛ ¬·¬´»Á·¼ ã °Á¬·¬´»·¼å ËÐÜßÌÛ ®»²¬¿´ ÍÛÌ ¿½¬Á®»¬Á¼¿¬» ã ÍÇÍÜßÌÛ ÉØÛÎÛ ¬·¬´»Á·¼ ã °Á¬·¬´»·¼ ßÒÜ ½±°§Á·¼ ã °Á½±°§·¼ ßÒÜ ¿½¬Á®»¬Á¼¿¬» ×Í ÒËÔÔå ËÐÜßÌÛ ¬·¬´»Á½±°§ ÍÛÌ ­¬¿¬«­ ã ËÐÐÛÎø°Á­¬­÷ ÉØÛÎÛ ¬·¬´»Á·¼ ã °Á¬·¬´»·¼ ßÒÜ ½±°§Á·¼ ã °Á½±°§·¼å ÚÑÎ ®»­Á®»½ ×Ò ½Á®»­Á½­® ÔÑÑÐ ×Ú ½Á®»­Á½­®ûÚÑËÒÜ ÌØÛÒ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøùЫ¬ ¬¸·­ ³±ª·» ±² ¸±´¼ óó ù¤¤ ù®»­»®ª»¼ ¾§ ³»³¾»® ýù ¤¤ ®»­Á®»½ò³»³¾»®Á·¼÷å ÛÒÜ ×Úå ÛÒÜ ÔÑÑÐå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÑÌØÛÎÍ ÌØÛÒ »¨½»°¬·±²Á¸¿²¼´»®øÍÏÔÝÑÜÛô ùÎÛÌËÎÒÁÓÑÊ×Ûù÷å ÛÒÜ ®»¬«®²Á³±ª·»å ÚËÒÝÌ×ÑÒ ²»©Á®»²¬¿´ø °Á³»³¾»®·¼ ×Ò ®»²¬¿´ò³»³¾»®Á·¼ûÌÇÐÛô °Á¬·¬´»·¼ ×Ò ®»²¬¿´ò¬·¬´»Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÜßÌÛ ×Í ÝËÎÍÑÎ ½Á½±°§Á½­® ×Í ÍÛÔÛÝÌ ö ÚÎÑÓ ¬·¬´»Á½±°§ ÉØÛÎÛ ¬·¬´»Á·¼ ã °Á¬·¬´»·¼ ÚÑÎ ËÐÜßÌÛå ªÁº´¿¹ ÞÑÑÔÛßÒ æã ÚßÔÍÛå ÞÛÙ×Ò ÚÑÎ ½±°§Á®»½ ×Ò ½Á½±°§Á½­® ÔÑÑÐ ×Ú ½±°§Á®»½ò­¬¿¬«­ ã ùßÊß×ÔßÞÔÛù ÌØÛÒ ËÐÜßÌÛ ¬·¬´»Á½±°§ ÍÛÌ ­¬¿¬«­ ã ùÎÛÒÌÛÜù

Page 457: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-68

ÉØÛÎÛ ÝËÎÎÛÒÌ ÑÚ ½Á½±°§Á½­®å ×ÒÍÛÎÌ ×ÒÌÑ ®»²¬¿´ø¾±±µÁ¼¿¬»ô ½±°§Á·¼ô ³»³¾»®Á·¼ô ¬·¬´»Á·¼ô »¨°Á®»¬Á¼¿¬»÷ ÊßÔËÛÍ øÍÇÍÜßÌÛô ½±°§Á®»½ò½±°§Á·¼ô °Á³»³¾»®·¼ô °Á¬·¬´»·¼ô ÍÇÍÜßÌÛ õ í÷å

ªÁº´¿¹ æã ÌÎËÛå

ÛÈ×Ìå ÛÒÜ ×Úå ÛÒÜ ÔÑÑÐå ÝÑÓÓ×Ìå ×Ú ªÁº´¿¹ ÌØÛÒ ÎÛÌËÎÒ øÍÇÍÜßÌÛ õ í÷å ÛÔÍÛ ®»­»®ª»Á³±ª·»ø°Á³»³¾»®·¼ô °Á¬·¬´»·¼÷å ÎÛÌËÎÒ ÒËÔÔå ÛÒÜ ×Úå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÑÌØÛÎÍ ÌØÛÒ »¨½»°¬·±²Á¸¿²¼´»®øÍÏÔÝÑÜÛô ùÒÛÉÁÎÛÒÌßÔù÷å ÎÛÌËÎÒ ÒËÔÔå ÛÒÜ ²»©Á®»²¬¿´å ÚËÒÝÌ×ÑÒ ²»©Á®»²¬¿´ø °Á³»³¾»®²¿³» ×Ò ³»³¾»®ò´¿­¬Á²¿³»ûÌÇÐÛô °Á¬·¬´»·¼ ×Ò ®»²¬¿´ò¬·¬´»Á·¼ûÌÇÐÛ÷ ÎÛÌËÎÒ ÜßÌÛ ×Í ÝËÎÍÑÎ ½Á½±°§Á½­® ×Í ÍÛÔÛÝÌ ö ÚÎÑÓ ¬·¬´»Á½±°§ ÉØÛÎÛ ¬·¬´»Á·¼ ã °Á¬·¬´»·¼ ÚÑÎ ËÐÜßÌÛå ªÁº´¿¹ ÞÑÑÔÛßÒ æã ÚßÔÍÛå ªÁ³»³¾»®·¼ ³»³¾»®ò³»³¾»®Á·¼ûÌÇÐÛå ÝËÎÍÑÎ ½Á³»³¾»®Á½­® ×Í ÍÛÔÛÝÌ ³»³¾»®Á·¼ô ´¿­¬Á²¿³»ô º·®­¬Á²¿³» ÚÎÑÓ ³»³¾»® ÉØÛÎÛ ÔÑÉÛÎø´¿­¬Á²¿³»÷ ã ÔÑÉÛÎø°Á³»³¾»®²¿³»÷ ÑÎÜÛÎ ÞÇ ´¿­¬Á²¿³»ô º·®­¬Á²¿³»å ÞÛÙ×Ò ÍÛÔÛÝÌ ³»³¾»®Á·¼ ×ÒÌÑ ªÁ³»³¾»®·¼ ÚÎÑÓ ³»³¾»® ÉØÛÎÛ ´±©»®ø´¿­¬Á²¿³»÷ ã ´±©»®ø°Á³»³¾»®²¿³»÷å ÚÑÎ ½±°§Á®»½ ×Ò ½Á½±°§Á½­® ÔÑÑÐ ×Ú ½±°§Á®»½ò­¬¿¬«­ ã ùßÊß×ÔßÞÔÛù ÌØÛÒ ËÐÜßÌÛ ¬·¬´»Á½±°§ ÍÛÌ ­¬¿¬«­ ã ùÎÛÒÌÛÜù ÉØÛÎÛ ÝËÎÎÛÒÌ ÑÚ ½Á½±°§Á½­®å ×ÒÍÛÎÌ ×ÒÌÑ ®»²¬¿´ ø¾±±µÁ¼¿¬»ô ½±°§Á·¼ô ³»³¾»®Á·¼ô ¬·¬´»Á·¼ô »¨°Á®»¬Á¼¿¬»÷ ÊßÔËÛÍ øÍÇÍÜßÌÛô ½±°§Á®»½ò½±°§Á·¼ô ªÁ³»³¾»®·¼ô °Á¬·¬´»·¼ô ÍÇÍÜßÌÛ õ í÷å ªÁº´¿¹ æã ÌÎËÛå

Page 458: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-69

ÛÈ×Ìå ÛÒÜ ×Úå ÛÒÜ ÔÑÑÐå ÝÑÓÓ×Ìå ×Ú ªÁº´¿¹ ÌØÛÒ ÎÛÌËÎÒøÍÇÍÜßÌÛ õ í÷å

ÛÔÍÛ

®»­»®ª»Á³±ª·»øªÁ³»³¾»®·¼ô °Á¬·¬´»·¼÷å ÎÛÌËÎÒ ÒËÔÔå ÛÒÜ ×Úå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÌÑÑÁÓßÒÇÁÎÑÉÍ ÌØÛÒ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø ùÉ¿®²·²¹ÿ Ó±®» ¬¸¿² ±²» ³»³¾»® ¾§ ¬¸·­ ²¿³»òù÷å ÚÑÎ ³»³¾»®Á®»½ ×Ò ½Á³»³¾»®Á½­® ÔÑÑÐ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛø³»³¾»®Á®»½ò³»³¾»®Á·¼ ¤¤ ÝØÎøç÷ ¤¤ ³»³¾»®Á®»½ò´¿­¬Á²¿³» ¤¤ ùô ù ¤¤ ³»³¾»®Á®»½òº·®­¬Á²¿³»÷å ÛÒÜ ÔÑÑÐå ÎÛÌËÎÒ ÒËÔÔå ÉØÛÒ ÑÌØÛÎÍ ÌØÛÒ »¨½»°¬·±²Á¸¿²¼´»®øÍÏÔÝÑÜÛô ùÒÛÉÁÎÛÒÌßÔù÷å ÎÛÌËÎÒ ÒËÔÔå ÛÒÜ ²»©Á®»²¬¿´å ÐÎÑÝÛÜËÎÛ ²»©Á³»³¾»®ø °Á´²¿³» ×Ò ³»³¾»®ò´¿­¬Á²¿³»ûÌÇÐÛô °Áº²¿³» ×Ò ³»³¾»®òº·®­¬Á²¿³»ûÌÇÐÛ ÜÛÚßËÔÌ ÒËÔÔô °Á¿¼¼®»­­ ×Ò ³»³¾»®ò¿¼¼®»­­ûÌÇÐÛ ÜÛÚßËÔÌ ÒËÔÔô °Á½·¬§ ×Ò ³»³¾»®ò½·¬§ûÌÇÐÛ ÜÛÚßËÔÌ ÒËÔÔô °Á°¸±²» ×Ò ³»³¾»®ò°¸±²»ûÌÇÐÛ ÜÛÚßËÔÌ ÒËÔÔ÷ ×Í ÞÛÙ×Ò ×ÒÍÛÎÌ ×ÒÌÑ ³»³¾»®ø³»³¾»®Á·¼ô ´¿­¬Á²¿³»ô º·®­¬Á²¿³»ô ¿¼¼®»­­ô ½·¬§ô °¸±²»ô ¶±·²Á¼¿¬»÷ ÊßÔËÛÍø³»³¾»®Á·¼Á­»¯òÒÛÈÌÊßÔô °Á´²¿³»ô °Áº²¿³»ô °Á¿¼¼®»­­ô °Á½·¬§ô °Á°¸±²»ô ÍÇÍÜßÌÛ÷å ÝÑÓÓ×Ìå ÛÈÝÛÐÌ×ÑÒ ÉØÛÒ ÑÌØÛÎÍ ÌØÛÒ »¨½»°¬·±²Á¸¿²¼´»®øÍÏÔÝÑÜÛô ùÒÛÉÁÓÛÓÞÛÎù÷å ÛÒÜ ²»©Á³»³¾»®å ÛÒÜ ª·¼»±Á°µ¹å ñ ÍØÑÉ ÛÎÎÑÎÍ

Page 459: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-70

4) Use the following scripts located in the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«/­±´² directory to test your routines. Make sure you enable ÍÛÎÊÛÎÑËÌÐËÌ:

a) Add two members using ­±´Á¿°ÁðîÁðïÁðìÁ¿ò­¯´.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðîÁðïÁðìÁ¿ò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ ÛÈÛÝËÌÛ ª·¼»±Á°µ¹ò²»©Á³»³¾»®øùØ¿¿­ùô ùÖ¿³»­ùô ùݸ»­¬²«¬ ͬ®»»¬ùô ùÞ±­¬±²ùô ùêïéóïîíóìëêéù÷ ÛÈÛÝËÌÛ ª·¼»±Á°µ¹ò²»©Á³»³¾»®øùÞ·®·ùô ùß´´¿²ùô ùØ·¿©¿¬¸¿ Ü®·ª»ùô ùÒ»© DZ®µùô ùëïêóïîíóìëêéù÷

Page 460: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-71

b) Add new video rentals using ­±´Á¿°ÁðîÁðïÁðìÁ¾ò­¯´.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðîÁðïÁðìÁ¾ò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

ÛÈÛÝ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøª·¼»±Á°µ¹ò²»©Á®»²¬¿´øïïðô çè÷÷ ÛÈÛÝ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøª·¼»±Á°µ¹ò²»©Á®»²¬¿´øïðçô çí÷÷ ÛÈÛÝ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøª·¼»±Á°µ¹ò²»©Á®»²¬¿´øïðéô çè÷÷

ÛÈÛÝ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøª·¼»±Á°µ¹ò²»©Á®»²¬¿´øùÞ·®·ùô çé÷÷

ÛÈÛÝ ÜÞÓÍÁÑËÌÐËÌòÐËÌÁÔ×ÒÛøª·¼»±Á°µ¹ò²»©Á®»²¬¿´øçéô çé÷÷

c) Return movies using the ­±´Á¿°ÁðîÁðïÁðìÁ½ò­¯´ script.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðîÁðïÁðìÁ½ò­¯´ script. The code, the connection prompt, and the results are displayed as follows:

Page 461: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-72

ÍÛÌ ÍÛÎÊÛÎÑËÌÐËÌ ÑÒ

ÛÈÛÝËÌÛ ª·¼»±Á°µ¹ò®»¬«®²Á³±ª·»øçèô ïô ùßÊß×ÔßÞÔÛù÷ ÛÈÛÝËÌÛ ª·¼»±Á°µ¹ò®»¬«®²Á³±ª·»øçëô íô ùßÊß×ÔßÞÔÛù÷ ÛÈÛÝËÌÛ ª·¼»±Á°µ¹ò®»¬«®²Á³±ª·»øïïïô ïô ùÎÛÒÌÛÜù÷

5) The business hours for the video store are 8:00 AM through 10:00 PM, Sunday through Friday, and 8:00 AM through 12:00 PM on Saturday. To ensure that the tables can be modified only during these hours, create a stored procedure that is called by triggers on the tables.

a) Create a stored procedure called Ì×ÓÛÁÝØÛÝÕ that checks the current time against business hours. If the current time is not within business hours, use the Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎ procedure to give an appropriate message.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðîÁðïÁðëÁ¿ò­¯´ script. The code, the connection prompt, and the results are displayed as follows: ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÐÎÑÝÛÜËÎÛ ¬·³»Á½¸»½µ ×Í ÞÛÙ×Ò ×Ú øøÌÑÁÝØßÎøÍÇÍÜßÌÛôùÜù÷ ÞÛÌÉÛÛÒ ï ßÒÜ ê÷ ßÒÜ øÌÑÁÜßÌÛøÌÑÁÝØßÎøÍÇÍÜßÌÛô ù¸¸îìæ³·ù÷ô ù¸¸îìæ³·ù÷ ÒÑÌ ÞÛÌÉÛÛÒ

Page 462: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-73

ÌÑÁÜßÌÛøùðèæððùô ù¸¸îìæ³·ù÷ ßÒÜ ÌÑÁÜßÌÛøùîîæððùô ù¸¸îìæ³·ù÷÷÷ ÑÎ øøÌÑÁÝØßÎøÍÇÍÜßÌÛô ùÜù÷ ã é÷ ßÒÜ øÌÑÁÜßÌÛøÌÑÁÝØßÎøÍÇÍÜßÌÛô ù¸¸îìæ³·ù÷ô ù¸¸îìæ³·ù÷ ÒÑÌ ÞÛÌÉÛÛÒ ÌÑÁÜßÌÛøùðèæððùô ù¸¸îìæ³·ù÷ ßÒÜ ÌÑÁÜßÌÛøùîìæððùô ù¸¸îìæ³·ù÷÷÷ ÌØÛÒ Îß×ÍÛÁßÐÐÔ×ÝßÌ×ÑÒÁÛÎÎÑÎøóîðçççô ùÜ¿¬¿ ½¸¿²¹»­ ®»­¬®·½¬»¼ ¬± ±ºº·½» ¸±«®­òù÷å ÛÒÜ ×Úå

ÛÒÜ ¬·³»Á½¸»½µå

ñ ÍØÑÉ ÛÎÎÑÎÍ

ÐÎÑÝÛÜËÎÛ ¬·³»Á½¸»½µ ݱ³°·´»¼ò Ò± Û®®±®­ò

b) Create a trigger on each of the five tables. Fire the trigger before data is inserted, updated, and deleted from the tables. Call your Ì×ÓÛÁÝØÛÝÕ procedure from each of these triggers.

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðîÁðïÁðëÁ¾ò­¯´ script. The code and the result are displayed as follows:

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÌÎ×ÙÙÛÎ ³»³¾»®Á¬®·¹ ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ ÑÎ ÜÛÔÛÌÛ ÑÒ ³»³¾»® ÝßÔÔ ¬·³»Á½¸»½µ ñ

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÌÎ×ÙÙÛÎ ®»²¬¿´Á¬®·¹ ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ ÑÎ ÜÛÔÛÌÛ ÑÒ ®»²¬¿´ ÝßÔÔ ¬·³»Á½¸»½µ

Page 463: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-74

ñ

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÌÎ×ÙÙÛÎ ¬·¬´»Á½±°§Á¬®·¹ ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ ÑÎ ÜÛÔÛÌÛ ÑÒ ¬·¬´»Á½±°§ ÝßÔÔ ¬·³»Á½¸»½µ ñ

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÌÎ×ÙÙÛÎ ¬·¬´»Á¬®·¹ ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ ÑÎ ÜÛÔÛÌÛ ÑÒ ¬·¬´» ÝßÔÔ ¬·³»Á½¸»½µ ñ

ÝÎÛßÌÛ ÑÎ ÎÛÐÔßÝÛ ÌÎ×ÙÙÛÎ ®»­»®ª¿¬·±²Á¬®·¹

ÞÛÚÑÎÛ ×ÒÍÛÎÌ ÑÎ ËÐÜßÌÛ ÑÎ ÜÛÔÛÌÛ ÑÒ ®»­»®ª¿¬·±² ÝßÔÔ ¬·³»Á½¸»½µ ñ

ÌÎ×ÙÙÛÎ ³»³¾»®Á¬®·¹ ݱ³°·´»¼ò ÌÎ×ÙÙÛÎ ®»²¬¿´Á¬®·¹ ݱ³°·´»¼ò ÌÎ×ÙÙÛÎ ¬·¬´»Á½±°§Á¬®·¹ ݱ³°·´»¼ò ÌÎ×ÙÙÛÎ ¬·¬´»Á¬®·¹ ݱ³°·´»¼ò ÌÎ×ÙÙÛÎ ®»­»®ª¿¬·±²Á¬®·¹ ݱ³°·´»¼ò

c) Test your triggers. Note: In order for your trigger to fail, you may need to change the time to be outside the range of your current time in class. For example, while testing, you may want valid video hours in your trigger to be from 6:00 PM through 8:00 AM.

Page 464: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2

Practice Solutions 2-1: Creating the VIDEO_PKG Package (continued)

Oracle Database 11g: Develop PL/SQL Program Units AP-75

Run the ñ¸±³»ñ±®¿½´»ñ´¿¾­ñ°´°«ñ­±´²­ñ­±´Á¿°ÁðîÁðïÁðëÁÝò­¯´ script. The code and the result are displayed as follows:

óó Ú·®­¬ ¼»¬»®³·²» ½«®®»²¬ ¬·³»¦±²» ¿²¼ ¬·³» ÍÛÔÛÝÌ ÍÛÍÍ×ÑÒÌ×ÓÛÆÑÒÛô ÌÑÁÝØßÎøÝËÎÎÛÒÌÁÜßÌÛô ùÜÜóÓÑÒóÇÇÇÇ ØØîìæÓ×ù÷ ÝËÎÎÁÜßÌÛ ÚÎÑÓ ÜËßÔå

óó ݸ¿²¹» §±«® ¬·³» ¦±²» «­·²¹» Åõ¤óÃØØæÓ× º±®³¿¬ ­«½¸ ¬¸¿¬ óó ¬¸» ½«®®»²¬ ¬·³» ®»¬«®²­ ¿ ¬·³» ¾»¬©»»² ê°³ ¿²¼ 迳

ßÔÌÛÎ ÍÛÍÍ×ÑÒ ÍÛÌ Ì×ÓÛÁÆÑÒÛãùóðéæððùå

óó ß¼¼ ¿ ²»© ³»³¾»® øº±® ¿ ­¿³°´» ¬»­¬÷

ÛÈÛÝËÌÛ ª·¼»±Á°µ¹ò²»©Á³»³¾»®øùÛ´·¿­ùô ùÛ´´·¿²»ùô ùÊ·²» ͬ®»»¬ùô ùÝ¿´·º±®²·¿ùô ùéèçóïîíóìëêéù÷

ÞÛÙ×Ò ª·¼»±Á°µ¹ò²»©Á³»³¾»®øùÛ´·¿­ùô ùÛ´´·¿²»ùô ùÊ·²» ͬ®»»¬ùô ùÝ¿´·º±®²·¿ùô ùéèçóïîíóìëêéù÷å ÛÒÜå

óó λ­¬±®» ¬¸» ±®·¹·²¿´ ¬·³» ¦±²» º±® §±«® ­»­­·±²ò ßÔÌÛÎ ÍÛÍÍ×ÑÒ ÍÛÌ Ì×ÓÛÁÆÑÒÛãùóððæððùå

Page 465: 6- Oracle Database 11g Develop PLSQL Program Units Vol 2