4 Copyright © 2009, Oracle. All rights reserved. Working with Packages.

Slides:



Advertisements
Similar presentations
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Advertisements

L/O/G/O Creating Packages. Objectives After completing this lesson, you should be able to do the following: –Describe packages and list their components.
Persistent State of Package Variables. 2 home back first prev next last What Will I Learn? Identify persistent states of package variables Control the.
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
3 Copyright © 2004, Oracle. All rights reserved. Creating Packages.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
Creating Packages. 2 home back first prev next last What Will I Learn? Describe the reasons for using a package Describe the two components of a package:
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
9 Copyright © 2009, Oracle. All rights reserved. Introducing Stored Procedures and Functions.
Copyright © SUPINFO. All rights reserved Procedures and functions.
6 Copyright © 2009, Oracle. All rights reserved. Working with Composite Data Types.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
9 Copyright © 2007, Oracle. All rights reserved. Managing Data and Concurrency.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
L/O/G/O Working with Composite Data Types. Objectives After completing this lesson, you should be able to do the following: –Create user-defined PL/SQL.
20 Copyright © Oracle Corporation, All rights reserved. Oracle9 i Extensions to DML and DDL Statements.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
PRACTICE OVERVIEW PL/SQL Part Examine this package specification and body: Which statement about the V_TOTAL_BUDGET variable is true? A. It must.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
8 Copyright © 2005, Oracle. All rights reserved. Managing Data.
7 Copyright © 2005, Oracle. All rights reserved. Managing Undo Data.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
ITEC 224 Database Programming PL/SQL Lab Cursors.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.
Using Functions in SQL Statements. 2 home back first prev next last What Will I Learn? List the advantages of user-defined functions in SQL statements.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Creating Procedures. PL/SQL Program Construct Tools Constructs Anonymous Block Application procedures or functions Application packages Application Triggers.
10 Copyright © 2004, Oracle..All rights reserved. PL/SQL.
I Copyright © 2007, Oracle. All rights reserved. Introduction.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
6 Copyright © 2004, Oracle. All rights reserved. Working with Composite Data Types.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
STORED PROCEDURE & STORED FUNCTION Politeknik Telkom 2012.
4 Copyright © 2004, Oracle. All rights reserved. Advanced Interface Methods.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Creating Stored Functions
Creating Stored Procedures and Functions
Creating Packages.
Database Management Systems 2
Database Management Systems 2
Using the Set Operators
Working with Composite Datatypes
PRACTICE OVERVIEW PL/SQL Part - 2.
Database Management Systems 2
Cursors.
Creating Noninput Items
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Prof. Arfaoui. COM390 Chapter 6
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

4 Copyright © 2009, Oracle. All rights reserved. Working with Packages

Copyright © 2009, Oracle. All rights reserved Objectives After completing this lesson, you should be able to do the following: Overload package procedures and functions Use forward declarations Create an initialization block in a package body Manage persistent package data states for the life of a session Use associative arrays (index-by tables) and records in packages

Copyright © 2009, Oracle. All rights reserved Lesson Agenda Overloading package subprograms, using forward declarations, and creating an initialization block in a package body Managing persistent package data states for the life of a session and using associative arrays (index-by tables) and records in packages

Copyright © 2009, Oracle. All rights reserved Overloading Subprograms in PL/SQL Enables you to create two or more subprograms with the same name Requires that the subprogram’s formal parameters differ in number, order, or data type family Enables you to build flexible ways for invoking subprograms with different data Provides a way to extend functionality without loss of existing code; that is, adding new parameters to existing subprograms Provides a way to overload local subprograms, package subprograms, and type methods, but not stand-alone subprograms

Copyright © 2009, Oracle. All rights reserved

Copyright © 2009, Oracle. All rights reserved CREATE OR REPLACE PACKAGE dept_pkg IS PROCEDURE add_department (p_deptno departments.department_id%TYPE, p_name departments.department_name%TYPE :='unknown', p_loc departments.location_id%TYPE := 1700); PROCEDURE add_department (p_name departments.department_name%TYPE :='unknown', p_loc departments.location_id%TYPE := 1700); END dept_pkg; / Overloading Procedures Example: Creating the Package Specification

Copyright © 2009, Oracle. All rights reserved Package body of package defined on previous slide. CREATE OR REPLACE PACKAGE BODY dept_pkg IS PROCEDURE add_department –- First procedure’s declaration (p_deptno departments.department_id%TYPE, p_name departments.department_name%TYPE := 'unknown', p_loc departments.location_id%TYPE := 1700) IS BEGIN INSERT INTO departments(department_id, department_name, location_id) VALUES (p_deptno, p_name, p_loc); END add_department; PROCEDURE add_department –- Second procedure’s declaration (p_name departments.department_name%TYPE := 'unknown', p_loc departments.location_id%TYPE := 1700) IS BEGIN INSERT INTO departments (department_id, department_name, location_id) VALUES (departments_seq.NEXTVAL, p_name, p_loc); END add_department; END dept_pkg; / Overloading Procedures Example: Creating the Package Body

Copyright © 2009, Oracle. All rights reserved Overloading and the STANDARD Package A package named STANDARD defines the PL/SQL environment and built-in functions. Most built-in functions are overloaded. An example is the TO_CHAR function: A PL/SQL subprogram with the same name as a built-in subprogram overrides the standard declaration in the local context, unless qualified by the package name. FUNCTION TO_CHAR (p1 DATE) RETURN VARCHAR2; FUNCTION TO_CHAR (p2 NUMBER) RETURN VARCHAR2; FUNCTION TO_CHAR (p1 DATE, P2 VARCHAR2) RETURN VARCHAR2; FUNCTION TO_CHAR (p1 NUMBER, P2 VARCHAR2) RETURN VARCHAR2;...

Copyright © 2009, Oracle. All rights reserved Illegal Procedure Reference Block-structured languages such as PL/SQL must declare identifiers before referencing them. Example of a referencing problem: CREATE OR REPLACE PACKAGE BODY forward_pkg IS PROCEDURE award_bonus(...) IS BEGIN calc_rating (...); --illegal reference END; PROCEDURE calc_rating (...) IS BEGIN... END; END forward_pkg; /

Copyright © 2009, Oracle. All rights reserved CREATE OR REPLACE PACKAGE BODY forward_pkg IS PROCEDURE calc_rating (...);-- forward declaration -- Subprograms defined in alphabetical order PROCEDURE award_bonus(...) IS BEGIN calc_rating (...); -- reference resolved!... END; PROCEDURE calc_rating (...) IS -- implementation BEGIN... END; END forward_pkg; Using Forward Declarations to Solve Illegal Procedure Reference In the package body, a forward declaration is a private subprogram specification terminated by a semicolon.

Copyright © 2009, Oracle. All rights reserved CREATE OR REPLACE PACKAGE taxes IS v_tax NUMBER; declare all public procedures/functions END taxes; / CREATE OR REPLACE PACKAGE BODY taxes IS declare all private variables define public/private procedures/functions BEGIN SELECT rate_value INTO v_tax FROM tax_rates WHERE rate_name = 'TAX'; END taxes; / Initializing Packages The block at the end of the package body executes once and is used to initialize public and private package variables.

Copyright © 2009, Oracle. All rights reserved Using Package Functions in SQL You use package functions in SQL statements. To execute a SQL statement that calls a member function, the Oracle database must know the function’s purity level. Purity level is the extent to which the function is free of side effects, which refers to accessing database tables, package variables, and so on, for reading or writing. It is important to control side effects because they can: –Prevent the proper parallelization of a query –Produce order-dependent and, therefore, indeterminate results –Require impermissible actions such as the maintenance of package state across user sessions

Copyright © 2009, Oracle. All rights reserved Controlling Side Effects of PL/SQL Subprograms To be callable from SQL statements, a stored function must obey the following purity rules to control side effects: When called from a SELECT or a parallelized DML statement, the function cannot modify any database tables. When called from a DML statement, the function cannot query or modify any database tables modified by that statement. When called from a SELECT or DML statement, the function cannot execute SQL transaction control, session control, or system control statements.

Copyright © 2009, Oracle. All rights reserved SELECT taxes_pkg.tax(salary), salary, last_name FROM employees; Package Function in SQL: Example CREATE OR REPLACE PACKAGE taxes_pkg IS FUNCTION tax (p_value IN NUMBER) RETURN NUMBER; END taxes_pkg; / CREATE OR REPLACE PACKAGE BODY taxes_pkg IS FUNCTION tax (p_value IN NUMBER) RETURN NUMBER IS v_rate NUMBER := 0.08; BEGIN RETURN (p_value * v_rate); END tax; END taxes_pkg; /

Copyright © 2009, Oracle. All rights reserved Lesson Agenda Overloading package subprograms, using forward declarations, and creating an initialization block in a package body Managing persistent package data states for the life of a session and using associative arrays (index-by tables) and records in packages

Copyright © 2009, Oracle. All rights reserved Persistent State of Packages The collection of package variables and the values define the package state. The package state is: Initialized when the package is first loaded Persistent (by default) for the life of the session: –Stored in the User Global Area (UGA) –Unique to each session –Subject to change when package subprograms are called or public variables are modified Not persistent for the session but persistent for the life of a subprogram call when using PRAGMA SERIALLY_REUSABLE in the package specification

Copyright © 2009, Oracle. All rights reserved

Copyright © 2009, Oracle. All rights reserved Persistent State of Package Variables: Example TimeEvents v_std_ comm [variable] MAX (commissi on_pct) [column] v_std_ comm [variable] MAX (commissi on_pct) [Column] 9:00 Scott> EXECUTE comm_pkg.reset_comm(0.25) :30 Jones> INSERT INTO employees(last_name, commission_pct) VALUES('Madonna', 0.8); :35 Jones> EXECUTE comm_pkg.reset_comm (0.5) :00 Scott> EXECUTE comm_pkg.reset_comm(0.6) Err –20210 'Bad Commission' :00 11:01 12:00 Jones> ROLLBACK; EXIT... EXEC comm_pkg.reset_comm(0.2) State for Scott State for Jones

Copyright © 2009, Oracle. All rights reserved Persistent State of a Package Cursor: Example CREATE OR REPLACE PACKAGE curs_pkg IS –- Package spec PROCEDURE open; FUNCTION next(p_n NUMBER := 1) RETURN BOOLEAN; PROCEDURE close; END curs_pkg; CREATE OR REPLACE PACKAGE BODY curs_pkg IS –- Package body CURSOR cur_c IS SELECT employee_id FROM employees; PROCEDURE open IS BEGIN IF NOT cur_c%ISOPEN THEN OPEN cur_c; END IF; END open; code continued on next slide

Copyright © 2009, Oracle. All rights reserved Persistent State of a Package Cursor: Example... FUNCTION next(p_n NUMBER := 1) RETURN BOOLEAN IS v_emp_id employees.employee_id%TYPE; BEGIN FOR count IN 1.. p_n LOOP FETCH cur_c INTO v_emp_id; EXIT WHEN cur_c%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Id: ' ||(v_emp_id)); END LOOP; RETURN cur_c%FOUND; END next; PROCEDURE close IS BEGIN IF cur_c%ISOPEN THEN CLOSE cur_c; END IF; END close; END curs_pkg;

Copyright © 2009, Oracle. All rights reserved Executing the CURS_PKG Package

Copyright © 2009, Oracle. All rights reserved Using Associative Arrays in Packages CREATE OR REPLACE PACKAGE emp_pkg IS TYPE emp_table_type IS TABLE OF employees%ROWTYPE INDEX BY BINARY_INTEGER; PROCEDURE get_employees(p_emps OUT emp_table_type); END emp_pkg; CREATE OR REPLACE PACKAGE BODY emp_pkg IS PROCEDURE get_employees(p_emps OUT emp_table_type) IS v_i BINARY_INTEGER := 0; BEGIN FOR emp_record IN (SELECT * FROM employees) LOOP emps(v_i) := emp_record; v_i:= v_i + 1; END LOOP; END get_employees; END emp_pkg;

Copyright © 2009, Oracle. All rights reserved Quiz Overloading subprograms in PL/SQL: 1.Enables you to create two or more subprograms with the same name 2.Requires that the subprogram’s formal parameters differ in number, order, or data type family 3.Enables you to build flexible ways for invoking subprograms with different data 4.Provides a way to extend functionality without loss of existing code; that is, adding new parameters to existing subprograms

Copyright © 2009, Oracle. All rights reserved Summary In this lesson, you should have learned how to: Overload package procedures and functions Use forward declarations Create an initialization block in a package body Manage persistent package data states for the life of a session Use associative arrays (index-by tables) and records in packages

Copyright © 2009, Oracle. All rights reserved Practice 4: Overview This practice covers the following topics: Using overloaded subprograms Creating a package initialization block Using a forward declaration

Copyright © 2009, Oracle. All rights reserved