1 copyright © 2009, oracle. all rights reserved. introducción a pl/sql

20
1 Copyright © 2009, Oracle. All rights reserved. Introducción a PL/SQL

Upload: sarah-dorsey

Post on 14-Dec-2015

218 views

Category:

Documents


4 download

TRANSCRIPT

1Copyright © 2009, Oracle. All rights reserved.

Introducción a PL/SQL

Copyright © 2009, Oracle. All rights reserved.1 - 2

Objetivos

Despues de completar esta lección, usted deberá ser capaz de hacer lo siguiente:

• Explicar la necesidad de PL/SQL

• Explicar los beneficios de PL/SQL

• Identificar los diferentes tipo de bloques de PL/SQL

• Mensajes de salida de PL/PLSQL

Copyright © 2009, Oracle. All rights reserved.1 - 3

Agenda

• Comprendiendo los beneficios y estructura de PL/SQL structure of PL/SQL

• Examinar bloques PL/SQL

• Generando mensajes de salida in PL/SQL

Copyright © 2009, Oracle. All rights reserved.1 - 4

Acerca det PL/SQL

PL/SQL:

• Soporte para “Procedural Language extension to SQL”

• Es para Oracle un estandar de acceso a datos para base de datos relacionales

• Integra perfectamente construcción de procedimientos con SQL

Copyright © 2009, Oracle. All rights reserved.1 - 5

Acerca de PL/SQL

PL/SQL:

• Proporciona una estructura de bloque de unidades de codigo ejecutable. El mantenimiento de código es facil con una estructura bien definida.

• 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

SQ

L

PL

/SQ

L

PL/SQL block

procedural

Procedural statementexecutor

SQL statement executor

Oracle Server

PL/SQL Engine

Copyright © 2009, Oracle. All rights reserved.1 - 7

Benefits of PL/SQL

• Integración de construcciones de procedimientos con SQL.

• Mejora el rendimiento

SQLIF...THEN

SQLELSE

SQLEND IF;SQL

SQL 1

SQL 2…

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

Benefits of PL/SQL

• Programa de desarrollo modularizado

• Integración con las herramientas de Oracle

• Portabilidad

• Manejo de excepciones

Copyright © 2009, Oracle. All rights reserved.1 - 10

PL/SQL Block Structure

• DECLARE (opcional)– Variables, constantes, cursores, excepciones definidas por

el usuario.

• BEGIN (obligatorio)– Sentencias SQL – Setencias PL/SQL

• EXCEPTION (optional)– Acciones que se llevarán a

cabo al producirse algún error.

• END; (obligatorio)

Copyright © 2009, Oracle. All rights reserved.1 - 12

Agenda

• Comprendiendo los beneficios y estructura de PL/SQL

• Examinando bloques PL/SQL

• Generando mensajes de salida de PL/SQL

Copyright © 2009, Oracle. All rights reserved.1 - 13

Tipo de Bloque

Procedimientos Funciones Anonimos

PROCEDURE nameIS

BEGIN --statements

[EXCEPTION]

END;

FUNCTION nameRETURN datatypeISBEGIN --statements RETURN value;[EXCEPTION]

END;

[DECLARE]

BEGIN --statements

[EXCEPTION]

END;

Copyright © 2009, Oracle. All rights reserved.1 - 15

Construcción de Programas

Application triggers

Application packages

Application proceduresor functions

Anonymous blocks

Tools Constructs

Object types

Database triggers

Stored packages

Stored procedures orfunctions

Anonymous blocks

Database ServerConstructs

Object types

Copyright © 2009, Oracle. All rights reserved.1 - 17

Examinando un Bloque anonimo

Un bloque anonimo en el area de trabajo de SQL Developer:

Copyright © 2009, Oracle. All rights reserved.1 - 18

Executiando un bloque Anonimo

Click en el botón de Run Script para ejecutar el bloque anonimo: Run Script (or F5)

Copyright © 2009, Oracle. All rights reserved.1 - 19

Agenda

• Comprendiendo los beneficios y estructura de PL/SQL

• Examinar bloques PL/SQL

• Generando mensajes de salida en PL/SQL

Copyright © 2009, Oracle. All rights reserved.1 - 20

Habilitando la salida de bloques PL/SQL Block

1. Para habilitar la salida en SQL Developer, ejecute los siguiente comandos antes de ejecutar el bloque PL/SQL:

2. Use un paquete predefinido en Oracle:– 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