Store programs
Upcoming SlideShare
Loading in...5
×
 

Store programs

on

  • 99 views

 

Statistics

Views

Total Views
99
Views on SlideShare
99
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Store programs Store programs Presentation Transcript

  • 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