Store programs


Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Store programs

  1. 1. Advanced SQL and PL/SQL Topics
  2. 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. 3. Types of Oracle 10g Stored Program Units Guide to Oracle 10g 3
  4. 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. 5. Stored Program Unit Procedures • CREATE_PROCEDURE command – – – – Header Parameter declarations list Program unit body Exception section Guide to Oracle 10g 5
  6. 6. Syntax to Create a Stored Program Unit Procedure Guide to Oracle 10g 6
  7. 7. Creating the Parameter Declarations List • Defines parameters • Declares associated data types • Parameter mode – IN – OUT – IN OUT Guide to Oracle 10g 7
  8. 8. Creating a Stored Procedure in SQL*Plus Guide to Oracle 10g 8
  9. 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. 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. 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. 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. 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. 14. Commands to Create a Stored Program Unit Function Guide to Oracle 10g 14
  15. 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. 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. 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. 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. 19. General Syntax for a Package Specification Guide to Oracle 10g 19
  20. 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. 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. 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. 23. General Syntax for a Package Body Guide to Oracle 10g 23
  24. 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. 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. 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. 27. Database Trigger Properties • Trigger timing – BEFORE – AFTER • Trigger statement – INSERT – UPDATE – DELETE Guide to Oracle 10g 27
  28. 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. 29. Creating Database Triggers • Database trigger header • Trigger body Guide to Oracle 10g 29
  30. 30. General Syntax to Create a Trigger in SQL*Plus Guide to Oracle 10g 30
  31. 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. 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. 33. Viewing Information About Triggers • USER_TRIGGERS data dictionary view – Contains information about triggers Guide to Oracle 10g 33