Sie sind auf Seite 1von 62

Database Programming with

PL/SQL
Introduction to PL/SQL

1 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

Objectives

This lesson covers the following objectives:


• Describe PL/SQL
• Differentiate between SQL and PL/SQL
• Explain the need for PL/SQL

2 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

Purpose

PL/SQL is Oracle Corporation’s standard procedural


language for relational databases. To describe PL/SQL
you learn its characteristics and get better prepared to
describe the differences between PL/SQL and SQL.

Understanding the limitations of SQL will help you to


understand why the PL/SQL language is needed. You
understand how important it is not to lose a key to your
house when you are trying to get into the house without
the key.

3 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

PL/SQL Description

Procedural Language extension to SQL:


• Allows basic program logic and control flow to be
combined with SQL statements.
• Is an Oracle proprietary programming language.
– It can be used only with an Oracle database or tool.

4 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

PL/SQL Description (cont.)

Procedural Language extension to SQL:


• Is a procedural language.
– It produces a result when a series of instructions are
followed.
• Is a 3GL (third-generation programming language).
– It is a “high-level” programming language.

5 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

Structured Query Language (SQL) Description

SQL:
• Is the primary language used to access and modify data
in a relational database.
• Is a nonprocedural language.
– Also known as a "declarative language," it allows the
programmer to focus on input and output rather than the
program steps.
• Is a 4GL (fourth-generation-programming language).
– A language that is closer to natural language than a
programming language; query languages are generally
4GL.
6 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Introduction to PL/SQL

Structured Query Language (SQL) Description


(cont.)
SQL:
• Is a common query language for many types of
databases, including Oracle.
• Has been standardized by the American National
Standards Institute (ANSI).

7 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

SQL Statement

The SQL statement shown is simple and straightforward.


However, if you want to alter any of the retrieved data in a
conditional manner (if the data is xyz then do this to it),
you come across the limitations of SQL.
SELECT class_id, stu_id,
final_numeric_grade, final_letter_grade
FROM enrollments;

For example, how would you write an SQL statement to


update the final_letter_grade data with varying letter
grades for students in different classes?

8 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

Limitations of SQL
For class_id=1
If 66<final_numeric_grade<75 then final_letter_grade=A
If 56<final_numeric_grade<65 then final_letter_grade=B
If 46<final_numeric_grade<55 then final_letter_grade=C
If 36<final_numeric_grade<45 then final_letter_grade=D
Otherwise, final_letter_grade=F

CLASS_ID STU_ID FINAL_NUMERIC_GRADE FINAL_LETTER_GRADE


1 101 75
1 107 71
1 131 65
2 155 91
2 114 93
For class_id=2
If 91<numeric_grade<100 then final_letter_grade=A
If 81<numeric_grade<90 then final_letter_grade=B
If 71<numeric_grade<80 then final_letter_grade=C
If 71<numeric_grade<80 then final_letter_grade=D
Otherwise, final_letter_grade=F

9 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

Limitations of SQL (cont.)

One solution to updating the final letter grade data is


shown. How many SQL statements do you need to write
for class_id=1? For class_id=2? What if there were 20
classes?
UPDATE enrollments
SET final_letter_grade='A'
WHERE class_id=1 AND
Final_numeric_grade BETWEEN 66 and 75;

UPDATE enrollments
SET final_letter_grade='B'
WHERE class_id=1 AND
Final_numeric_grade between 56 and 65;

And so on…

10 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

Limitations of SQL (cont.)

One solution is to write one SQL statement for each


class_id plus number_grade combination. This results in
five SQL statements for class_id=1:
UPDATE enrollments SET final_letter_grade='A'
WHERE class_id=1
AND final_numeric_grade BETWEEN 66 and 75;
UPDATE enrollments SET final_letter_grade='B'
WHERE class_id=1
AND final_numeric_grade BETWEEN 56 and 65;
UPDATE enrollments SET final_letter_grade='C'
WHERE class_id=1
AND final_numeric_grade BETWEEN 46 and 55;
UPDATE enrollments SET final_letter_grade='D'
WHERE class_id=1
AND final_numeric_grade BETWEEN 36 and 45;
UPDATE enrollments SET final_letter_grade='F'
WHERE class_id=1
AND final_numeric_grade <=35;

11 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

Limitations of SQL (cont.)

This is a lot of statements and it does not even include the


statements for the other class IDs! Similarly, there would
be five statements for class_id=2.

It would be easier to write a single statement to


accomplish this task. The statement would require logic,
otherwise known as conditional or procedural logic.

PL/SQL extends SQL with procedural logic.

12 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

PL/SQL Extends SQL With Procedural Logic

DECLARE
v_new_letter_grade varchar2(1);
CURSOR c_enrollments IS
SELECT stu_id, final_numeric_grade FROM enrollments WHERE class_id=1;
BEGIN
FOR c1 in c_enrollments
LOOP
IF c1.final_numeric_grade BETWEEN 66 and 75 THEN v_new_letter_grade := 'A';
ELSIF c1.final_numeric_grade BETWEEN 56 AND 65 THEN v_new_letter_grade := 'B';
ELSIF c1.final_numeric_grade BETWEEN 46 AND 55 THEN v_new_letter_grade := 'C';
ELSIF c1.final_numeric_grade BETWEEN 36 AND 45 THEN v_new_letter_grade := 'D';
ELSE
v_new_letter_grade := 'F';
END IF;
UPDATE enrollments
SET final_letter_grade=v_new_letter_grade WHERE class_id=1
AND stu_id=c1.stu_id;
END LOOP;
COMMIT;
END;

13 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

Procedural Constructs

You use PL/SQL to write the procedural code, and embed


SQL data-accessing statements within the PL/SQL code.
• The PL/SQL code uses variables, cursors, and
conditional logic.
• PL/SQL provides procedural constructs, such as:
– Variables, constants, and types
– Control structures, such as conditional statements and
loops
– Reusable program units that are written once and executed
many times

14 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

Procedural Constructs Highlighted

DECLARE Cursor

BEGIN
FOR c1 IN c_enrollments
Iterative LOOP
Control IF c1.final_numeric_grade BETWEEN 66 AND 75 THEN
v_new_letter_grade := 'A';
ELSIF c1.final_numeric_grade BETWEEN 56 AND 65 THEN
v_new_letter_grade := 'B';

ELSE Conditional
v_new_letter_grade := 'F'; Control
END IF;
UPDATE enrollments
SET final_letter_grade=v_new_letter_grade
SQL WHERE class_id=1
AND stu_id=c1.stu_id;
END LOOP; Variable
END;

15 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

Characteristics of PL/SQL

PL/SQL:
• Is a highly structured, readable, and accessible
language.
• Is a standard and portable language for Oracle
development.
• Is an embedded language and it works with SQL.
• Is a high-performance, highly integrated database
language.
• Is based on the ADA language and has many similarities
in syntax.

16 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

Terminology

Key terms used in this lesson included:


• PL/SQL
• Procedural Constructs

17 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

Summary

In this lesson, you should have learned how to:


• Describe PL/SQL
• Differentiate between SQL and PL/SQL
• Explain the need for PL/SQL

18 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Database Programming with
PL/SQL
Benefits of PL/SQL

1 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Objectives

This lesson covers the following objectives:


• List and explain the benefits of PL/SQL
• List the differences between PL/SQL and other
programming languages
• Give examples of how to use PL/SQL in other Oracle
products

2 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Purpose

Have you ever explained to someone the reason why one


type of shoe is better than another, given a specific
condition?

In this lesson, you learn about the benefits of the PL/SQL


programming language and how it compares to other
programming languages. You also learn how PL/SQL
relates to other Oracle products.

3 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Benefits of PL/SQL

There are many benefits to using the PL/SQL


programming language with an Oracle database.
1. Integration of procedural constructs with SQL
2. Modularized program development
3. Improved performance
4. Integration with Oracle tools
5. Portability
6. Exception handling

4 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Benefit 1: Integration of Procedural Constructs


With SQL
The first and foremost advantage of PL/SQL is the
integration of procedural constructs with SQL.
• SQL is a nonprocedural language. When you issue a
SQL command, your command tells the database server
what to do. However, you cannot specify how to do it or
how often to do it.
• PL/SQL integrates control statements and conditional
statements with SQL. This gives you better control of
your SQL statements and their execution.

5 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Benefit 2: Modularized Program Development

The basic unit in a PL/SQL program is a block. All PL/SQL


programs consist of blocks. You can think of these blocks
as modules and you can “modularize” these blocks in a
sequence, or nest them in other blocks.

6 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Benefit 2: Modularized Program Development


(cont.)
Modularized program development has
the following advantages:
• You can group logically related
statements within blocks.
• You can nest blocks inside other
blocks to build powerful programs.
• You can share blocks with other
programmers to speed up
development time.

7 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Benefit 2: Modularized Program Development


(cont.)
Modularized program development has
the following advantages:
• You can break your application into
smaller modules. If you are designing
a complex application, PL/SQL allows
you to break down the application into
smaller, manageable, and logically
related modules.
• You can easily read, maintain, and
debug the programming statements.

8 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Benefit 3: Improved Performance

PL/SQL allows you to logically combine multiple SQL


statements as one unit or block. The application can send
the entire block to the database instead of sending the
SQL statements one at a time. This significantly reduces
the number of database calls (consider a database with
several million records).
SQL 1
SQL 2

SQL
IF...THEN
SQL
ELSE
SQL
END IF;
SQL
9 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Benefits of PL/SQL

Benefit 4: Integration With Oracle Tools

PL/SQL is integrated in Oracle tools, such as Oracle


Forms Developer, Oracle Report Builder, and Application
Express.

PL/SQL

SQL

10 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Benefit 5: Portability

PL/SQL is integrated in Oracle tools, such as Oracle


PL/SQL programs can run anywhere an Oracle server
runs, regardless of the operating system and the platform.

PL/SQL programs do not need to be tailored for different


operating systems and platforms.

11 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Benefit 5: Portability (cont.)

You can write portable program packages and create


libraries that can be reused on Oracle databases in
different environments. You can even anticipate those
differences and establish instructions to run a specific way
given a specific environment.

Linux HP Tru64 IBM z/OS Solaris

12 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Benefit 6: Exception Handling

An exception is an error that occurs in the database or in


a user’s program during runtime. Examples of errors
include: hardware or network failures, application logic
errors, data integrity errors, and so on.

You can prepare for errors by writing exception handling


code. Exception handling code tells your program what to
do in the event of an exception.

13 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Benefit 6: Exception Handling (cont.)

PL/SQL allows you to handle database and program


exceptions efficiently. You can define separate blocks for
dealing with exceptions.

If no data is found then …


If too many rows are found then…
If an invalid number is calculated then …

14 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

PL/SQL Compared to Other Languages

PL/SQL C Java
Requires Oracle database
Yes No No
or tool

Object-oriented Some features No Yes

Performance against an
Very efficient Less efficient Less efficient
Oracle database

Portable to different
Yes Somewhat Yes
operating systems

Ease of learning Relatively easy More difficult More difficult

15 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

PL/SQL in Oracle Products

Oracle Product PL/SQL


You can write PL/SQL code to manage application
data or to manage the Oracle database itself. For
example, you can write code for updating data
(DML), creating data (DDL), generating reports,
managing security, and so on.
Using the Web Application Toolkit, you can create
database-centric web applications written entirely
or partially in PL/SQL.
Using Forms Builder and Reports Developer,
Oracle’s client-side developer tools, you can build
database-centric web applications and reports
that include PL/SQL.

Using a Web browser you can develop web


applications that include PL/SQL.

16 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Terminology

Key terms used in this lesson included:


• Blocks
• Portability
• Exceptions

17 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Benefits of PL/SQL

Summary

In this lesson, you should have learned how to:


• List and explain the benefits of PL/SQL
• List differences between PL/SQL and other
programming languages
• Give examples of how to use PL/SQL in other Oracle
products

18 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Database Programming with
PL/SQL
Creating PL/SQL Blocks

1 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Objectives

This lesson covers the following objectives:


• Describe the structure of a PL/SQL block
• Identify the different types of PL/SQL blocks
• Identify PL/SQL programming environments
• Create and execute an anonymous PL/SQL block
• Output messages in PL/SQL

2 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Purpose

When you put something into a box you intuitively know


that the box has consistent properties. It has sides, a
bottom, and a top that can be opened and closed.

In PL/SQL, you put your programming instructions into


block structures that also have consistent properties.

3 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Purpose (cont.)

Here you will learn the structure of a PL/SQL block and


create one kind of block: an anonymous block.

After learning about the different environments into which


you can develop your PL/SQL programs, you will also
begin coding PL/SQL in the Application Express
development environment.

4 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

PL/SQL Block Structure

A PL/SQL block consists of three sections.


Section Description
The declarative section begins with the
Declarative
keyword DECLARE and ends when your
(optional)
executable section starts.
The executable section begins with the
keyword BEGIN and ends with END.
Executable Observe that END is terminated with a
(mandatory) semicolon. The executable section of a
PL/SQL block can include any number of
nested PL/SQL blocks.
Exception The exception section is nested within the
handling executable section. This section begins with
(optional) the keyword EXCEPTION.

5 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

PL/SQL Block Structure Sections

Section Description Inclusion


Contains declarations of all variables,
Declarative constants, cursors, and user-defined
Optional
(DECLARE) exceptions that are referenced in the
executable and exception sections.

Contains SQL statements to retrieve data


Executable
from the database and PL/SQL statements
(BEGIN … Mandatory
to manipulate data in the block. Must
END;)
contain at least one statement.

Exception Specifies the actions to perform when


(EXCEPTION) errors and abnormal conditions arise in the Optional
executable section.

6 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Introduction to PL/SQL

PL/SQL Extends SQL With Procedural Logic

DECLARE
v_new_letter_grade varchar2(1);
CURSOR c_enrollments IS
SELECT stu_id, final_numeric_grade FROM enrollments WHERE class_id=1;
BEGIN
FOR c1 in c_enrollments
LOOP
IF c1.final_numeric_grade BETWEEN 66 and 75 THEN v_new_letter_grade := 'A';
ELSIF c1.final_numeric_grade BETWEEN 56 AND 65 THEN v_new_letter_grade := 'B';
ELSIF c1.final_numeric_grade BETWEEN 46 AND 55 THEN v_new_letter_grade := 'C';
ELSIF c1.final_numeric_grade BETWEEN 36 AND 45 THEN v_new_letter_grade := 'D';
ELSE
v_new_letter_grade := 'F';
END IF;
UPDATE enrollments
SET final_letter_grade=v_new_letter_grade WHERE class_id=1
AND stu_id=c1.stu_id;
END LOOP;
COMMIT;
END;

13 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

The PL/SQL Compiler

Every program written in a high-level programming


language (C, Java, PL/SQL and so on) must be checked
and translated into binary code (ones and zeros) before it
can execute. The software that does this checking and
translation is called a compiler.

7 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

The PL/SQL Compiler (cont.)

The PL/SQL compiler executes automatically when


needed. It checks not only that every word is spelled
correctly, but also that any referenced database objects
(such as tables) exist, and that the user has the necessary
privileges to access them.

8 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Anonymous Blocks

Characteristics of anonymous blocks:


• Unnamed block
• Not stored in the database
• Declared inline at the point in an application where it
is executed
• Compiled each time the application is executed
• Passed to the PL/SQL engine for execution at run
time
• Cannot be invoked or called because it does not have
a name and does not exist after it is executed
9 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Creating PL/SQL Blocks

Anonymous Blocks (cont.)

[DECLARE]

BEGIN
--statements

[EXCEPTION]

END;

10 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Examples of Anonymous Blocks

No declaration or exception sections, execution only


BEGIN
DBMS_OUTPUT.PUT_LINE('PL/SQL is easy!');
END;

Declaration and execution sections, but no exception


section
DECLARE
v_date DATE := SYSDATE;
BEGIN
DBMS_OUTPUT.PUT_LINE(v_date);
END;

11 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Examples of Anonymous Blocks (cont.)

Declaration and exception sections


DECLARE
v_country_name VARCHAR2(40);
v_region_id NUMBER;
BEGIN
SELECT country_name, region_id
INTO v_country_name, v_region_id
FROM countries WHERE country_id='CA';
DBMS_OUTPUT.PUT_LINE ('The country name is: '
||v_country_name||' and is located in '
||v_region_id||'.') ;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE ('Your select statement retrieved
multiple rows. Consider using a cursor.');
END;

12 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Subprograms

Subprograms: PROCEDURE name


IS
• Are named PL/SQL blocks --variable declaration(s)
BEGIN
--statements
• Are named PL/SQL blocks
[EXCEPTION]
• Are stored in the database END;

• Can be invoked whenever you


FUNCTION name
want depending on your RETURN datatype
--variable declaration(s)
application IS
BEGIN
--statements
RETURN value;

[EXCEPTION]

END;

13 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Subprograms (cont.)

Subprograms:
• Can be declared as procedures or as functions
– Procedure: Performs an action
– Function: Computes and returns a value

14 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Examples of Subprograms

Procedure to print the current date


CREATE PROCEDURE print_date IS
v_date VARCHAR2(30);
BEGIN
SELECT TO_CHAR(SYSDATE,'Mon DD, YYYY')
INTO v_date
FROM DUAL;
DBMS_OUTPUT.PUT_LINE(v_date);
END;

Function to return the number of characters in a string


•CREATE
section
FUNCTION num_characters (p_string IN VARCHAR2)
RETURN INTEGER IS
v_num_characters INTEGER;
BEGIN
SELECT LENGTH(p_string) INTO v_num_characters
FROM DUAL;
RETURN v_num_characters;
END;

15 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Program Constructs

The following table outlines a variety of different


PL/SQL program constructs that use the basic PL/SQL
block. The constructs are available based on the
environment in which they are executed.

Tools constructs Database server constructs


Anonymous blocks Stored procedures or functions
Application procedures or functions Stored packages
Application packages Database triggers
Application triggers Object types
Object types
16 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Creating PL/SQL Blocks

PL/SQL Programming Environments

There are many tools that provide an environment for


developing PL/SQL. Oracle provides several tools you
can use. Some of the Oracle development tools are:

A component of Application
SQL*Workshop
Express.
SQL*Plus A command-line application.
A Graphical User Interface
SQL Developer (GUI) integrated development
environments (IDE).
JDeveloper A Windows-based application.
Application Express A web-browser application.

17 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Oracle Application Express

Oracle Application Express is a browser-based web


application environment that offers a SQL Workshop
component.

18 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Developing with SQL Workshop

When you log in to Oracle Application Express and


choose SQL Workshop, you can choose to use the
SQL Commands option to use the SQL command-line
editor, or you can choose the SQL Scripts option to
work within the Script Editor.

19 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

SQL Commands

You can use SQL


Commands to enter and
run a single SQL
statement or a single
PL/SQL block. A SQL
script can contain one
or more SQL statements
and/or PL/SQL blocks.
Use SQL Scripts to
enter and run multi-
statement scripts.

20 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Using DBMS_OUTPUT.PUT_LINE Example

Look at this simple PL/SQL block and its output. How can
you display the result?

21 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Using DBMS_OUTPUT.PUT_LINE

Let’s add a call to DBMS_OUTPUT.PUT_LINE. Now you


can see the result!

22 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Using DBMS_OUTPUT.PUT_LINE (cont.)

The DBMS_OUTPUT.PUT_LINE allows you to display


results so that you can check that your block is working
correctly. It allows you to display one character string
at a time, although this can be concatenated.
DECLARE
v_emp_count NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('PL/SQL is easy so far!');
SELECT COUNT(*) INTO v_emp_count FROM employees;
DBMS_OUTPUT.PUT_LINE('There are '||v_emp_count||'
rows in the employees table');
END;

23 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Terminology

Key terms used in this lesson included:


• Anonymous PL/SQL block
• Compiler
• Subprograms
• Procedures
• Functions

24 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.
Creating PL/SQL Blocks

Summary

In this lesson, you should have learned how to:


• Describe the structure of a PL/SQL block
• Identify the different types of PL/SQL blocks
• Identify PL/SQL programming environments
• Create and execute an anonymous PL/SQL block
• Output messages in PL/SQL

25 Copyright © 2013, Oracle and/or its affiliates. All rights


reserved.

Das könnte Ihnen auch gefallen