SlideShare une entreprise Scribd logo
1  sur  33
Advanced SQL and PL/SQL Topics
Overview of PL/SQL Stored Program
Units
• Program unit
– Self-contained group of program statements that
can be used within larger program

•
•
•
•

Anonymous PL/SQL programs
Stored PL/SQL program units
Server-side program units
Client-side program units

Guide to Oracle 10g

2
Types of Oracle 10g Stored Program
Units

Guide to Oracle 10g

3
Creating Stored Program Units
• Procedure
– Receive multiple input parameters
– Return multiple output values or return no output
values
– Perform action such as inserting, updating, or
deleting database records

• Function
– Receive multiple input parameters
– Always returns single output value
Guide to Oracle 10g

4
Stored Program Unit Procedures
• CREATE_PROCEDURE command
–
–
–
–

Header
Parameter declarations list
Program unit body
Exception section

Guide to Oracle 10g

5
Syntax to Create a Stored Program
Unit Procedure

Guide to Oracle 10g

6
Creating the Parameter Declarations
List
• Defines parameters
• Declares associated data types
• Parameter mode
– IN
– OUT
– IN OUT

Guide to Oracle 10g

7
Creating a Stored Procedure in
SQL*Plus

Guide to Oracle 10g

8
Debugging Stored Program Units in
SQL*Plus
• Similar to debugging any program
• Identify program line causing error
• SQL*Plus interpreter displays error warning
message
– Does not automatically display compile error
messages and line locations
– Writes all compile errors to system table
• Access using USER_ERRORS data dictionary view
• Execute SHOW ERRORS command
Guide to Oracle 10g

9
Calling a Stored Procedure
• Execute directly from SQL*Plus command line
• Create separate PL/SQL program that contains
– Command to call stored procedure
– Passes parameter values to procedure

• Calling stored procedure from SQL*Plus
command line:
EXECUTE procedure_name
(parameter1_value,
parameter2_value, ...);
Guide to Oracle 10g

10
Passing Parameters to a Procedure

•IN The value of the parameter is passed into the procedure when the
procedure is invoked. It is similar to read-only
•OUT Any value the parameter has when it is called is ignored. When the
procedure finishes, any value assigned to the parameter during its execution is
returned to the calling environment. It is similar to write-only IN OUT This
mode is a combination of both IN and OUT. The value of the parameter can be
passed into the procedure when the procedure is invoked. It is then
manipulated within the procedure and returned to the calling environment. It is
similar to read-write
Guide to Oracle 10g

11
Calling a Stored Procedure
(continued)
• Variables passed for each parameter
– Must be in same order as parameters appear in
parameter declarations list

• Calling stored procedure from separate PL/SQL
program
– Similar to calling stored procedure from SQL*Plus
command line
– Omit EXECUTE command
– update_enrollment_grade(MA100, 12, B);
Guide to Oracle 10g

12
Creating a Stored Program Unit
Function
• Use CREATE OR REPLACE FUNCTION
command
• function_return_value_datatype
– Defines data type that function returns

• return_value_variable
– Declares variable that represents function return
value

• RETURN command
Guide to Oracle 10g

13
Commands to Create a Stored
Program Unit Function

Guide to Oracle 10g

14
Calling a Function
• Syntax:
variable_name :=
function_name(parameter1, paramete
r2, ...);

• Variables passed for parameter values
– Must be in same order as parameters appear in
function declaration

Guide to Oracle 10g

15
Packages
• Code library containing related program units and
variables
• Stored in database
• Executes on database server
• Grant other users privilege to use package
– Any PL/SQL program can reference package
procedures and functions

• More functionality than PL/SQL libraries
• More convenient to use than PL/SQL libraries
Guide to Oracle 10g

16
The Package Specification
• Also called package header
• Declares public package objects, including:
–
–
–
–

Variables
Cursors
Procedures
Functions

• Made public
– Program units outside package can reference
package’s objects
Guide to Oracle 10g

17
The Package Specification (continued)
• Public variables
– Visible to many different PL/SQL programs
– Values remain in memory even after programs that
declare and reference them terminate
– Declare public variable in DECLARE section of
package
• Same syntax used to declare private variable

Guide to Oracle 10g

18
General Syntax for a Package
Specification

Guide to Oracle 10g

19
The Package Specification (continued)
• Declare variables and cursors in packages
– Same syntax used in other PL/SQL programs

• Declare procedure syntax:
PROCEDURE procedure_name
(parameter1 parameter1_data_type,
parameter2 parameter2_data_type, ...);

Guide to Oracle 10g

20
The Package Specification (continued)
• Declare function syntax:
FUNCTION function_name
(parameter1 parameter1_data_type,
parameter2 parameter2_data_type, ...)
RETURN return_datatype;

Guide to Oracle 10g

21
The Package Body
• Contains commands to create program units that
package specification declares
• Must create specification before body
• Optional
• Variables declared at beginning of package body
– Private to package

• Each program unit in package specification must
be defined
Guide to Oracle 10g

22
General Syntax for a Package Body

Guide to Oracle 10g

23
The Package Body (continued)
•
•
•
•

Create package using SQL*Plus
Create package specification in SQL*Plus
Create package body in SQL*Plus
Reference package objects syntax:
– package_name.item_name

Guide to Oracle 10g

24
The Package Body (continued)
• Package exists in user schema
– Must grant permission to others
– GRANT EXECUTE ON package_name TO
username;

Guide to Oracle 10g

25
Database Triggers
• Program units
– Execute in response to database events of
inserting, updating, or deleting record

• Different from form triggers
• Useful for maintaining integrity constraints
• Similar to other program units
– But cannot accept parameters

Guide to Oracle 10g

26
Database Trigger Properties
• Trigger timing
– BEFORE
– AFTER

• Trigger statement
– INSERT
– UPDATE
– DELETE

Guide to Oracle 10g

27
Database Trigger Properties
(continued)
• Trigger level
– Statement-level
– Row-level

• Reference value of field in current record
– Both before and after triggering statement executes
• :OLD.fieldname
• :NEW.fieldname

Guide to Oracle 10g

28
Creating Database Triggers
• Database trigger header
• Trigger body

Guide to Oracle 10g

29
General Syntax to Create a Trigger in
SQL*Plus

Guide to Oracle 10g

30
Creating Database Triggers to Leave
an Audit Trail for the Northwoods
University ENROLLMENT Table
• Track when users insert, update, and delete table
records
• Create trigger that leaves audit trail
– Create one or more tables to store audit trail values

• Create database trigger in SQL*Plus
• Create database trigger in Forms Builder

Guide to Oracle 10g

31
Disabling and Dropping Triggers
• Drop trigger when not needed:
– DROP TRIGGER trigger_name;

• Disable trigger
– Trigger still exists in user’s database schema
– No longer fires when triggering event occurs
– Syntax:
• ALTER TRIGGER trigger_name [ENABLE
| DISABLE];
Guide to Oracle 10g

32
Viewing Information About Triggers
• USER_TRIGGERS data dictionary view
– Contains information about triggers

Guide to Oracle 10g

33

Contenu connexe

Tendances

Less17 flashback tb3
Less17 flashback tb3Less17 flashback tb3
Less17 flashback tb3
Imran Ali
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
Ashwin Kumar
 
Case_Study_-_Advanced_Oracle_PLSQL
Case_Study_-_Advanced_Oracle_PLSQLCase_Study_-_Advanced_Oracle_PLSQL
Case_Study_-_Advanced_Oracle_PLSQL
Ziemowit Jankowski
 

Tendances (20)

R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 11R12 d49656 gc10-apps dba 11
R12 d49656 gc10-apps dba 11
 
(ATS6-DEV01) What’s new for Protocol and Component Developers in AEP 9.0
(ATS6-DEV01) What’s new for Protocol and Component Developers in AEP 9.0(ATS6-DEV01) What’s new for Protocol and Component Developers in AEP 9.0
(ATS6-DEV01) What’s new for Protocol and Component Developers in AEP 9.0
 
Oracle-L11 using Oracle flashback technology-Mazenet solution
Oracle-L11 using  Oracle flashback technology-Mazenet solutionOracle-L11 using  Oracle flashback technology-Mazenet solution
Oracle-L11 using Oracle flashback technology-Mazenet solution
 
Nov. 4, 2011 o reilly webcast-hbase- lars george
Nov. 4, 2011 o reilly webcast-hbase- lars georgeNov. 4, 2011 o reilly webcast-hbase- lars george
Nov. 4, 2011 o reilly webcast-hbase- lars george
 
Oracle flashback
Oracle flashbackOracle flashback
Oracle flashback
 
Less17 flashback tb3
Less17 flashback tb3Less17 flashback tb3
Less17 flashback tb3
 
Jsp project module
Jsp project moduleJsp project module
Jsp project module
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 
Case_Study_-_Advanced_Oracle_PLSQL
Case_Study_-_Advanced_Oracle_PLSQLCase_Study_-_Advanced_Oracle_PLSQL
Case_Study_-_Advanced_Oracle_PLSQL
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
Hovitaga OpenSQL Editor - Comparison with SE16 and SAP query
Hovitaga OpenSQL Editor - Comparison with SE16 and SAP queryHovitaga OpenSQL Editor - Comparison with SE16 and SAP query
Hovitaga OpenSQL Editor - Comparison with SE16 and SAP query
 
plsql les03
 plsql les03 plsql les03
plsql les03
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
Improving the Performance of PL/SQL function calls from SQL
Improving the Performance of PL/SQL function calls from SQLImproving the Performance of PL/SQL function calls from SQL
Improving the Performance of PL/SQL function calls from SQL
 
Plsql
PlsqlPlsql
Plsql
 
R12 d49656 gc10-apps dba 06
R12 d49656 gc10-apps dba 06R12 d49656 gc10-apps dba 06
R12 d49656 gc10-apps dba 06
 
08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata
 
Lecture 2. MS SQL. Stored procedures.
Lecture 2. MS SQL. Stored procedures.Lecture 2. MS SQL. Stored procedures.
Lecture 2. MS SQL. Stored procedures.
 
plsql les02
 plsql les02 plsql les02
plsql les02
 
07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development
 

Similaire à Store programs

Exploring plsql new features best practices september 2013
Exploring plsql new features best practices   september 2013Exploring plsql new features best practices   september 2013
Exploring plsql new features best practices september 2013
Andrejs Vorobjovs
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
Syed Asrarali
 
COUG_AAbate_Oracle_Database_12c_New_Features
COUG_AAbate_Oracle_Database_12c_New_FeaturesCOUG_AAbate_Oracle_Database_12c_New_Features
COUG_AAbate_Oracle_Database_12c_New_Features
Alfredo Abate
 

Similaire à Store programs (20)

Oracle applications r12.2, ebr, online patching means lot of work for devel...
Oracle applications r12.2, ebr, online patching   means lot of work for devel...Oracle applications r12.2, ebr, online patching   means lot of work for devel...
Oracle applications r12.2, ebr, online patching means lot of work for devel...
 
Db Triggers05ch
Db Triggers05chDb Triggers05ch
Db Triggers05ch
 
Plsql guide 2
Plsql guide 2Plsql guide 2
Plsql guide 2
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
The Amazing and Elegant PL/SQL Function Result Cache
The Amazing and Elegant PL/SQL Function Result CacheThe Amazing and Elegant PL/SQL Function Result Cache
The Amazing and Elegant PL/SQL Function Result Cache
 
Plsql les04
Plsql les04Plsql les04
Plsql les04
 
Sql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices ISql and PL/SQL Best Practices I
Sql and PL/SQL Best Practices I
 
ebs-performance-tuning-part-1-470542.pdf
ebs-performance-tuning-part-1-470542.pdfebs-performance-tuning-part-1-470542.pdf
ebs-performance-tuning-part-1-470542.pdf
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
Exploring plsql new features best practices september 2013
Exploring plsql new features best practices   september 2013Exploring plsql new features best practices   september 2013
Exploring plsql new features best practices september 2013
 
MOUG17: SQLT Utility for Tuning - Practical Examples
MOUG17: SQLT Utility for Tuning - Practical ExamplesMOUG17: SQLT Utility for Tuning - Practical Examples
MOUG17: SQLT Utility for Tuning - Practical Examples
 
MOUG17: DB Security; Secure your Data
MOUG17: DB Security; Secure your DataMOUG17: DB Security; Secure your Data
MOUG17: DB Security; Secure your Data
 
0396 oracle-goldengate-12c-tutorial
0396 oracle-goldengate-12c-tutorial0396 oracle-goldengate-12c-tutorial
0396 oracle-goldengate-12c-tutorial
 
Intro to tsql
Intro to tsqlIntro to tsql
Intro to tsql
 
Intro to tsql unit 14
Intro to tsql   unit 14Intro to tsql   unit 14
Intro to tsql unit 14
 
COUG_AAbate_Oracle_Database_12c_New_Features
COUG_AAbate_Oracle_Database_12c_New_FeaturesCOUG_AAbate_Oracle_Database_12c_New_Features
COUG_AAbate_Oracle_Database_12c_New_Features
 
ow.ppt
ow.pptow.ppt
ow.ppt
 
ow.ppt
ow.pptow.ppt
ow.ppt
 
Ow
OwOw
Ow
 

Plus de Hitesh Kumar Markam (19)

Concepts of Distributed Computing & Cloud Computing
Concepts of Distributed Computing & Cloud Computing Concepts of Distributed Computing & Cloud Computing
Concepts of Distributed Computing & Cloud Computing
 
Data guard
Data guardData guard
Data guard
 
Tunning overview
Tunning overviewTunning overview
Tunning overview
 
Resize sga
Resize sgaResize sga
Resize sga
 
Rman offline backup
Rman offline backupRman offline backup
Rman offline backup
 
Oracle shutdown
Oracle shutdownOracle shutdown
Oracle shutdown
 
Log miner in oracle.ppt
Log miner in oracle.pptLog miner in oracle.ppt
Log miner in oracle.ppt
 
Dba in 2 days unit no 9
Dba in 2 days unit no 9Dba in 2 days unit no 9
Dba in 2 days unit no 9
 
5 backuprecoveryw imp
5 backuprecoveryw imp5 backuprecoveryw imp
5 backuprecoveryw imp
 
Rmanpres
RmanpresRmanpres
Rmanpres
 
Pl sql
Pl sqlPl sql
Pl sql
 
Oracle archi ppt
Oracle archi pptOracle archi ppt
Oracle archi ppt
 
Lecture2 oracle ppt
Lecture2 oracle pptLecture2 oracle ppt
Lecture2 oracle ppt
 
Dbms objective and subjective notes
Dbms objective and subjective notesDbms objective and subjective notes
Dbms objective and subjective notes
 
Dba in 2 days
Dba in 2 daysDba in 2 days
Dba in 2 days
 
Creating database
Creating databaseCreating database
Creating database
 
1 plsql introduction1
1 plsql introduction11 plsql introduction1
1 plsql introduction1
 
javascript code for mysql database connection
javascript code for mysql database connectionjavascript code for mysql database connection
javascript code for mysql database connection
 
Advanced Planning And Optimization
Advanced Planning And OptimizationAdvanced Planning And Optimization
Advanced Planning And Optimization
 

Dernier

IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
17thcssbs2
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Dernier (20)

TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
The Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptxThe Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptx
 
MichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdfMichaelStarkes_UncutGemsProjectSummary.pdf
MichaelStarkes_UncutGemsProjectSummary.pdf
 
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdfPost Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
Post Exam Fun(da) Intra UEM General Quiz 2024 - Prelims q&a.pdf
 
Essential Safety precautions during monsoon season
Essential Safety precautions during monsoon seasonEssential Safety precautions during monsoon season
Essential Safety precautions during monsoon season
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf
 
IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
How to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryHow to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 Inventory
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
Behavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdfBehavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdf
 

Store programs

  • 1. Advanced SQL and PL/SQL Topics
  • 2. Overview of PL/SQL Stored Program Units • Program unit – Self-contained group of program statements that can be used within larger program • • • • Anonymous PL/SQL programs Stored PL/SQL program units Server-side program units Client-side program units Guide to Oracle 10g 2
  • 3. Types of Oracle 10g Stored Program Units Guide to Oracle 10g 3
  • 4. Creating Stored Program Units • Procedure – Receive multiple input parameters – Return multiple output values or return no output values – Perform action such as inserting, updating, or deleting database records • Function – Receive multiple input parameters – Always returns single output value Guide to Oracle 10g 4
  • 5. Stored Program Unit Procedures • CREATE_PROCEDURE command – – – – Header Parameter declarations list Program unit body Exception section Guide to Oracle 10g 5
  • 6. Syntax to Create a Stored Program Unit Procedure Guide to Oracle 10g 6
  • 7. Creating the Parameter Declarations List • Defines parameters • Declares associated data types • Parameter mode – IN – OUT – IN OUT Guide to Oracle 10g 7
  • 8. Creating a Stored Procedure in SQL*Plus Guide to Oracle 10g 8
  • 9. Debugging Stored Program Units in SQL*Plus • Similar to debugging any program • Identify program line causing error • SQL*Plus interpreter displays error warning message – Does not automatically display compile error messages and line locations – Writes all compile errors to system table • Access using USER_ERRORS data dictionary view • Execute SHOW ERRORS command Guide to Oracle 10g 9
  • 10. Calling a Stored Procedure • Execute directly from SQL*Plus command line • Create separate PL/SQL program that contains – Command to call stored procedure – Passes parameter values to procedure • Calling stored procedure from SQL*Plus command line: EXECUTE procedure_name (parameter1_value, parameter2_value, ...); Guide to Oracle 10g 10
  • 11. Passing Parameters to a Procedure •IN The value of the parameter is passed into the procedure when the procedure is invoked. It is similar to read-only •OUT Any value the parameter has when it is called is ignored. When the procedure finishes, any value assigned to the parameter during its execution is returned to the calling environment. It is similar to write-only IN OUT This mode is a combination of both IN and OUT. The value of the parameter can be passed into the procedure when the procedure is invoked. It is then manipulated within the procedure and returned to the calling environment. It is similar to read-write Guide to Oracle 10g 11
  • 12. Calling a Stored Procedure (continued) • Variables passed for each parameter – Must be in same order as parameters appear in parameter declarations list • Calling stored procedure from separate PL/SQL program – Similar to calling stored procedure from SQL*Plus command line – Omit EXECUTE command – update_enrollment_grade(MA100, 12, B); Guide to Oracle 10g 12
  • 13. Creating a Stored Program Unit Function • Use CREATE OR REPLACE FUNCTION command • function_return_value_datatype – Defines data type that function returns • return_value_variable – Declares variable that represents function return value • RETURN command Guide to Oracle 10g 13
  • 14. Commands to Create a Stored Program Unit Function Guide to Oracle 10g 14
  • 15. Calling a Function • Syntax: variable_name := function_name(parameter1, paramete r2, ...); • Variables passed for parameter values – Must be in same order as parameters appear in function declaration Guide to Oracle 10g 15
  • 16. Packages • Code library containing related program units and variables • Stored in database • Executes on database server • Grant other users privilege to use package – Any PL/SQL program can reference package procedures and functions • More functionality than PL/SQL libraries • More convenient to use than PL/SQL libraries Guide to Oracle 10g 16
  • 17. The Package Specification • Also called package header • Declares public package objects, including: – – – – Variables Cursors Procedures Functions • Made public – Program units outside package can reference package’s objects Guide to Oracle 10g 17
  • 18. The Package Specification (continued) • Public variables – Visible to many different PL/SQL programs – Values remain in memory even after programs that declare and reference them terminate – Declare public variable in DECLARE section of package • Same syntax used to declare private variable Guide to Oracle 10g 18
  • 19. General Syntax for a Package Specification Guide to Oracle 10g 19
  • 20. The Package Specification (continued) • Declare variables and cursors in packages – Same syntax used in other PL/SQL programs • Declare procedure syntax: PROCEDURE procedure_name (parameter1 parameter1_data_type, parameter2 parameter2_data_type, ...); Guide to Oracle 10g 20
  • 21. The Package Specification (continued) • Declare function syntax: FUNCTION function_name (parameter1 parameter1_data_type, parameter2 parameter2_data_type, ...) RETURN return_datatype; Guide to Oracle 10g 21
  • 22. The Package Body • Contains commands to create program units that package specification declares • Must create specification before body • Optional • Variables declared at beginning of package body – Private to package • Each program unit in package specification must be defined Guide to Oracle 10g 22
  • 23. General Syntax for a Package Body Guide to Oracle 10g 23
  • 24. The Package Body (continued) • • • • Create package using SQL*Plus Create package specification in SQL*Plus Create package body in SQL*Plus Reference package objects syntax: – package_name.item_name Guide to Oracle 10g 24
  • 25. The Package Body (continued) • Package exists in user schema – Must grant permission to others – GRANT EXECUTE ON package_name TO username; Guide to Oracle 10g 25
  • 26. Database Triggers • Program units – Execute in response to database events of inserting, updating, or deleting record • Different from form triggers • Useful for maintaining integrity constraints • Similar to other program units – But cannot accept parameters Guide to Oracle 10g 26
  • 27. Database Trigger Properties • Trigger timing – BEFORE – AFTER • Trigger statement – INSERT – UPDATE – DELETE Guide to Oracle 10g 27
  • 28. Database Trigger Properties (continued) • Trigger level – Statement-level – Row-level • Reference value of field in current record – Both before and after triggering statement executes • :OLD.fieldname • :NEW.fieldname Guide to Oracle 10g 28
  • 29. Creating Database Triggers • Database trigger header • Trigger body Guide to Oracle 10g 29
  • 30. General Syntax to Create a Trigger in SQL*Plus Guide to Oracle 10g 30
  • 31. Creating Database Triggers to Leave an Audit Trail for the Northwoods University ENROLLMENT Table • Track when users insert, update, and delete table records • Create trigger that leaves audit trail – Create one or more tables to store audit trail values • Create database trigger in SQL*Plus • Create database trigger in Forms Builder Guide to Oracle 10g 31
  • 32. Disabling and Dropping Triggers • Drop trigger when not needed: – DROP TRIGGER trigger_name; • Disable trigger – Trigger still exists in user’s database schema – No longer fires when triggering event occurs – Syntax: • ALTER TRIGGER trigger_name [ENABLE | DISABLE]; Guide to Oracle 10g 32
  • 33. Viewing Information About Triggers • USER_TRIGGERS data dictionary view – Contains information about triggers Guide to Oracle 10g 33