HELP! * GREY=local Full HTML for

LOCAL foilset Access of Data using SQL-PL/SQL-OraPerl in Oracle

Given by Gang Cheng, C.W. Ou, Geoffrey C. Fox at CPS616 Basic Information Track for Computational Science on Winter-Spring Semester 97. Foils prepared 5 March 1996
Abstract * Foil Index for this file

See also color IMAGE
General Structure of SQL
Features of Oracle SQL and Examples
The six classes of SQL commands: Data Manipulation Language statements (DML), Data Definition Language statements (DDL), Transaction Control statements, Session Control statements, System Control statements, Embedded SQL statements
Programming in PL/SQL with examples and discussion of difference between SQL and PL/SQL
Oraperl - Access Oracle RDBMS from Perl

Table of Contents for full HTML of Access of Data using SQL-PL/SQL-OraPerl in Oracle


1 CPS 616 January-April 1997
Technologies for the Information Age
Oracle RDBMS and Web Integration
Module 2: Data Access SQL and Progamming PL/SQL in Oracle

2 Abstract of: Module 2: Data Access and Progamming in Oracle
3 Data Access and Progamming in Oracle
4 What we will do in These Lectures
5 Some Basic Terminology for Relational Database Model
6 Examples of Typical Relational Operations
7 Examples of SELECT and PROJECT
8 Examples of JOIN and UNION Select Operations
9 Examples of INTERSECT and recursive embedded Operations
10 Structure Query Language (SQL) - I
11 Structure Query Language - II
12 Data Types and Operators in Oracle SQL
13 Functions in Oracle SQL
14 Classes of Oracle SQL Statements
15 DDL Contrasted to DML
16 Data Definition Language statements (DDL)
17 DDL Statement Examples
18 Data Manipulation Language statements (DML)
19 DML Examples - I
20 DML Examples - II
21 Transaction Control Statements
22 PL/SQL - Oracle's Procedural Language to SQL
23 Advantages of PL/SQL
24 Architecture of The PL/SQL "Engine"
25 PL/SQL Data Types
26 Program Units in PL/SQL
27 PL/SQL Stored Procedure
28 PL/SQL Packages
29 Example of A PL/SQL Package Structure
30 Cursors in PL/SQL - I
31 Cursors in PL/SQL - II
32 An Example of PL/SQL Stored Procedure - I
33 An Example of PL/SQL Stored Procedure - II
34 An Example of PL/SQL Stored Procedure - III
35 How to use Example of PL/SQL Stored Procedure
36 Explanation of Example of PL/SQL Stored Procedure
37 Scope and Life time of Procedure and Package Variables, Constants, and Cursors
38 Oraperl - Oracle Data Access From a Perl Program
39 Major Built-in Functions in Oraperl
40 An Example in OraPerl - Query a Person's Phone Number By Last or First Name - I
41 An Example in OraPerl - Query a Person's Phone Number By Last or First Name - II
42 More detailed information about SQL, PL/SQL and Oraperl, including examples, can be found in the handout and on-line at http://osprey7.npac.syr.edu
:1997/database

This table of Contents Abstract



HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 1 CPS 616 January-April 1997
Technologies for the Information Age
Oracle RDBMS and Web Integration
Module 2: Data Access SQL and Progamming PL/SQL in Oracle

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
Instructor: Geoffrey Fox
Version 3 March 97
teamed with Gang Cheng, Chao-Wei Ou
Syracuse University
111 College Place
Syracuse
New York 13244-4100

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 2 Abstract of: Module 2: Data Access and Progamming in Oracle

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
General Structure of SQL
Features of Oracle SQL and Examples
The six classes of SQL commands: Data Manipulation Language statements (DML), Data Definition Language statements (DDL), Transaction Control statements, Session Control statements, System Control statements, Embedded SQL statements
Programming in PL/SQL with examples and discussion of difference between SQL and PL/SQL
Oraperl - Access Oracle RDBMS from Perl

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 3 Data Access and Progamming in Oracle

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * Critical Information in IMAGE
Full HTML Index
There are several ways to access and manipulate database objects in Oracle -- the diagram shows SQL, PL/SQl, OCI, Pro*C and OraPerl

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 4 What we will do in These Lectures

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
We will cover in this module the basic three languages - SQL, PL/SQL and Oraperl
Another important approach - embedded SQL in a host programming language such as SQL in C (Pro*C) is not covered here but is highly recommended for serious Oracle programmers
Oracle Call Interface (OCI) is used by Oracle system developer

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 5 Some Basic Terminology for Relational Database Model

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
Data Structure
  • relations (files, tables)
  • tuples (records, rows)
  • attributes (fields, columns)
Relation operators
  • scan (select-project) (one or more tables , a condition to satisfy, and an attribute list)
  • sort (reorder)
  • aggregate operators (SUM,AVG,MAX,MIN,...)
  • insert/delete/update modification of tables
  • set operators (union, intersection, difference)
  • join, merge and division
  • operators can be embedded recursively with the characteristics:
    • uniformity of the data and operators as input and output is same data object (feature of 4GL)
    • source of data-flow execution model

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 6 Examples of Typical Relational Operations

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * Critical Information in IMAGE
Full HTML Index
The base database is two tables -- assume for later examples that value a21 is same as b21:

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 7 Examples of SELECT and PROJECT

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * Critical Information in IMAGE
Full HTML Index
Selection Operation: create a new table by limiting the set of rows of a given table according to a specified criterion
Projection Operation: acts on a table by deleting some of its columns

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 8 Examples of JOIN and UNION Select Operations

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * Critical Information in IMAGE
Full HTML Index Addon(SQL:Querying Multiple Tables Through Joins)
Join Operation: a SELECT operation that combines rows from two or more tables. Each returned row contains data from more than one table

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 9 Examples of INTERSECT and recursive embedded Operations

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * Critical Information in IMAGE
Full HTML Index
Embedded Operations: several relational operators can be embedded together recursively to perform complex operations
(remember value of b21 assumed same as a21)

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 10 Structure Query Language (SQL) - I

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index Addon(SQL:what is SQL?)
A database language specifies the semantics of various components of a DBMS: structures and operation of a data model, data definition, data access, security, programming language interface, and data administration
Industry accepted Standard, first introduced by ANSI in 1986, current SQL92 by ANSI and ISO, new standard SQL3 with enhancements in object-oriented data management is undergoing
Portable to all RDBMS systems
Built on relational model, simple and powerful
Non-procedure language, only specify "what-to-do", not "how-to-do" (4GL)
Extended to object-oriented programming interface
  • This extended model competes with fledging object-oriented database in industry

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 11 Structure Query Language - II

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
SQL is a very simple, yet powerful database access language, non-procedural and interpretive language
Oracle SQL has many extensions to the ANSI/ISO standard SQL language
Although some Oracle tools (PL/SQL,Pro*C,Oraperl) and applications simplify or mask the use of SQL, all database operations are performed using SQL
Any other data access method would circumvent the security built into Oracle and potentially compromise data security and integrity
Data-independent --- two-phase database schema (logical and physical database)
SQL in Oracle 7 will do the following:
  • Create tables in the database
  • Store information in tables
  • select exactly the information you need from your database
  • Make changes to your data and the structure of underlying tables, and
  • Combine and calculate data to generate the information you need

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 12 Data Types and Operators in Oracle SQL

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
varchar2, number, long, date, raw, long raw, rowid, char
Operators in Oracle SQL
  • arithmetic operators: +, -, *, /
  • character operators: || (concatenation)
  • comparison operators: =, !=, >, <, >+, <=, IN, NOT IN, ANY, SOME, ALL, between x AND y, exists, x like y, IS NULL
  • logical operators: not, and, or
  • set operators: union, union all, intersect, minus
see handout for details

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 13 Functions in Oracle SQL

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
number functions: abs, ceil, cos, cosh, exp, floor, ln, log, mod, power, round, sign, sin, sinh, sort, tan, tanh, trunc
character functions: chr, concat, initcap, lower, lpad, ltrim, replace, rpad, rtrim, soundex, substr, substrb, translate, upper, ascii, intstr, instrb, length, lengthb
date functions: add_months, last_day, months_between, new_time, next_day, round, sysdate, trunc
conversion functions: chartorowid, convert, hextoraw, rawtohex, rowidtochar, to_char, to_date, to_label, to_number
group functions: avg, count, min,max, stddev, sum, variance

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 14 Classes of Oracle SQL Statements

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * Critical Information in IMAGE
Full HTML Index
These fall into six categories
  • Data Definition Language statements (DDL)
  • Data Manipulation Language statements (DML)
  • Transaction Control statements
  • Session Control statements
  • System Control statements
  • Embedded SQL statements
See handout for details about SQL syntax of each Oracle SQL command

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 15 DDL Contrasted to DML

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
Remember schema are:
  • tablespaces, tables, views, sequences, PL/SQL procedures/functions/packages, synonyms, indexes,clusters.
We gave plenty of examples of schema in first module on Oracle/Databases
The DDL defines the structure and existence of Schema
The DML adjusts the data IN schema

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 16 Data Definition Language statements (DDL)

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
Define, alter the structure of, and drop schema objects. More specifically:
  • create, alter, and drop schema objects and other database structures, including for DBA the database itself and database users (CREATE,ALTER,DROP)
  • change the names of schema objects (RENAME)
  • delete all the data in schema objects without removing the objects' structure (TRUNCATE)
  • gather statistics about schema objects, validate object structure, and list chained rows within objects (ANALYZE)
  • turn auditing options on and off (AUDIT,NOAUDIT)
  • add a comment to the data dictionary (COMMENT)
DDL implicitly commits the preceding transaction, and starts a new transaction

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 17 DDL Statement Examples

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * Critical Information in IMAGE
Full HTML Index Addon(SQL:Update a Row and Delete a Row)

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 18 Data Manipulation Language statements (DML)

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
Query or manipulate data in existing schema objects.
More specifically functionality is:
  • remove rows from tables or views (DELETE)
  • see the execution plan for a SQL statment (EXPLAIN PLAN)
  • add new rows of data into a table or view (INSERT)
  • lock a table or view, temporarily limiting other users' access to it (LOCK TABLE)
  • retrieve data from one or more tables and views (SELECT)
  • change column values in existing rows of a table or view (UPDATE)
DML statements are the most frequently used SQL statements by developers

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 19 DML Examples - I

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index Addon(SQL:Query by Name and Insert a Row)
This statement selects all columns of a row whose last_name field value equals to 'Fox':
  • SELECT * FROM person_info_table WHERE last_name = 'Fox';
This statement inserts a row into the person_info_table table
  • INSERT INTO person_info_table (person_id, last_name, first_name, title, user_name, address) VALUES (99, 'lin', 'tseng-hui', 'gra', 'thlin', '3-211');

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 20 DML Examples - II

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index Addon(SQL:Create Tables)
This statement updates a column of a particular row
  • UPDATE person_info_table SET first_name = 'frank' WHERE user_name = 'thlin';
This statement deletes a row from a table
  • DELETE FROM person_info_table WHERE user_name = 'thlin';

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 21 Transaction Control Statements

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
Transaction control statements manage the changes made by DML statements, and group DML statements into transaction. More specifically:
  • make a transaction's changes permanent (COMMIT)
  • undo the changes in a transaction, either since the transaction started or since a savepoint (ROLLBACK)
  • set a point to which you can roll back (SAVEPOINT)
  • establish properties for a transaction (SET TRANSACTION)

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 22 PL/SQL - Oracle's Procedural Language to SQL

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
Relationship between SQL and PL/SQL
  • PL/SQL is an application development language.
  • while SQL is the basic data access language in Oracle that all other Oracle products and tools use, including PL/SQL engine
  • PL/SQL is a superset of SQL, allowing all the DML statements, cursor operations and the transaction processing statements in SQL
  • SQL is a non-procedural language, only allowing to specify what to do, not how to do.
  • while PL/SQL is the procedual extension of SQL, allowing one to specify both

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 23 Advantages of PL/SQL

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
Procedural language features!
Improved performance - PL/SQL code is stored and shared in a RDBMS server
Enhanced productivity - allow modularity and software reuse
Portability - across all Oracle platforms
"Seamless" integration with the RDBMS server - the procedural option
  • Oracle allows several other options
  • distributed, parallel server, parallel query, context server, media server, web server

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 24 Architecture of The PL/SQL "Engine"

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * Critical Information in IMAGE
Full HTML Index

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 25 PL/SQL Data Types

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * Critical Information in IMAGE
Full HTML Index

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 26 Program Units in PL/SQL

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
PL/SQL Block - an unnamed PL/SQL procedure that groups SQL statements and PL/SQL constructs into a single program unit
Stored Procedures - A PL/SQL block stored in the database and called by name from applications
Stored Functions - identical to procedures except functions always retun a single value to the caller while procedures do not
Stored Packages - a method to encapsulate and store related procedures, functions, variables, and other package constructs together as a unit in the database

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 27 PL/SQL Stored Procedure

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * Critical Information in IMAGE
Full HTML Index
A standalone stored procedure has two parts
  • Specification - declare the procedure with the procedure name and its argument list (names and their databtypes)
  • Body - define the procedure with a PL/SQL block consists of SQL and PL/SQL statement
  • Both parts are created in one step using the CREATE PROCEDURE command
Basic Structure:

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 28 PL/SQL Packages

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
The declaration of a package has two parts
  • package specification - contains the declarations of procedures (and functions), variables, constants, and exceptions that are accessible outside the package
  • package body: defines procedures (and functions), cursors, and exceptions that are declared in the package specification.
    • It also defines those PL/SQL program units and constructs (procedures, variables etc) that are only local to the package body
  • The package specification is created by CREATE PACKAGE command
  • The package body is created by CREATE PACKAGE BODY command

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 29 Example of A PL/SQL Package Structure

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
CREATE OR REPLACE PACKAGE phone_pkg AS
  • PROCEDURE get_phone_by_no (no IN NUMBER);
  • PROCEDURE get_person_by_name (name IN VARCHAR2);
END phone_pkg;
/
CREATE OR REPLACE PACKAGE BODY phone_pkg AS
  • PROCEDURE get_phone_by_no (no IN NUMBER) is
    • Begin
    • <procedure body>
  • End get_phone_by_no;
  • PROCEDURE get_person_by_name (name IN VARCHAR2) is
    • Begin
    • <procedure body>
  • End get_person_by_name;
END phone_pkg;
/

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 30 Cursors in PL/SQL - I

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
A cursor is a handle to or name for a specific private SQL area.
  • such an area is also called a private SQL area.
In application development, a cursor is a named resource available to a program, which can be specifically used for parsing SQL statements embedded with the application
PL/SQL implicitly declares a cursor for all SQL DML statements, including queries that return only one row
For queries that return more than one row, you must explicitly declare a cursor to process the rows individually

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 31 Cursors in PL/SQL - II

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
You can use a cursor to re-execute SQL statements - after each stage of execution, the cursor retains enough information about the statement to re-execute it without starting over again
Cursors are "structured" in that know about SQL -- you can open, close and fetch from a cursor
  • Fetch gets get row from results table -- called a cursor as moves through table
Closing a cursor means the information currently in the associated private area is lost and its memory is deallocated

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 32 An Example of PL/SQL Stored Procedure - I

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index Addon(PL/SQL Query Examples -- Query by Name)
A stored procedure to query the phone-list database by a person's last or first name may look like this:
CREATE PROCEDURE get_phoneno_by_name
(name IN VARCHAR2) IS
CURSOR person_cur(cname IN VARCHAR2) IS
SELECT last_name,first_name,phone_no,phone_type
from person_info_table,phone_list_table
WHERE (person_info_table.person_id =
  • phone_list_table.person_id)
AND ( last_name LIKE ('%' || LOWER(cname) || '%')
OR first_name LIKE ('%' || LOWER(cname) || '%'));
Note that above we have SQL not PL/SQL and variables are column names from database
Argument cname of CURSOR will be supplied when we invoke later!

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 33 An Example of PL/SQL Stored Procedure - II

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index Addon(PL/SQL:Query Examples)
We now define some PL/SQL variables to reference database entries fetched in previous SELECT command
lname person_info_table.last_name%TYPE;
  • -- a variable to hold last name fname person_info_table.first_name%TYPE;
  • -- a variable to hold first name
phone phone_list_table.phone_no%TYPE;
  • -- a variable to hold phone no. ptype phone_list_table.phone_type%TYPE;
  • -- a variable to hold phone type
-- Note format tablename.columnname to
reference database variables and %TYPE syntax to inherit database type in PL/SQL datatype

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 34 An Example of PL/SQL Stored Procedure - III

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index Addon(PL/SQL:Query Examples)
BEGIN
OPEN person_cur(name); -- open the cursor
LOOP -- Fetch each row matching
  • the query into variables repeatedly
FETCH person_cur INTO lname,fname,phone,ptype;
EXIT WHEN person_cur%NOTFOUND;
    • -- check end of result buffer
/* print out the query result */
htp.p('The '||ptype||' phone no. of' ||fname||' '||lname||': '||phone);
END LOOP;
CLOSE person_cur;-- close the cursor after it is done
END get_person_by_name;

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 35 How to use Example of PL/SQL Stored Procedure

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
On invoking the stored procedure with the name 'fox' - get_phoneno_by_name('fox'), one will generate the following two lines:
  • The office phone no. of Geoffrey Fox: 3154432163
  • The home phone no. of Geoffrey Fox: 3156827945
Note a cursor is used to hold the query results in memory for processing
htp.p is the invocation of a procedure p in a package htp to print out characters to stdout
When this procedure is created/compiled, all the objects referred in the procedure (e.g. the two tables) must be present and valid in the RDBMS

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 36 Explanation of Example of PL/SQL Stored Procedure

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
LOWER is a built-in function converting a string into its lower case
|| concatenates two strings into one. E.g., 'CPS'||'616' equals 'CPS616' (same as + in Java, . in Perl)
pname dbname%TYPE implies PL/SQL variable pname inherits data type of dbname
LIKE is a comparison operator such that x LIKE y is TRUE if x does match the pattern y.
The character '%' matches any string of zero or more characters
The SQL statement looks at rows whose last_name or first_name field has the input string cname as a substring in lower case

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 37 Scope and Life time of Procedure and Package Variables, Constants, and Cursors

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
For procedure, their state persists only for the duration of a procedure call and is lost when the procedure execution terminates
For package
  • the state of packages declared in the specification or body (i.e., not within a procedure) persists for the duration of the user's session (i.e, across trnasactions)
  • every session of a package has its own set of package variables, constants and cursors
  • during a session, the package user can modify the values of the package variables and cursors
Overloading of Package Procedure Names - PL/SQL allows two or more procedures in the same package to have the same name.
  • This option is very useful where you need a procedure to accept arguments of different datatypes or different number of arguments

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 38 Oraperl - Oracle Data Access From a Perl Program

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
Perl With Built-in Functions to Access Oracle RDBMS
Oraperl is built by extending the Perl Interpreter through the Iusersubs feature
Built on top the Oracle Call Interface Driver Layer
Oraperl is public domain software

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 39 Major Built-in Functions in Oraperl

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 40 An Example in OraPerl - Query a Person's Phone Number By Last or First Name - I

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
#!/usr/local/bin/oraperl
# Specify the user name/password and a database server
$user_name = "cps616"; $passwd = "xyz"; $database = $ENV{'ORACLE_SID'};
# Login to the ORACLE SQL.
$lda = &ora_login($database, $user_name, $passwd) || die; $ora_errstr;
# Cursor for quering phone number by a person's first or last name
$phone_cursor =
"SELECT last_name, first_name, phone_no, phone_type
FROM person_info_table a, phone_list_table b
WHERE (last_name LIKE ('%' || LOWER(:1) || '%') OR
    • first_name LIKE ('%' || LOWER(:1) || '%'))
AND (a.person_id = b.person_id)";

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 41 An Example in OraPerl - Query a Person's Phone Number By Last or First Name - II

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index
# Open cursor and execute the query
$csr = &ora_open($lda, $phone_cursor);
# Assign actual value in the SQL statement
&ora_bind($csr, 'geoffrey');
# Fetch the query results into array variables and print them out
while (($lname,$fname,$phone,$ptype) =&ora_fetch($csr))) {
print "The $ptype $phone no. of $fname $lname:$phone\n";}
&ora_close($csr); # Close a cursor
&ora_logoff($lda); # disconnect the database server

HELP! * GREY=local HTML version of LOCAL Foils prepared 5 March 1996

Foil 42 More detailed information about SQL, PL/SQL and Oraperl, including examples, can be found in the handout and on-line at http://osprey7.npac.syr.edu
:1997/database

From Access of Data using SQL-PL/SQL-OraPerl in Oracle CPS616 Basic Information Track for Computational Science -- Winter-Spring Semester 97. * See also color IMAGE
Full HTML Index

Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sat May 24 1997