0% found this document useful (0 votes)
47 views22 pages

Advanced SQL and PL/SQL: Guide To Oracle 10g

This document provides an overview of PL/SQL stored program units including procedures and functions. It discusses how to create stored procedures and functions, define parameters, call procedures and functions from SQL*Plus or another PL/SQL program, and debug PL/SQL code. Procedures can accept input parameters and return output values while functions always return a single value. Stored units allow code reuse and interaction between database objects.

Uploaded by

Deepak Malusare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views22 pages

Advanced SQL and PL/SQL: Guide To Oracle 10g

This document provides an overview of PL/SQL stored program units including procedures and functions. It discusses how to create stored procedures and functions, define parameters, call procedures and functions from SQL*Plus or another PL/SQL program, and debug PL/SQL code. Procedures can accept input parameters and return output values while functions always return a single value. Stored units allow code reuse and interaction between database objects.

Uploaded by

Deepak Malusare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Advanced SQL and PL/SQL

Guide to Oracle 10g


Lesson A Objectives
After completing this lesson, you should be able to:
• Work with PL/SQL stored units

By: Deepak Malusare


PL/SQL Stored Program Units
• A program unit is a self-contained group of program
statements that can be used within a large program
• PL/SQL part written in the form builder that may be
called by a form trigger is an example of a unit.
• The unit you created and executed so far in a form
builder or SQL*Plus are anonymous PL/SQL
programs which are
– Programs submitted to the interpreter and run
– Do not interact with other program units
– They only exist within forms

By: Deepak Malusare


PL/SQL Stored Program Units
• Stored PL/SQL program units are:
– program units that other PL/SQL program can
reference,
– and that other database users can use and execute.
• What do stored units can do?
– It can receive input values from other program units
– It can pass output values to other program units
• Stored unit can be linked to database application
components created in Form builder to enhance their
functionality.

By: Deepak Malusare


PL/SQL Stored Program Units
• It can be either
– Server-side program units: stored as database
object and execute on the sever (can be used by all
users)
– Client-side program units: stored in the file system
of a client workstation and execute on the client
workstation

By: Deepak Malusare


Creating Stored program Units
• Stored program units can be either:
– Procedures:
• Can receive multiple input parameters and return multiple
output values (or no output values)
• Can perform an action such insert, update or delete record
– Functions:
• Can receive multiple input parameters and always returns
a single output value

By: Deepak Malusare


Creating Procedures
• CREATE [OR REPLACE] PROCEDURE procedure_name
[ (parameter mod datatype [,parameter]) ]
Header
IS Section
[local variable declaration_section]
BEGIN
body
executable_section
[EXCEPTION
exception_section] Exception
section
END [procedure_name];

By: Deepak Malusare


Creating Procedures

By: Deepak Malusare


Creating Procedures
• The header section defines:
– The procedure name (name should be unique )
– The parameters that the procedure receives or
delivers (IN, OUT, IN OUT)
– IS keyword: follows the parameters list
– The local procedure variables
• Create or replace: instructs the DBMS to create
the procedure if it does it exists, otherwise; it
replaces the existing one
• OR REPLACE clause is optional. But if omitted
and a procedure exists with the same name, an
error occurs
By: Deepak Malusare
Creating Procedures (parameters list)
• Parameter name, mod and data type are enclosed in
parentheses, each is separated by a comma.
• Parameter mod: describes how the procedure change
the value. It can be:
– IN: passed parameter is a Read only value. Cannot be
changed by the procedure
– OUT: write-only value. Always comes on the left side
of an assignment statement
– IN OUT: its value can be changed

By: Deepak Malusare


Creating Procedures (parameters list)
• The data type cannot specify a length, precision, or
scale value for a numerical data type, or maximum
length of a character data type.
• Oracle Database derives the length, precision, or
scale of the return value from the environment from
which the function is called.
• When the procedure or function is
created and debugged, it ca be
called from any application.

By: Deepak Malusare


Creating Procedures (Example)
• UPDATE_ENROLLMENT_GRADE: a procedure
that updates a student’s grade for a course.
• It accepts 3 input parameters (student ID, Course
section ID and grade)
• Upon successful, the stored procedure exists as an
object in your schema.

By: Deepak Malusare


Creating Procedures (Example)

• The (%ROWTYPE) reference data type creates


composite variables that reference theNO Data Size
entire data
record.
– Faculty_row FACULTY%ROWTYPE;

• The variable faculty_row references all of the


columns in the FACULTY table, and each column
has the same data type as its associated DB column.

By: Deepak Malusare


Debugging program units (SQL*Plus)
• Interpreter displays only a warning message. It does
not contain a compiler error message nor a line
location.
• Instead, it writes all compiler errors to a system table
accessed using USER_ERRORS data dictionary
view.
• It displays a summary listing of compile errors
generated by the last program unit that was compiled.
• Use the SHOW ERRORS command:
• See Figure 9-12.

By: Deepak Malusare


Calling a Stored Procedure
1. Execute directly from SQL*Plus command line
2. 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, ...);
Execute update_enrollment_grade(‘MA100’, 12, ‘B’);
– Single quote for character is important.
– Variables passed for each parameter must be in same order as
parameters appear in parameter declarations list
By: Deepak Malusare
Calling a Stored Procedure
• 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’);

By: Deepak Malusare


Creating Functions
• A function is similar to a procedure, except that it
returns a single value.

By: Deepak Malusare


Creating Functions
• The function name (name should be unique )
• The syntax of the parameter list is identical to that of
the procedure.
• After the parameters list, RETURN sentence determines
the data type of the return value of the function.
(remember that procedure does not return a value, so it
contains NO Return statement)
• After IS, the return_value_variable declares the variable
that represents the function return value.

By: Deepak Malusare


Creating Functions

By: Deepak Malusare


Calling a Functions
• Calling a function requires assignment the command
to a previously declared variable in the calling
program, since the function returns a single data
value.
variable_name :=
function_name(parameter1,
parameter2, ...);
• Variables passed for parameter values
– Must be in same order as parameters appear in
function declaration

By: Deepak Malusare


Calling a Functions

• Pay attention to to_date function.

By: Deepak Malusare


Advanced SQL and PL/SQL

Guide to Oracle 10g

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy