procedures & functions oracle database pl/sql 10g programming
DESCRIPTION
Procedures & Functions Oracle Database PL/SQL 10g Programming. Chapter 8. Procedures & Functions. Functions Procedures Forward Referencing Parameter Mode Positional versus Named Notation Call Statement Package Specifications Package Bodies. Procedures & Functions Function: Definition. - PowerPoint PPT PresentationTRANSCRIPT
Procedures & FunctionsProcedures & FunctionsOracle Database PL/SQL 10g Oracle Database PL/SQL 10g
ProgrammingProgramming
Chapter 8Chapter 8
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 22
Procedures & FunctionsProcedures & Functions
FunctionsFunctions ProceduresProcedures Forward ReferencingForward Referencing Parameter ModeParameter Mode Positional versus Named NotationPositional versus Named Notation Call StatementCall Statement Package SpecificationsPackage Specifications Package BodiesPackage Bodies
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 33
Procedures & FunctionsProcedures & Functions
Function: DefinitionFunction: Definition Functions are black boxes:Functions are black boxes:
Taking values in to process them.Taking values in to process them. Returning a single value as a scalar or compound Returning a single value as a scalar or compound
variable to the left operand of an assignment, or as a variable to the left operand of an assignment, or as a column value in a SQL statement (single value is column value in a SQL statement (single value is misleading because a compound variable can be a misleading because a compound variable can be a collection of a structure).collection of a structure).
Function parameters:Function parameters: Are copies of variables, known as Are copies of variables, known as pass-by-valuepass-by-value
parameters.parameters. Are internally managed as a unidirectional reference, Are internally managed as a unidirectional reference,
which means a reference is passed that disallows which means a reference is passed that disallows changes to the referenced value.changes to the referenced value.
Can not use the NOCOPY hint without raising a Can not use the NOCOPY hint without raising a compilation error.compilation error.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 44
Procedures & FunctionsProcedures & Functions
Function: PrototypeFunction: PrototypeCREATE [OR REPLACE] FUNCTION function_nameCREATE [OR REPLACE] FUNCTION function_name [([(parameter1parameter1 [IN] data_type [:= [IN] data_type [:= null_default1null_default1]] [,[,parameter2parameter2 [IN] data_type [:= [IN] data_type [:= null_default2null_default2]] [,[,parameter(n+1)parameter(n+1) [IN] data_type [:= [IN] data_type [:= null_default(n+1)null_default(n+1)])]]]])]]]RETURN RETURN return_data_typereturn_data_type[AUTHID {CURRENT_USER | DEFINER}] {IS | AS}[AUTHID {CURRENT_USER | DEFINER}] {IS | AS} declaration_statementsdeclaration_statements;;BEGINBEGIN execution_statementsexecution_statements;; RETURN RETURN return_data_variablereturn_data_variable;;END END function_namefunction_name;;/ /
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 55
Procedures & FunctionsProcedures & Functions
Procedure: DefinitionProcedure: Definition Procedures are black boxes:Procedures are black boxes:
Taking values in to process them.Taking values in to process them. Returning any parameter as a scalar or compound variable to Returning any parameter as a scalar or compound variable to
the calling scope of the procedure.the calling scope of the procedure. Cannot be used as the right operand of an assignment because Cannot be used as the right operand of an assignment because
they return no variable as an output, which means a void type they return no variable as an output, which means a void type in many other programming languages.in many other programming languages.
Procedure parameters:Procedure parameters: Are references to variables, known as Are references to variables, known as pass-by-referencepass-by-reference
parameters.parameters. Are internally managed as a unidirectional or bidirectional Are internally managed as a unidirectional or bidirectional
references; the former means a reference is passed that references; the former means a reference is passed that disallows changes to the referenced value and the latter allows disallows changes to the referenced value and the latter allows changes to the value.changes to the value.
Can use the NOCOPY hint without raising a compilation error Can use the NOCOPY hint without raising a compilation error provided the variables are designated as bidirectional provided the variables are designated as bidirectional references.references.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 66
Procedures & FunctionsProcedures & Functions
Procedure: PrototypeProcedure: PrototypeCREATE [OR REPLACE] PROCEDURE CREATE [OR REPLACE] PROCEDURE procedure_nameprocedure_name [([(parameter1parameter1 [IN [OUT]] data_type [:= [IN [OUT]] data_type [:= null_default1null_default1]] [,[,parameter2parameter2 [IN [OUT]] data_type [:= [IN [OUT]] data_type [:= null_default2null_default2]] [,[,parameter(n+1)parameter(n+1) [IN [OUT]] data_type [:= [IN [OUT]] data_type [:= null_default(n+1)null_default(n+1)])]]]])]]][AUTHID {CURRENT_USER | DEFINER}] {IS | AS}[AUTHID {CURRENT_USER | DEFINER}] {IS | AS} declaration_statementsdeclaration_statements;;BEGINBEGIN execution_statementsexecution_statements;;END END procedure_nameprocedure_name;;/ /
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 77
Procedures & FunctionsProcedures & Functions
Forward Referencing: Forward Referencing: DefinitionDefinition
Forward Referencing is how compilers parse Forward Referencing is how compilers parse programs:programs: A single pass compiler only reads the file once, which means A single pass compiler only reads the file once, which means
all items must appear in logical sequence.all items must appear in logical sequence. A double pass compiler reads the file twice, which means a A double pass compiler reads the file twice, which means a
forward reference can be managed without providing a forward reference can be managed without providing a header definition or declared but undefined variable.header definition or declared but undefined variable.
The PL/SQL parse is a single pass compiler:The PL/SQL parse is a single pass compiler: Types, variables, local functions, local procedures, and Types, variables, local functions, local procedures, and
cursors must be declared before referencing them in the cursors must be declared before referencing them in the program.program.
Local functions and procedures are declared by providing a Local functions and procedures are declared by providing a header signature as a stub before they are called in another header signature as a stub before they are called in another subroutine.subroutine.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 88
Procedures & FunctionsProcedures & Functions
Forward Referencing: Forward Referencing: PrototypePrototype
DECLAREDECLARE -- A forward referencing stub-- A forward referencing stub for a local procedure. for a local procedure. PROCEDURE b;PROCEDURE b; -- A local procedure calling a forward referenced procedure.-- A local procedure calling a forward referenced procedure. PROCEDURE a ISPROCEDURE a IS BEGINBEGIN b;b; dbms_output.put_line('This is procedure A.');dbms_output.put_line('This is procedure A.'); END a;END a; -- A local procedure implementation for the forward referencing stub.-- A local procedure implementation for the forward referencing stub. PROCEDURE b ISPROCEDURE b IS BEGINBEGIN dbms_output.put_line('This is procedure B.');dbms_output.put_line('This is procedure B.'); END b;END b;BEGINBEGIN a;a;END;END;//
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 99
Procedures & FunctionsProcedures & Functions
Parameter Mode: DefinitionParameter Mode: Definition Parameter mode is either Parameter mode is either ININ, , IN OUTIN OUT, or , or OUTOUT::
The mode determines whether or not a formal parameter can The mode determines whether or not a formal parameter can be updated after it is passed to a function or procedure.be updated after it is passed to a function or procedure.
The The ININ mode disallows update to the referenced variable in all mode disallows update to the referenced variable in all cases.cases.
The The IN OUTIN OUT or or OUTOUT mode allows update to the referenced mode allows update to the referenced variable in all cases.variable in all cases.
The The IN OUTIN OUT or or OUTOUT mode do not support parameter default mode do not support parameter default values.values.
Functions support:Functions support: Only the Only the ININ mode because they return formal data type other mode because they return formal data type other
than a void.than a void. Procedures support:Procedures support:
All combinations because they return a void data type that is All combinations because they return a void data type that is inaccessible in the PL/SQL environment.inaccessible in the PL/SQL environment.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 1010
Procedures & FunctionsProcedures & Functions
Positional versus Named Positional versus Named NotationNotation
Formal parameters in functions and procedures:Formal parameters in functions and procedures: Are positional specific by data type as a default.Are positional specific by data type as a default. Can have default values, which makes them optional when called Can have default values, which makes them optional when called
by another program unit.by another program unit. Can be referenced by the formal parameter name using the Can be referenced by the formal parameter name using the
named notation assignment operator: named notation assignment operator: =>=>.. Formal parameter design considerations:Formal parameter design considerations:
Organize all optional parameters at the end of the list of Organize all optional parameters at the end of the list of parameters.parameters.
When using positional calls to two or more optional parameters, When using positional calls to two or more optional parameters, a NULL value must be passed to any optional parameter skipped, a NULL value must be passed to any optional parameter skipped, or the value will be sent as the wrong actual parameter:or the value will be sent as the wrong actual parameter:
This raises an exception if the types differ.This raises an exception if the types differ. This raises no advice if the types agree.This raises no advice if the types agree.
When two or more optional parameters are in a parameter list, When two or more optional parameters are in a parameter list, the function or procedure should be called using named the function or procedure should be called using named notation. notation.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 1111
Procedures & FunctionsProcedures & Functions
Call Statement: DefinitionCall Statement: Definition The The CALLCALL statement lets the developer test statement lets the developer test
calling PL/SQL programs from the SQL calling PL/SQL programs from the SQL prompt.prompt.
The The CALLCALL statement can run stored statement can run stored procedures with formal parameters using procedures with formal parameters using any mode:any mode: The The ININ only mode variables can be called by only mode variables can be called by
passing values.passing values. The The IN OUTIN OUT and and OUTOUT mode variable must be mode variable must be
called by passing a bind variable.called by passing a bind variable.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 1212
Procedures & FunctionsProcedures & Functions
Call Statement: PrototypeCall Statement: PrototypeCREATE OR REPLACE PROCEDURE show_callCREATE OR REPLACE PROCEDURE show_call(whom_in IN OUT VARCHAR2) IS(whom_in IN OUT VARCHAR2) IS hello VARCHAR2(10) := 'Hello ';hello VARCHAR2(10) := 'Hello '; whom VARCHAR2(10) := 'World'; whom VARCHAR2(10) := 'World'; BEGINBEGIN whom_in := hello || NVL(whom_in,whom) ||'!';whom_in := hello || NVL(whom_in,whom) ||'!';END show_call;END show_call;//
VARIABLE chosen VARCHAR2(20)VARIABLE chosen VARCHAR2(20)
CALL show_call(:chosen);CALL show_call(:chosen);
SELECT :chosen FROM dual;SELECT :chosen FROM dual;
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 1313
Procedures & FunctionsProcedures & Functions
Package Specification: Package Specification: DefinitionDefinition
Package Specifications:Package Specifications: Define the database catalog for the Define the database catalog for the
package.package. Can define types, global variables, Can define types, global variables,
system reference cursors, functions and system reference cursors, functions and procedures.procedures.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 1414
Procedures & FunctionsProcedures & Functions
Package Specification: Package Specification: DefinitionDefinition
CREATE [OR REPLACE] PACKAGE CREATE [OR REPLACE] PACKAGE package_namepackage_name[AUTHID {CURRENT_USER | DEFINER}] {IS | AS}[AUTHID {CURRENT_USER | DEFINER}] {IS | AS}
[TYPE type_name IS {RECORD | REF CURSOR | TABLE} OF [TYPE type_name IS {RECORD | REF CURSOR | TABLE} OF data_type [%ROWTYPE]data_type [%ROWTYPE]
[(parameter 1 data_type [:= [(parameter 1 data_type [:= null_default1null_default1]] [,[,parameter2parameter2 data_type [:= data_type [:= null_default2null_default2]] [,[,parameter(n+1)parameter(n+1) data_type [:= data_type [:= null_default(n+1)null_default(n+1)]]])]]]])] [INDEX BY BINARY_INTEGER];[INDEX BY BINARY_INTEGER];
… … more_in_later_slide …more_in_later_slide …
END END package_namepackage_name;;//
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 1515
Procedures & FunctionsProcedures & Functions
Package Specification: Package Specification: DefinitionDefinition
CREATE [OR REPLACE] PACKAGE CREATE [OR REPLACE] PACKAGE package_namepackage_name[AUTHID {CURRENT_USER | DEFINER}] {IS | AS}[AUTHID {CURRENT_USER | DEFINER}] {IS | AS} … … covered_in_early_slide …covered_in_early_slide …
FUNCTION function_nameFUNCTION function_name [([(parameter1parameter1 [IN] data_type [:= [IN] data_type [:= null_default1null_default1]] [,[,parameter2parameter2 [IN] data_type [:= [IN] data_type [:= null_default2null_default2]] [,[,parameter(n+1)parameter(n+1) [IN] data_type [:= [IN] data_type [:= null_default(n+1)null_default(n+1)]]])]]]])] RETURN RETURN return_data_typereturn_data_type;;
… … more_in_later_slide …more_in_later_slide …
END END package_namepackage_name;;//
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 1616
Procedures & FunctionsProcedures & Functions
Package Specification: Package Specification: DefinitionDefinition
CREATE [OR REPLACE] PACKAGE CREATE [OR REPLACE] PACKAGE package_namepackage_name[AUTHID {CURRENT_USER | DEFINER}] {IS | AS}[AUTHID {CURRENT_USER | DEFINER}] {IS | AS}
… … covered_in_early_slidecovered_in_early_slide … …
PROCEDURE PROCEDURE procedure_nameprocedure_name [([(parameter1parameter1 [IN [OUT]] data_type [:= [IN [OUT]] data_type [:= null_default1null_default1]] [,[,parameter2parameter2 [IN [OUT]] data_type [:= [IN [OUT]] data_type [:= null_default2null_default2]] [,[,parameter(n+1)parameter(n+1) [IN [OUT]] data_type [:= [IN [OUT]] data_type [:=
null_default(n+1)null_default(n+1)]]])];]]])];
END END package_namepackage_name;;//
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 1717
Procedures & FunctionsProcedures & Functions
Package Body: DefinitionPackage Body: Definition Package Bodies:Package Bodies:
Implement the definitions provided to Implement the definitions provided to the database catalog by the package the database catalog by the package specification.specification.
Can define local types, global variables, Can define local types, global variables, system reference cursors, functions and system reference cursors, functions and procedures, which are not visible procedures, which are not visible externally to the package.externally to the package.
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 1818
Procedures & FunctionsProcedures & Functions
Package Body: DefinitionPackage Body: DefinitionCREATE [OR REPLACE] PACKAGE BODY package_name {IS | AS}CREATE [OR REPLACE] PACKAGE BODY package_name {IS | AS}
-- Define package body only types.-- Define package body only types. [TYPE type_name IS {RECORD | REF CURSOR | TABLE} OF[TYPE type_name IS {RECORD | REF CURSOR | TABLE} OF data_type [%ROWTYPE]data_type [%ROWTYPE] [(parameter 1 data_type [:= null_default1][(parameter 1 data_type [:= null_default1] [,parameter2 data_type [:= null_default2][,parameter2 data_type [:= null_default2] [,parameter(n+1) data_type [:= null_default(n+1)]]])][,parameter(n+1) data_type [:= null_default(n+1)]]])] [INDEX BY BINARY_INTEGER];[INDEX BY BINARY_INTEGER];
… … more_in_later_slidemore_in_later_slide … …
END package_name;END package_name;//
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 1919
Procedures & FunctionsProcedures & Functions
Package Body: DefinitionPackage Body: DefinitionCREATE [OR REPLACE] PACKAGE BODY package_name {IS | AS}CREATE [OR REPLACE] PACKAGE BODY package_name {IS | AS} … … covered_in_early_slide …covered_in_early_slide … FUNCTION function_nameFUNCTION function_name [(parameter1 [IN] data_type [:= null_default1][(parameter1 [IN] data_type [:= null_default1] [,parameter2 [IN] data_type [:= null_default2][,parameter2 [IN] data_type [:= null_default2] [,parameter(n+1) [IN] data_type [:= null_default(n+1)])][,parameter(n+1) [IN] data_type [:= null_default(n+1)])] RETURN return_data_type {IS | AS}RETURN return_data_type {IS | AS} declaration_statements;declaration_statements; BEGINBEGIN execution_statements;execution_statements; RETURN return_data_variable;RETURN return_data_variable; END function_name;END function_name; … … more_in_later_slidemore_in_later_slide … … END package_name;END package_name;//
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 2020
Procedures & FunctionsProcedures & Functions
Package Body: DefinitionPackage Body: DefinitionCREATE [OR REPLACE] PACKAGE BODY package_name {IS | AS}CREATE [OR REPLACE] PACKAGE BODY package_name {IS | AS}
… … covered_in_early_slide …covered_in_early_slide …
PROCEDURE procedure_namePROCEDURE procedure_name [(parameter1 [IN [OUT]] data_type [:= null_default1][(parameter1 [IN [OUT]] data_type [:= null_default1] [,parameter2 [IN [OUT]] data_type [:= null_default2][,parameter2 [IN [OUT]] data_type [:= null_default2] [,parameter(n+1) [IN [OUT]] data_type [:= null_default(n+1)]]])];[,parameter(n+1) [IN [OUT]] data_type [:= null_default(n+1)]]])]; declaration_statements;declaration_statements; BEGINBEGIN execution_statements;execution_statements; END procedure_name;END procedure_name;
… … more_in_later_slidemore_in_later_slide … …
END package_name;END package_name;//
20062006 Oracle Database PL/SQL 10g Programming Oracle Database PL/SQL 10g Programming
(Chapter 8)(Chapter 8) Page Page 2121
SummarySummary
FunctionsFunctions ProceduresProcedures Forward ReferencingForward Referencing Parameter ModeParameter Mode Positional versus Named NotationPositional versus Named Notation Call StatementCall Statement Package SpecificationsPackage Specifications Package BodiesPackage Bodies