0% found this document useful (0 votes)
6 views21 pages

Intro RW

The document outlines a PL/SQL course, detailing lesson objectives, course objectives, and a three-day agenda covering topics such as creating stored procedures, functions, packages, and triggers. It emphasizes the importance of modular programming and the use of PL/SQL blocks for code organization and maintenance. Additionally, it introduces the PL/SQL execution environment and development tools available for coding PL/SQL.

Uploaded by

haithambayoud00
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)
6 views21 pages

Intro RW

The document outlines a PL/SQL course, detailing lesson objectives, course objectives, and a three-day agenda covering topics such as creating stored procedures, functions, packages, and triggers. It emphasizes the importance of modular programming and the use of PL/SQL blocks for code organization and maintenance. Additionally, it introduces the PL/SQL execution environment and development tools available for coding PL/SQL.

Uploaded by

haithambayoud00
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/ 21

I

Introduction

Copyright © 2004, Oracle. All rights reserved.


Lesson Objectives

After completing this lesson, you should be able to do


the following:
• Discuss the goals of the course
• Identify the modular components of PL/SQL:
– Anonymous blocks
– Procedures and functions
– Packages
• Discuss the PL/SQL execution environment
• Describe the database schema and tables that are
used in the course
• List the PL/SQL development environments that
are available in the course

I-2 Copyright © 2004, Oracle. All rights reserved.


Course Objectives

After completing this course, you should be able to do


the following:
• Create, execute, and maintain:
– Procedures and functions with OUT parameters
– Package constructs
– Database triggers
• Manage PL/SQL subprograms and triggers
• Use a subset of Oracle-supplied packages to:
– Generate screen, file, and Web output
– Schedule PL/SQL jobs to run independently
• Build and execute dynamic SQL statements
• Manipulate large objects (LOBs)
I-3 Copyright © 2004, Oracle. All rights reserved.
Course Agenda

Lessons that are to be covered on day 1:


Introduction
1. Creating Stored Procedures
2. Creating Stored Functions
3. Creating Packages
4. Using More Package Concepts

I-4 Copyright © 2004, Oracle. All rights reserved.


Course Agenda

Lessons that are to be covered on day 2:


5. Utilizing Oracle-Supplied Packages in Application
Development
6. Dynamic SQL and Metadata
7. Design Considerations for PL/SQL Code
8. Managing Dependencies

I-5 Copyright © 2004, Oracle. All rights reserved.


Course Agenda

Lessons that are to be covered on day 3:


9. Manipulating Large Objects
10. Creating Triggers
11. Applications for Triggers
12. Understanding and Influencing the PL/SQL
Compiler

I-6 Copyright © 2004, Oracle. All rights reserved.


Human Resources (HR) Schema

I-7 Copyright © 2004, Oracle. All rights reserved.


Creating a Modularized and Layered
Subprogram Design

1 xx xxx xxx
xx xxx xxx 2 3 P
xx xxx xxx ----- --- ---
xx xxx xxx ----- --- ---
----- --- --- P
----- --- --- ----- --- ---
xx xxx xxx P
xx xxx xxx
----- --- ---
• Modularize code into subprograms.
1. Locate code sequences repeated more than once.
2. Create subprogram P containing the repeated code.
3. Modify original code to invoke the new subprogram.
• Create subprogram layers for your application.
– Data access subprogram layer with SQL logic
– Business logic subprogram layer, which may or
may not use data access layer

I-8 Copyright © 2004, Oracle. All rights reserved.


Modularizing Development
with PL/SQL Blocks

• PL/SQL is a block-structured language. The


PL/SQL code block helps modularize code by
using:
– Anonymous blocks
– Procedures and functions
– Packages
– Database triggers
• The benefits of using modular program constructs
are:
– Easy maintenance
– Improved data security and integrity
– Improved performance
– Improved code clarity
I-9 Copyright © 2004, Oracle. All rights reserved.
Review of Anonymous Blocks

Anonymous blocks:
• Form the basic PL/SQL block structure
• Initiate PL/SQL processing tasks from applications
• Can be nested within the executable section of
any PL/SQL block
[DECLARE -- Declaration Section (Optional)
variable declarations; ... ]
BEGIN -- Executable Section (Mandatory)
SQL or PL/SQL statements;
[EXCEPTION -- Exception Section (Optional)
WHEN exception THEN statements; ]
END; -- End of Block (Mandatory)

I-10 Copyright © 2004, Oracle. All rights reserved.


Introduction to PL/SQL Procedures

Procedures are named PL/SQL blocks that perform a


sequence of actions.
CREATE PROCEDURE getemp IS -- header
emp_id employees.employee_id%type;
lname employees.last_name%type;
BEGIN
emp_id := 100;
SELECT last_name INTO lname
FROM EMPLOYEES
WHERE employee_id = emp_id;
DBMS_OUTPUT.PUT_LINE('Last name: '||lname);
END;
/

I-11 Copyright © 2004, Oracle. All rights reserved.


Introduction to PL/SQL Functions

Functions are named PL/SQL blocks that perform a


sequence of actions and return a value. A function can
be invoked from:
• Any PL/SQL block
• A SQL statement (subject to some restrictions)
CREATE FUNCTION avg_salary RETURN NUMBER IS
avg_sal employees.salary%type;
BEGIN
SELECT AVG(salary) INTO avg_sal
FROM EMPLOYEES;
RETURN avg_sal;
END;
/

I-12 Copyright © 2004, Oracle. All rights reserved.


Introduction to PL/SQL Packages

PL/SQL packages have a specification and an optional


body. Packages group related subprograms together.
CREATE PACKAGE emp_pkg IS
PROCEDURE getemp;
FUNCTION avg_salary RETURN NUMBER;
END emp_pkg;
/
CREATE PACKAGE BODY emp_pkg IS
PROCEDURE getemp IS ...
BEGIN ... END;

FUNCTION avg_salary RETURN NUMBER IS ...


BEGIN ... RETURN avg_sal; END;
END emp_pkg;
/

I-13 Copyright © 2004, Oracle. All rights reserved.


Introduction to PL/SQL Triggers

PL/SQL triggers are code blocks that execute when a


specified application, database, or table event occurs.
• Oracle Forms application triggers are standard
anonymous blocks.
• Oracle database triggers have a specific structure.
CREATE TRIGGER check_salary
BEFORE INSERT OR UPDATE ON employees
FOR EACH ROW
DECLARE
c_min constant number(8,2) := 1000.0;
c_max constant number(8,2) := 500000.0;
BEGIN
IF :new.salary > c_max OR
:new.salary < c_min THEN
RAISE_APPLICATION_ERROR(-20000,
'New salary is too small or large');
END IF;
END;
/

I-14 Copyright © 2004, Oracle. All rights reserved.


PL/SQL Execution Environment

The PL/SQL run-time architecture:


PL/SQL engine
Procedural
PL/SQL PL/SQL
Block Block procedural Statement
Executor

sql

Oracle Server
SQL Statement Executor

I-15 Copyright © 2004, Oracle. All rights reserved.


PL/SQL Development Environments

This course provides the following tools for


developing PL/SQL code:
• Oracle SQL*Plus (either the GUI or command-line
versions)
• Oracle iSQL*Plus (used from a browser)
• Oracle JDeveloper IDE

I-16 Copyright © 2004, Oracle. All rights reserved.


Coding PL/SQL in iSQL*Plus

I-17 Copyright © 2004, Oracle. All rights reserved.


Coding PL/SQL in SQL*Plus

I-18 Copyright © 2004, Oracle. All rights reserved.


Coding PL/SQL in Oracle JDeveloper

Edit

Run

I-19 Copyright © 2004, Oracle. All rights reserved.


Summary

In this lesson, you should have learned how to:


• Declare named PL/SQL blocks: Procedures,
functions, packages, and triggers
• Use anonymous (unnamed) PL/SQL blocks to
invoke stored procedures and functions
• Use iSQL*Plus or SQL*Plus for developing PL/SQL
code
• Explain the PL/SQL execution environment:
– The client-side PL/SQL engine for executing
PL/SQL code in Oracle Forms and Oracle Reports
– The server-side PL/SQL engine for executing
PL/SQL code stored in an Oracle database

I-20 Copyright © 2004, Oracle. All rights reserved.


Practice I: Overview

This practice covers the following topics:


• Browsing the HR tables
• Creating a simple PL/SQL procedure
• Creating a simple PL/SQL function
• Using an anonymous block to execute the PL/SQL
procedure and function

I-21 Copyright © 2004, Oracle. All rights reserved.

You might also like