Está en la página 1de 20

Copyright 2009, Oracle. All rights reserved.

Introduction to PL/SQL
Copyright 2009, Oracle. All rights reserved. 1 - 2
Objectives
After completing this lesson, you should be able to do the
following:
Explain the need for PL/SQL
Explain the benefits of PL/SQL
Identify the different types of PL/SQL blocks
Output messages in PL/SQL
Copyright 2009, Oracle. All rights reserved. 1 - 3
Agenda
Understanding the benefits and structure of PL/SQL
Examining PL/SQL blocks
Generating output messages in PL/SQL
Copyright 2009, Oracle. All rights reserved. 1 - 4
About PL/SQL
PL/SQL:
Stands for Procedural Language extension to SQL
Is Oracle Corporations standard data access language for
relational databases
Seamlessly integrates procedural constructs with SQL
Copyright 2009, Oracle. All rights reserved. 1 - 5
About PL/SQL
PL/SQL:
Provides a block structure for executable units of code.
Maintenance of code is made easier with such a well-
defined structure.
Provides procedural constructs such as:
Variables, constants, and data types
Control structures such as conditional statements and loops
Reusable program units that are written once and executed
many times
Copyright 2009, Oracle. All rights reserved. 1 - 6
PL/SQL Run-Time Architecture
S
Q
L
P
L
/
S
Q
L
PL/SQL block
procedural
Procedural statement
executor
SQL statement executor
Oracle Server
PL/SQL Engine
Copyright 2009, Oracle. All rights reserved. 1 - 7
Benefits of PL/SQL
Integration of procedural constructs with SQL
Improved performance
SQL
IF...THEN
SQL
ELSE
SQL
END IF;
SQL
SQL 1
SQL 2

Copyright 2009, Oracle. All rights reserved. 1 - 8


Benefits of PL/SQL
Modularized program development
Integration with Oracle tools
Portability
Exception handling
Copyright 2009, Oracle. All rights reserved. 1 - 10
PL/SQL Block Structure
DECLARE (optional)
Variables, cursors, user-defined exceptions
BEGIN (mandatory)
SQL statements
PL/SQL statements
EXCEPTION (optional)
Actions to perform
when exceptions occur
END; (mandatory)
Copyright 2009, Oracle. All rights reserved. 1 - 12
Agenda
Understanding the benefits and structure of PL/SQL
Examining PL/SQL blocks
Generating output messages in PL/SQL
Copyright 2009, Oracle. All rights reserved. 1 - 13
Block Types
Procedure Function Anonymous
PROCEDURE name
IS
BEGIN
--statements
[EXCEPTION]
END;
FUNCTION name
RETURN datatype
IS
BEGIN
--statements
RETURN value;
[EXCEPTION]
END;
[DECLARE]
BEGIN
--statements
[EXCEPTION]
END;
Copyright 2009, Oracle. All rights reserved. 1 - 15
Program Constructs
Application triggers
Application packages
Application procedures
or functions
Anonymous blocks
Tools Constructs
Object types
Database triggers
Stored packages
Stored procedures or
functions
Anonymous blocks
Database Server
Constructs
Object types
Copyright 2009, Oracle. All rights reserved. 1 - 17
Examining an Anonymous Block
An anonymous block in the SQL Developer workspace:
Copyright 2009, Oracle. All rights reserved. 1 - 18
Executing an Anonymous Block
Click the Run Script button to execute the anonymous block:
Run Script (or F5)
Copyright 2009, Oracle. All rights reserved. 1 - 19
Agenda
Understanding the benefits and structure of PL/SQL
Examining PL/SQL blocks
Generating output messages in PL/SQL
Copyright 2009, Oracle. All rights reserved. 1 - 20
Enabling Output of a PL/SQL Block
1. To enable output in SQL Developer, execute the following
command before running the PL/SQL block:
2. Use a predefined Oracle package and its procedure in the
anonymous block:
DBMS_OUTPUT.PUT_LINE
DBMS_OUTPUT.PUT_LINE(' The First Name of the
Employee is ' || v_fname);

SET SERVEROUTPUT ON
Copyright 2009, Oracle. All rights reserved. 1 - 21
Viewing the Output of a PL/SQL Block
Press F5 to execute the
command and PL/SQL
block.
Copyright 2009, Oracle. All rights reserved. 1 - 22
Quiz
A PL/SQL block must consist of the following three sections:
A Declarative section, which begins with the keyword
DECLARE and ends when the executable section starts.
An Executable section, which begins with the keyword
BEGIN and ends with END.
An Exception handling section, which begins with the
keyword EXCEPTION and is nested within the executable
section.
1. True
2. False
Copyright 2009, Oracle. All rights reserved. 1 - 23
Summary
In this lesson, you should have learned how to:
Integrate SQL statements with PL/SQL program constructs
Describe the benefits of PL/SQL
Differentiate between PL/SQL block types
Output messages in PL/SQL
Copyright 2009, Oracle. All rights reserved. 1 - 24
Practice 1: Overview
This practice covers the following topics:
Identifying the PL/SQL blocks that execute successfully
Creating and executing a simple PL/SQL block

También podría gustarte