1. What is PL/SQL? PL/SQL storage and execution Different types of PL/SQL programs Call PL/SQL from different environments

Similar documents
Oracle10g PL/SQL Programming V1.0

Oracle Database: SQL and PL/SQL Fundamentals

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

Database Programming with PL/SQL: Learning Objectives

Oracle Database 10g: Program with PL/SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: Develop PL/SQL Program Units

Oracle Database: SQL and PL/SQL Fundamentals NEW

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

Oracle Database: Program with PL/SQL

Oracle Database: Program with PL/SQL

Oracle Database: Program with PL/SQL

Oracle Database: Program with PL/SQL

Oracle Database: Program with PL/SQL

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

Oracle SQL*PLUS (For , Fall 2006) Prepared by: Mr. Nkulu Nkasa

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

Oracle 10g PL/SQL Training

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

Oracle 11g PL/SQL training

Toad for Oracle 8.6 SQL Tuning

Oracle(PL/SQL) Training

PL/SQL MOCK TEST PL/SQL MOCK TEST I

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

STUDY OF PL/SQL BLOCK AIM: To Study about PL/SQL block.

Handling Exceptions. Schedule: Timing Topic 45 minutes Lecture 20 minutes Practice 65 minutes Total

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Oracle Forms Developer 10g: Build Internet Applications

2 SQL in iseries Navigator

MyOra 3.5. User Guide. SQL Tool for Oracle. Kris Murthy

Oracle Database 11g: Program with PL/SQL

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database 11g Express Edition PL/SQL and Database Administration Concepts -II

Training Guide. PL/SQL for Beginners. Workbook

SQL/PSM. Outline. Database Application Development Oracle PL/SQL. Why Stored Procedures? Stored Procedures PL/SQL. Embedded SQL Dynamic SQL

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

Oracle Database 10g: Introduction to SQL

PROCEDURES, FUNCTIONS AND PACKAGES

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Handling Exceptions. Copyright 2008, Oracle. All rights reserved.

An Introduction to PL/SQL. Mehdi Azarmi

ORACLE DATABASE 11G: COMPLETE

Handling Exceptions. Schedule: Timing Topic. 45 minutes Lecture 20 minutes Practice 65 minutes Total

SQL Basics for RPG Developers

Using ORACLE in the CSLab

Oracle Internal & Oracle Academy

3.GETTING STARTED WITH ORACLE8i

1 Stored Procedures in PL/SQL 2 PL/SQL. 2.1 Variables. 2.2 PL/SQL Program Blocks

PeopleSoft DDL & DDL Management

Oracle Database: Introduction to SQL

Developing SQL and PL/SQL with JDeveloper

Oracle Database 10g Express

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Darshan Institute of Engineering & Technology PL_SQL

Creating a Simple Macro

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

1 Triggers. 2 PL/SQL Triggers

Oracle Database 12c: Introduction to SQL Ed 1.1

Toad for Data Analysts, Tips n Tricks

Advanced SQL Injection in Oracle databases. Esteban Martínez Fayó

Basic Unix/Linux 1. Software Testing Interview Prep

Using SQL Developer. Copyright 2008, Oracle. All rights reserved.

Data Tool Platform SQL Development Tools

Introducing Oracle s SQL Developer

MyOra 4.5. User Guide. SQL Tool for Oracle. Kris Murthy

Writing Control Structures

14 Triggers / Embedded SQL

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Maintaining Stored Procedures in Database Application

COMS20700 DATABASES 13 PL/SQL. COMS20700 Databases Dr. Essam Ghadafi

Handling PL/SQL Errors

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Oracle For Beginners Page : 1

DBArtisan 8.5 Evaluation Guide. Published: October 2, 2007

<Insert Picture Here> Oracle SQL Developer 3.0: Overview and New Features

Oracle9i: Develop PL/SQL Program Units

T-SQL STANDARD ELEMENTS

PL/SQL Programming Workbook

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Setting up SQL Translation Framework OBE for Database 12cR1

Instant SQL Programming

Oracle SQL Developer for Database Developers. An Oracle White Paper June 2007

Data Integration and ETL with Oracle Warehouse Builder: Part 1

1 File Processing Systems

SQL SERVER DEVELOPER Available Features and Tools New Capabilities SQL Services Product Licensing Product Editions Will teach in class room

Oracle PL/SQL Programming

DataFlex Connectivity Kit For ODBC User's Guide. Version 2.2

2. Which of the following declarations is invalid? Mark for Review (1) Points

Running your first Linux Program

JD Edwards World. Database Audit Manager Release A9.3 E

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

Oracle. Brief Course Content This course can be done in modular form as per the detail below. ORA-1 Oracle Database 10g: SQL 4 Weeks 4000/-

Introduction to Triggers using SQL

Transcription:

What is PL/SQL? 1.1 1. What is PL/SQL? PL/SQL storage and execution Different types of PL/SQL programs Call PL/SQL from different environments SKILLBUILDERS

What is PL/SQL? 1.2 1.2 What is PL/SQL? PL/SQL is a procedural language for the ORACLE server and its clients Key concept: SQL and PL/SQL combine to provide the following features: SQL - data manipulation and transaction processing PL/SQL - third generation procedural language: variable definition, assignment, conditional processing, looping constructs, file I/O, subprograms, error handling, etc. What is PL/SQL? PL/SQL is a procedural language for the ORACLE server and its clients. PL/SQL stands for Procedural Language extensions to SQL. Programs stored on the client side are called anonymous blocks. Server side stored PL/SQL programs are called triggers, stored procedures, or stored functions. Client-side products such as Oracle Forms also support PL/SQL.

What is PL/SQL? 1.3 PL/SQL Capabilities & Restrictions Capabilities PL/SQL can call: DML statements standard SQL functions other PL/SQL programs 1.3 Restrictions PL/SQL programs cannot directly contain DDL statements PL/SQL Capabilities & Restrictions Capabilities All standard SQL functions except the DECODE function and the aggregate functions (SUM, COUNT, MAX, MIN, etc.) may be used in PL/SQL. The DECODE and aggregate functions can, however, be used in SQL statements within PL/SQL programs. Restrictions PL/SQL can indirectly contain DDL statements (ALTER, CREATE, etc.) via the use of native dynamic SQL. This topic is beyond the scope of this course.

What is PL/SQL? 1.4 1.4 Types of PL/SQL Programs Anonymous blocks (also called unnamed blocks) Triggers Stored Procedures Stored Functions Types of PL/SQL Programs Anonymous Blocks are called anonymous because they are not stored and named within the server. They are also sometimes referred to as unnamed blocks. However, they can be stored in an operating system file, not within the database. Anonymous blocks are usually executed under SQL*PLUS to: update table data produce reports build database objects Triggers are, usually, code associated with a table and are executed automatically when an appropriate SQL statement is issued against the table. There are also triggers which are associated with system events such as Startup and Shutdown or DDL such as CREATE and DROP statements. Other triggers can be created which are associated with user events such as Logon or Logoff. Triggers are compiled and stored permanently in the Oracle server. Triggers are used to: Enforce complex business and/or integrity rules Audit modifications to a table Derive column values Maintain mirror tables continues

What is PL/SQL? 1.5 Types of programs continued Stored Procedures and Stored Functions are callable subprograms which are also compiled and stored permanently in the Oracle server. The main difference between the two is that stored functions must return a value to the caller. Otherwise, they are basically the same. There are many benefits in using stored procedures and functions including: increased performance shared code lowered maintenance cost portability speed (compiled) security

What is PL/SQL? 1.6 Executing PL/SQL 1.6 Client Server DECLARE x NUMBER; BEGIN INSERT INTO... SELECT x INTO x... IF x > 10 THEN... END; EXECUTE PROCEDURE... Server DB Files Procedures Functions Triggers Network Client with PL/SQL Call procedure Retrieve results Server DB Files Server Memory Network Client w/o PL/SQL INSERT INTO t1 INSERT INTO t2 INSERT INTO t3 SELECT * FROM t1 Key concept: A single call to the database invokes the procedure or function. Before stored procedures and functions, you had to make a separate call to the database for each SQL statement. The result was high network traffic and overhead.

What is PL/SQL? 1.7 1.7 PL/SQL Subprograms PL/SQL program units are subprograms called by: Client (Visual Basic, Oracle Forms) Server (triggers, scheduled jobs) 3GL s (C, C++, COBOL) SQL*Plus PL/SQL Subprograms PL/SQL program units are subprograms called by a client application, the server, a 3GL or a host development environment such as SQL*Plus. Anonymous block usage is not the norm. You will use anonymous blocks for quick and dirty tasks in development. Production level PL/SQL program units will be callable subprograms, not SQL*Plus scripts!

What is PL/SQL? 1.8 Storage PL/SQL Storage & Execution On the server Triggers Stored procedures Stored functions On the client Anonymous blocks Execution All execute on the server 1.8 PL/SQL Storage & Execution All types of PL/SQL programs except for anonymous blocks are stored on the server. Execution of any PL/SQL program, regardless of its type, is done on the server. In other words, PL/SQL is a server side language. How are PL/SQL programs executed? With the exception of triggers, other PL/SQL program types are executed by calling them in code. Triggers, on the other hand, are automatically executed by Oracle when the appropriate triggering event occurs.

What is PL/SQL? 1.9 1.9 PL/SQL Program Structure PL/SQL is a block-structured language A PL/SQL block consists of 3 parts Declarative Executable Exception Handling DECLARE variables; constants; cursors; BEGIN BEGIN PL/SQL and and SQL SQL DML DML statements; EXCEPTION exception handlers; END; END; PL/SQL Program Structure PL/SQL is a block-structured language. Each PL/SQL program consists of 3 sections, or parts. The declarative section contains all declarations for variables, constants and cursors. The executable section contains the main body of PL/SQL and SQL statements. Finally, the exception handling section is where all errors can be trapped and resolved. Syntax Each line of code in a PL/SQL program must end in a semicolon. In that way, PL/SQL is like SQL where every line must also end in a semicolon. SQL*Plus, on the other hand, terminates at the end of the physical line and as such does not use a statement terminator. If you wish to continue a SQL*Plus command onto the next physical line you need a dash as a continuation character.

What is PL/SQL? 1.10 10 Anonymous Block Example Copy a customer to the cust_history table 1.10 10 declare v_custno number := := 1 ; begin INSERT INTO cust_history SELECT * FROM customer WHERE cust_no = v_custno; end; / Anonymous Block Example Place the code for an anonymous block in a.sql script file. Note that we follow our anonymous block with a slash (/) on a line by itself. When we run the script from SQL*Plus, the anonymous block is read into the SQL*Plus buffer. The slash tells SQL*Plus to execute the contents of the SQL*Plus buffer. Note that the SQL*Plus buffer can hold one and only one SQL command or PL/SQL block. We have hard coded the customer number in the above example. You could use a SQL*Plus substitution variable in place of the hard coded customer number. It you do, this PL/SQL block must be executed from SQL*Plus. Only SQL*Plus understands SQL*Plus substitution variables! Coding Anonymous Blocks with the SQL*Plus buffer editor All normal SQL*Plus buffer editor commands work as they did for SQL commands. It is the same SQL*Plus buffer editor. But to end editing of an SQL statement we just entered a blank line. This worked because blank lines are not allowed in an SQL statement. But blank lines are allowed in a PL/SQL block for readability. Therefore SQL*Plus needs a different way of detecting that you want to end the buffer editor session. We tell SQL*Plus that we are done with the edit session by typing a period on a line by itself.

What is PL/SQL? 1.11 11 Anonymous Block Compile Errors 1.11 11 declare v_custno number := := 1 ; begin begin INSRET INTO INTO cust_history SELECT * FROM FROM customer WHERE WHERE cust_no = v_custno; end; end; / INSRET INTO INTO cust_history * ERROR ERROR at at line line 4: 4: ORA-06550: line line 4, 4, column 10: 10: PLS-00103: Encountered the the symbol "INTO" when when expecting one one of of the the following: := :=. ( @ % ; Anonymous Block Compile Errors Note the misleading error message. The problem is that INSERT is misspelled. While the error messages may not be as clear as we would like, notice the * is under the misspelled INSRET. When you execute an anonymous block, SQL*Plus sends it the the server to be compiled and executed. If the program compiles correctly, Oracle will execute it and return the results to SQL*Plus. If the program does not compile correctly, Oracle will return the compile errors to SQL*Plus. SQL*Plus will then display either the results or the errors to your screen. Later we will see that compile errors in triggers, stored procedures, and stored functions are not automatically displayed by SQL*Plus. You must specifically display them yourself.

What is PL/SQL? 1.12 12 PL/SQL Output to SQL*Plus PL/SQL may display output to SQL*Plus using the system procedure dbms_output.put_line Example 1.12 12 begin dbms_output.put_line('hello World') ; end end ; / PL/SQL Output to SQL*Plus PL/SQL may display output to SQL*Plus by using the Oracle supplied package procedure called DBMS_OUTPUT.PUT_LINE. This procedure displays text on the output device (console) in SQL*Plus. In order for the output from DBMS_OUTPUT.PUT_LINE to be seen, the SET SERVEROUTPUT on SQL*Plus command must be executed. You may want to consider putting this command in your LOGIN.SQL file as this setting reverts to the default (off) when the session ends.

What is PL/SQL? 1.13 13 Tools for PL/SQL Development SQL*Plus is the traditional tool Included with all Oracle database products Procedure Builder Sophisticated IDE GUI tool An extra cost product Sophisticated debugging features Breakpoints stepping through code variable examination, etc. 1.13 13 Tools for PL/SQL Development SQL*Plus is the traditional tool for PL/SQL development as it is included with all Oracle database products. You may choose to use a more robust development tool, like Procedure Builder, which provides a graphical development environment. This product has to be purchased separately but may be worth the extra cost as it provides many sophisticated debugging features and a better overall working environment for your development efforts. There are many third party tools also available (e.g. TOAD, www.toadsoft.com).

What is PL/SQL? 1.14 14 1.14 14 Working in SQL*Plus SET ECHO ON can be used to show compiled source on your screen Error messages will refer to a line and column number ECHO output will show line numbers Oracle Error messages manual is available to help Working in SQL*Plus Debugging your PL/SQL programs can be tedious if you are uncertain of where the error messages are pointing to. The simplest way to see the errors is to SET ECHO ON prior to the compile of your source. (@programname) You will have the program source echoed on your screen with line numbers. If there are any error messages, it will be simple to ascertain where the error occurred. You can also get the source line number from your USER_SOURCE library. You cannot just count lines in your original.sql file because the PL/SQL compiler does not count blank lines and some comment lines. It is inaccurate. Sometimes the messages are so clear you do not need to know the line number. Most often, messages are somewhat obscure and you can use all the help you can get!

What is PL/SQL? 1.15 15 PL/SQL HelloWorld Workshop PL/SQL Hello World 1.15 15 Workshop 1. No course is complete without the infamous Hello World! application. So let s create an anonymous PL/SQL block to print the string Hello World! to the screen. Code the PL/SQL block in a script, and test it by running the script. 2. Create an script called copy_to_history containing an anonymous PL/SQL block to copy a customer from the Customer table into the Cust_history table. Prompt the user for the customer number to be copied. 3. Verify that the copy worked.