using procedures & functions oracle database pl/sql 10g programming chapter 9

23
Using Procedures & Using Procedures & Functions Functions Oracle Database PL/SQL 10g Oracle Database PL/SQL 10g Programming Programming Chapter 9 Chapter 9

Upload: shon-waters

Post on 13-Jan-2016

259 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

Using Procedures & Using Procedures & FunctionsFunctions

Oracle Database PL/SQL 10g Oracle Database PL/SQL 10g ProgrammingProgrammingChapter 9Chapter 9

Page 2: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 22

Using Procedures & Using Procedures & FunctionsFunctions

Data DictionaryData Dictionary SubprogramsSubprograms Definer RightsDefiner Rights Invoker RightsInvoker Rights Purity LevelsPurity Levels Multiple-Valued FunctionsMultiple-Valued Functions Shared PoolShared Pool

Page 3: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 33

Using Procedures & FunctionsUsing Procedures & Functions

Data Dictionary: LocationsData Dictionary: Locations Stored Program Catalog Views:Stored Program Catalog Views:

Definition views:Definition views: Stored Program Source –Stored Program Source –USER_SOURCEUSER_SOURCE

Stored Program Information –Stored Program Information –USER_PROCEDURESUSER_PROCEDURES

Stored Program Header –Stored Program Header –USER_ARGUMENTSUSER_ARGUMENTS

Stored Program Settings – Stored Program Settings – USER_PLSQL_OBJECT_SETTINGSUSER_PLSQL_OBJECT_SETTINGS

Dependency view:Dependency view:USER_DEPENDENCIESUSER_DEPENDENCIES

Page 4: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 44

Using Procedures & FunctionsUsing Procedures & Functions

Data Dictionary: LocationsData Dictionary: Locations Object Catalog Views:Object Catalog Views:

Object definitions:Object definitions:USER_SOURCEUSER_SOURCE

Table definitions:Table definitions: Table Structure –Table Structure –USER_TABLESUSER_TABLES

Table Columns –Table Columns –USER_TAB_COLSUSER_TAB_COLS

Trigger definitions:Trigger definitions: Trigger Structure –Trigger Structure –USER_TRIGGERSUSER_TRIGGERS

Trigger Columns –Trigger Columns –USER_TRIGGER_COLSUSER_TRIGGER_COLS

Page 5: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 55

Using Procedures & FunctionsUsing Procedures & Functions

Data Dictionary: Query Data Dictionary: Query StatusStatus

SQL> SELECT object_nameSQL> SELECT object_name

2 , object_type2 , object_type

3 , status3 , status

4 FROM user_objects;4 FROM user_objects;

Page 6: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 66

Using Procedures & FunctionsUsing Procedures & Functions

Data Dictionary: Query Data Dictionary: Query TablesTables

SQL> SELECT table_nameSQL> SELECT table_name

2 , column_id2 , column_id

3 , column_name3 , column_name

4 , data_type4 , data_type

5 FROM user_tab_cols5 FROM user_tab_cols

6 ORDER BY table_name6 ORDER BY table_name

7 , column_id;7 , column_id;

Page 7: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 77

Using Procedures & FunctionsUsing Procedures & Functions

Data Dictionary: Query Data Dictionary: Query ProgramsPrograms

SQL> SELECT lineSQL> SELECT line

2 , text2 , text

3 FROM user_source3 FROM user_source

4 WHERE name = UPPER('program_name‘);4 WHERE name = UPPER('program_name‘);

Page 8: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 88

Using Procedures & FunctionsUsing Procedures & FunctionsPackage Specification Subprograms: Package Specification Subprograms:

RulesRules Stored Package Specification Programs:Stored Package Specification Programs:

Are stored in compiled p-code, like Java byte Are stored in compiled p-code, like Java byte code.code.

Can be called by any other block.Can be called by any other block. Are modularized into smaller program units.Are modularized into smaller program units. Compiled p-code can be pinned in the SGA Compiled p-code can be pinned in the SGA

for faster execution.for faster execution. Can be overloaded.Can be overloaded.

Page 9: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 99

Using Procedures & FunctionsUsing Procedures & Functions

Package Subprograms: Package Subprograms: StoredStored

Package stored subprograms:Package stored subprograms: Are defined as procedures in the package specification.Are defined as procedures in the package specification. Are defined as functions in the package specification.Are defined as functions in the package specification.

Can use schema defined types from the package specification.Can use schema defined types from the package specification. Can use schema defined SQL types.Can use schema defined SQL types. Can use locally defined types from package body ONLY in their Can use locally defined types from package body ONLY in their

internal implementation:internal implementation: Locally defined types can declared in local subprogram as Locally defined types can declared in local subprogram as

variables.variables. Locally defined types cannot be used as parameter data types.Locally defined types cannot be used as parameter data types. Locally defined types cannot be used as function return data Locally defined types cannot be used as function return data

types.types. Can be called from any schema PL/SQL program.Can be called from any schema PL/SQL program. Stored functions Stored functions CANNOTCANNOT be called from SQL when they return be called from SQL when they return

a PL/SQL data type.a PL/SQL data type.

Page 10: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 1010

Using Procedures & FunctionsUsing Procedures & Functions

Package Subprograms: Package Subprograms: StoredStored

Package stored subprogram referencing:Package stored subprogram referencing: Do not require forward referencing:Do not require forward referencing:

The specification publishes their definitions.The specification publishes their definitions. The body relies on the specification to avoid forward The body relies on the specification to avoid forward

referencing issues.referencing issues. Package stored subprogram overloading:Package stored subprogram overloading:

Signature rules:Signature rules: The list of formal parameters must differ by either:The list of formal parameters must differ by either:

The position and type of formal parameters.The position and type of formal parameters. The number of formal parameters.The number of formal parameters.

The function signature may change based on the return The function signature may change based on the return data type.data type.

Page 11: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 1111

Using Procedures & FunctionsUsing Procedures & FunctionsPackage Body (Local) Subprograms: Package Body (Local) Subprograms:

RulesRules Local Package Body Programs:Local Package Body Programs:

Are stored in compiled p-code, like Java Are stored in compiled p-code, like Java byte code.byte code.

Can be called ONLY by package blocks.Can be called ONLY by package blocks. Tightly coupled design.Tightly coupled design. Compiled p-code cannot be pinned in Compiled p-code cannot be pinned in

the SGA.the SGA. Can be overloaded only within the same Can be overloaded only within the same

block.block.

Page 12: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 1212

Using Procedures & FunctionsUsing Procedures & Functions

Package Subprograms: Package Subprograms: LocalLocal

Package local subprograms:Package local subprograms: Are defined as procedures in the package body.Are defined as procedures in the package body. Are defined as functions in the package body.Are defined as functions in the package body.

Can use locally defined types from the package body.Can use locally defined types from the package body. Can use schema defined types from package specifications.Can use schema defined types from package specifications. Can use schema defined SQL types.Can use schema defined SQL types.

Can ONLY be called from a package body program unit.Can ONLY be called from a package body program unit. Package local subprogram referencing:Package local subprogram referencing:

Require forward referencing, like anonymous block Require forward referencing, like anonymous block subprograms.subprograms.

Package local subprogram overloading:Package local subprogram overloading: Follows the same rules as stored subprograms.Follows the same rules as stored subprograms.

Page 13: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 1313

Using Procedures & FunctionsUsing Procedures & FunctionsPackage Subprograms: Package Subprograms:

DependenciesDependencies Timestamp Model (default):Timestamp Model (default):

Checks to make sure that dependents have an Checks to make sure that dependents have an early compilation timestamp.early compilation timestamp.

Cannot resolve distributed timestamps in Cannot resolve distributed timestamps in different timezones.different timezones.

Signature Model:Signature Model: Checks to make sure that the signature for Checks to make sure that the signature for

functions and procedures do not change functions and procedures do not change between compilations.between compilations.

Works in distributed configurations without Works in distributed configurations without impacts of region timezones.impacts of region timezones.

Page 14: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 1414

Using Procedures & FunctionsUsing Procedures & FunctionsPackage Subprograms: Serially Package Subprograms: Serially

ReusableReusable Non-Serially Reusable (default):Non-Serially Reusable (default):

Runtime state is kept in process memory Runtime state is kept in process memory between calls during a session.between calls during a session.

Requires memory in the SGA for all logged-on Requires memory in the SGA for all logged-on users.users.

Serially Reusable:Serially Reusable: Runtime state is refreshed after each database Runtime state is refreshed after each database

call.call. Requires memory in the SGA for users Requires memory in the SGA for users

concurrently calling the same program.concurrently calling the same program.

Page 15: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 1515

Using Procedures & FunctionsUsing Procedures & Functions

Definer Rights: DescriptionDefiner Rights: Description Definer rightsDefiner rights

Is the default when creating stored programs.Is the default when creating stored programs. Means that the stored program executes with Means that the stored program executes with

the same privileges as the defining user.the same privileges as the defining user. Can mean that calling the stored programs lets Can mean that calling the stored programs lets

it run against any schema level data.it run against any schema level data. TypicallyTypically means that users only access a slice means that users only access a slice

of data in any schema, like a private virtual of data in any schema, like a private virtual database.database.

Page 16: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 1616

Using Procedures & FunctionsUsing Procedures & Functions

Definer Rights: DepictionDefiner Rights: Depiction

User #1

User #2

User #3

User #4

Page 17: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 1717

Using Procedures & FunctionsUsing Procedures & Functions

Invoker Rights: DescriptionInvoker Rights: Description Invoker rightsInvoker rights

Is the override when creating stored programs.Is the override when creating stored programs. Means that the stored program executes with Means that the stored program executes with

the local privileges, which generally differ from the local privileges, which generally differ from the definer’s privileges.the definer’s privileges.

TypicallyTypically means that users only access their means that users only access their own schema data, like a distributed or local own schema data, like a distributed or local database.database.

Page 18: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 1818

Using Procedures & FunctionsUsing Procedures & Functions

Invoker Rights: DepictionInvoker Rights: Depiction

User #1

User #2

User #3

User #4

Page 19: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 1919

Using Procedures & FunctionsUsing Procedures & Functions

Purity Level: DefinedPurity Level: Defined Purity levels restrict the behavior of stored Purity levels restrict the behavior of stored

programs.programs. Purity level guarantees are optional beginning Purity level guarantees are optional beginning

with Oracle 8i.with Oracle 8i. Purity levels are:Purity levels are:

Writes no database states (WNDS), which disallows DML Writes no database states (WNDS), which disallows DML statements, like statements, like INSERTINSERT, , UPDATEUPDATE and and DELETEDELETE..

Reads no database states (RNDS), which disallows DQL Reads no database states (RNDS), which disallows DQL statements, like statements, like SELECTSELECT..

Writes no package states (WNPS), which disallows Writes no package states (WNPS), which disallows changes to package variables by assignment operations changes to package variables by assignment operations (including the (including the FETCHFETCH cursorcursor INTOINTO variablevariable syntax). syntax).

Reads no package states (RNPS), which disallows Reads no package states (RNPS), which disallows assigning any package variable values.assigning any package variable values.

Page 20: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 2020

Using Procedures & FunctionsUsing Procedures & Functions

Multiple-Valued Functions: Multiple-Valued Functions: DefinedDefined

A multiple-valued function exists A multiple-valued function exists when the return type of a stored or when the return type of a stored or local function returns a system local function returns a system reference cursor, or a user-defined reference cursor, or a user-defined data type that has multiple rows.data type that has multiple rows.

Page 21: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 2121

Using Procedures & FunctionsUsing Procedures & Functions

Multiple-Valued Functions: Multiple-Valued Functions: RulesRules

A multiple-valued functions must return A multiple-valued functions must return the value to the same type variable, which the value to the same type variable, which is done by:is done by: Assignment in a calling PL/SQL block.Assignment in a calling PL/SQL block. Assignment to a bind variable using a system Assignment to a bind variable using a system

reference cursor.reference cursor. A multiple-valued function returning a A multiple-valued function returning a

PL/SQL variable can PL/SQL variable can ONLYONLY be used inside be used inside another PL/SQL block.another PL/SQL block.

Page 22: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 2222

Using Procedures & FunctionsUsing Procedures & Functions

Shared Pool: PinningShared Pool: Pinning Pinning in the SGA shared pool can Pinning in the SGA shared pool can

accelerate and guarantee behavior of accelerate and guarantee behavior of frequently called stored programs:frequently called stored programs: This places the p-code in the shared pool.This places the p-code in the shared pool. This prevents the least used algorithm from This prevents the least used algorithm from

removing it inbetween calls to it.removing it inbetween calls to it. The The DBMS_SHARED_POOL.KEEPDBMS_SHARED_POOL.KEEP enables enables

pinning a stored program into the SGA.pinning a stored program into the SGA. The The DBMS_SHARED_POOL.UNKEEPDBMS_SHARED_POOL.UNKEEP removes a removes a

pinned stored program from the SGA.pinned stored program from the SGA.

Page 23: Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9

20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming

(Chapter 9)(Chapter 9) Page Page 2323

SummarySummary

Data DictionaryData Dictionary SubprogramsSubprograms Definer RightsDefiner Rights Invoker RightsInvoker Rights Purity LevelsPurity Levels Multiple-Valued FunctionsMultiple-Valued Functions Shared PoolShared Pool