33
Advanced SQL and PL/SQL Topics

Store programs

Embed Size (px)

Citation preview

Page 1: Store programs

Advanced SQL and PL/SQL Topics

CE
Global to this PPT: Please note that most figure slides have figure caption as slide title, not heading - okay? (The individual slides are commented.)
Page 2: Store programs

Guide to Oracle 10g 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

Page 3: Store programs

Guide to Oracle 10g 3

Types of Oracle 10g Stored Program Units

CE
Slide title is figure caption, not heading - okay?
Page 4: Store programs

Guide to Oracle 10g 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

Page 5: Store programs

Guide to Oracle 10g 5

Stored Program Unit Procedures

• CREATE_PROCEDURE command– Header

– Parameter declarations list

– Program unit body

– Exception section

Page 6: Store programs

Guide to Oracle 10g 6

Syntax to Create a Stored Program Unit Procedure

CE
Slide title is figure caption, not heading - okay?
Page 7: Store programs

Guide to Oracle 10g 7

Creating the Parameter Declarations List

• Defines parameters • Declares associated data types• Parameter mode

– IN

– OUT

– IN OUT

Page 8: Store programs

Guide to Oracle 10g 8

Creating a Stored Procedure in SQL*Plus

CE
Please note that the text in this figure may be hard to read.
Page 9: Store programs

Guide to Oracle 10g 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

Page 10: Store programs

Guide to Oracle 10g 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, ...);

Page 11: Store programs

Guide to Oracle 10g 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

CE
Slide title is figure caption, not heading - okay?Please note that this seems odd since it comes between two slides with the same title/heading (slides 20 and 22).
Page 12: Store programs

Guide to Oracle 10g 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);

Page 13: Store programs

Guide to Oracle 10g 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

Page 14: Store programs

Guide to Oracle 10g 14

Commands to Create a Stored Program Unit Function

CE
Slide title is figure caption, not heading - okay?
Page 15: Store programs

Guide to Oracle 10g 15

Calling a Function

• Syntax:variable_name := function_name(parameter1, parameter2, ...);

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

function declaration

Page 16: Store programs

Guide to Oracle 10g 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

Page 17: Store programs

Guide to Oracle 10g 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

Page 18: Store programs

Guide to Oracle 10g 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

Page 19: Store programs

Guide to Oracle 10g 19

General Syntax for a Package Specification

CE
Slide title is figure caption, not heading - okay?Please note that this seems odd since it comes between two slides with the same title/heading (slides 40 and 42).
Page 20: Store programs

Guide to Oracle 10g 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, ...);

Page 21: Store programs

Guide to Oracle 10g 21

The Package Specification (continued)

• Declare function syntax:FUNCTION function_name

(parameter1 parameter1_data_type,

parameter2 parameter2_data_type, ...)

RETURN return_datatype;

Page 22: Store programs

Guide to Oracle 10g 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

Page 23: Store programs

Guide to Oracle 10g 23

General Syntax for a Package Body

CE
Slide title is figure caption, not heading - okay?Please note that this seems odd since it comes between two slides with the same title/heading (slides 44 and 46).
Page 24: Store programs

Guide to Oracle 10g 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

Page 25: Store programs

Guide to Oracle 10g 25

The Package Body (continued)

• Package exists in user schema– Must grant permission to others– GRANT EXECUTE ON package_name TO username;

Page 26: Store programs

Guide to Oracle 10g 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

Page 27: Store programs

Guide to Oracle 10g 27

Database Trigger Properties

• Trigger timing– BEFORE

– AFTER

• Trigger statement– INSERT

– UPDATE

– DELETE

Page 28: Store programs

Guide to Oracle 10g 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

Page 29: Store programs

Guide to Oracle 10g 29

Creating Database Triggers

• Database trigger header• Trigger body

Page 30: Store programs

Guide to Oracle 10g 30

General Syntax to Create a Trigger in SQL*Plus

CE
Slide title is figure caption, not heading - okay?
Page 31: Store programs

Guide to Oracle 10g 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

Page 32: Store programs

Guide to Oracle 10g 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];

Page 33: Store programs

Guide to Oracle 10g 33

Viewing Information About Triggers

• USER_TRIGGERS data dictionary view– Contains information about triggers