PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
From last class… PL/SQL –Triggers –Block structure –Anonymous vs. named blocks –Functions and procedures –Using PL/SQL from SQL*Plus.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
PL/SQL Agenda: Basic PL/SQL block structure
Programming in Oracle with PL/SQL
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
PL/SQL Introduction Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
Dr. James Dullea, CSC8490 Introduction to PL/SQLSlide 1 of 36 7From Prof. Dullea CSC8490 Introduction to PL/SQL Module 01-9 Revised: June 12, 2005 Dr.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Distributed Database Applications COSC 5050 Week Three.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Cursor and Exception Handling By Nidhi Bhatnagar.
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
CSIT 313 DB PROGRAMMING EXCEPTION HANDLING. In PL/SQL, an error condition is called an exception. An exception can be either –internally defined (by the.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Distributed Database Applications COSC 5050 Week Five.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
Database Application Development using PL/SQL Programming.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
Oracle 8i Exception Handling. General Syntax DECLARE --- BEGIN --- EXCEPTION WHEN exception_name1 THEN -Error handling statements WHEN exception_name2.
1 PL\SQL Dev Templates. 2 TEMPLATE DEFINITION Whenever you create a new program unit, its initial contents are based upon a template which contains pre-defined.
Stored Procedures. Definition a stored procedure is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database.
1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.
DATABASE PROGRAMS CS 260 Database Systems. Overview  Introduction  Anonymous blocks  Oracle’s PL/SQL language basics  Conditions and loops  Cursors.
Chapter 18: Exception Handling1 Chapter Eighteen Exception Handling Objective: – Define exceptions – List types of exception handlers – Trap errors – Exception.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
A procedure is a module performing one or more actions; it does not need to return any values. The syntax for creating a procedure is as follows: CREATE.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
ADVANCED FEATURES Of PL/SQL. * PROCEDURAL LANGUAGE FEATURE * OBJECT ORIENTED FEATURES * EXCEPTION HANDLING * INCREASED SECUTIRY * PACKAGES.
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
STORED PROCEDURE & STORED FUNCTION Politeknik Telkom 2012.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
Oracle 数据库应用 -- PL/SQL 进阶 (3) & Oracle DBA 2016/5/ /5/10.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Creating Stored Functions
Chapter 2 Anonymous Block
Introduction to PL/SQL Programing
Difference between Oracle PL/SQL and MySQL
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Database Programming PL SQL.
Oracle Stored Procedures and Functions
Advanced PL/SQL Programing
Handling Exceptions.
Handling Exceptions.
Database Management Systems 2
Handling Exceptions.
PL/SQL week10.
PL/SQL Declaring Variables.
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

PL/SQL Oracle's Database Programming Language

Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on: Was it successful or not?!?!?! SQL> Execute Record_Sale( 'Dick Cheney', 'Cezanne', 'kindergarten', '8', 3550, :vReturn); PL/SQL procedure successfully completed. SQL> Execute Record_Sale( 'Dick Cheney', 'Cezanne', 'kindergarten', '8', 3550, :vReturn); Entered stored procedure Looking up CustomerID Looking up ArtistID Finding Work record Finding Transaction record Testing to see if a Transaction record was found No valid Transaction record exists. Transaction not completed. PL/SQL procedure successfully completed.

PL/SQL anonymous code block declare begin exception (optional) end; (note ;)

Hello World! declare /* note common Oracle variable name style: v prefix means local variable tx suffix means text */ v_message_tx varchar2(50) := 'Hello World'; begin /* to see this, serveroutput must be on */ dbms_output.put_line( v_message_tx ); end; sql> get HelloWorld.sql... sql> / Hello World

Simple Stored Procedure -- in the file sp_hello.sql: create or replace procedure sp_hello is v_message_tx varchar2(50):='Hello World'; begin dbms_output.put_line(v_message_tx); end; sp_hello 6 / Procedure created. SQL> begin 2 sp_hello; 3 end; 4 / Hello World PL/SQL procedure successfully completed.

Function (returns a value) CREATE OR REPLACE FUNCTION f_area_nr( i_width_nr NUMBER, i_ht_nr NUMBER ) return NUMBER is -- Function header to compute area -- i:IN parameters, nr:NUMBER v_rtn_nr NUMBER; -- Declaration of local variable -- v:local variable, nr:NUMBER begin v_rtn_nr := i_width_nr * i_ht_nr; return v_rtn_nr; end; SQL> list 1 begin 2 dbms_output.put_line( f_area_nr(3,4)); 3* end; SQL> / 12 PL/SQL procedure successfully completed.

Dealing with compilation errors 1 CREATE OR REPLACE FUNCTION f_area_nr( i_width_nr NUMBER, 2 i_ht_nr NUMBER ) 3 return NUMBER is 4 declare 5 v_rtn_nr NUMBER; //n.b. this function is in error 6 begin 7 v_rtn_nr := i_width_nr * i_ht_nr; 8 return v_rtn_nr; 9* end; SQL> / Warning: Function created with compilation errors. SQL> show errors Errors for FUNCTION F_AREA_NR: LINE/COL ERROR /5 PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following: begin function package pragma procedure subtype type use form current cursor external language The symbol "begin" was substituted for "DECLARE" to continue.

IN and OUT parameters in procedures SQL> list 1 declare 2 v_hr_nr NUMBER; 3 v_mn_nr NUMBER; 4 begin 5 sp_hourMin(sysdate, v_hr_nr, v_mn_nr); 6 dbms_output.put_line('hour: ' || v_hr_nr || ' min: ' || v_mn_nr); 7* end; SQL> / hour: 17 min: 46 PL/SQL procedure successfully completed. create or replace procedure sp_hourMin (i_date_dt DATE, -- Input parameter o_hour_nr OUT NUMBER, -- Return parameter o_minute_nr OUT NUMBER) is -- Return parameter begin o_hour_nr := to_NUMBER(to_char(i_date_dt, 'hh24')); o_minute_nr := to_NUMBER(to_char(i_date_dt, 'mi')); end ;

What did that procedure do? SQL> SELECT TEXT FROM USER_SOURCE WHERE NAME = 'SP_HOURMIN'; TEXT procedure sp_hourMin (i_date_dt DATE, o_hour_nr OUT NUMBER, o_minute_nr OUT NUMBER) is begin o_hour_nr := to_NUMBER(to_char(i_date_dt, 'hh24')); o_minute_nr := to_NUMBER(to_char(i_date_dt, 'mi')); end; 6 rows selected.

What was the name of that routine? SQL> column object_type format A12; SQL> column object_name format A12; SQL> select object_type, object_name, status 2 from user_objects 3 where object_type in ('FUNCTION', 'PROCEDURE'); OBJECT_TYPE OBJECT_NAME STATUS FUNCTION DECLARE INVALID FUNCTION FINDAREA INVALID FUNCTION F_AREA_NR VALID PROCEDURE SP_HELLO VALID PROCEDURE SP_HOURMIN VALID

IF statements if condition then elsif condition then else end if; IF recCount = 0 THEN DBMS_OUTPUT.PUT_LINE ('Customer Does Not Exist In Database -- No Action Taken'); RETURN; END IF;

Loops loop... exit when condition... end loop; FOR Trans_record in TransactionCursor LOOP IF( Trans_Record.SalesPrice is null ) THEN v_TransactionFound := TRUE; DBMS_OUTPUT.PUT_LINE( 'Updating Transaction' ); UPDATE Transaction SET SalesPrice = v_Price, CustomerID = v_CustomerID, PurchaseDate = SYSDATE WHERE CURRENT OF TransactionCursor; END IF; EXIT WHEN v_TransactionFound; END LOOP;

Exceptions Typical Exception Block: EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE( 'No data found' ); v_Return := 'Exception: No data found'; ROLLBACK; WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE( 'Too many rows found' ); v_Return := 'Exception: Too many rows found'; ROLLBACK; WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE( 'Exception occurred' ); DBMS_OUTPUT.PUT_LINE( 'Error code: ' || SQLCODE); DBMS_OUTPUT.PUT_LINE( SQLERRM ); v_Return := ( 'Exception: ' || SQLERRM ); ROLLBACK;

Cursors CURSOR TransactionCursor IS SELECT SalesPrice FROM Transaction WHERE WorkID = v_WorkID FOR UPDATE OF SalesPrice, CustomerID, PurchaseDate;... FOR Trans_record in TransactionCursor LOOP IF( Trans_Record.SalesPrice is null ) THEN v_TransactionFound := TRUE; DBMS_OUTPUT.PUT_LINE( 'Updating Transaction' ); UPDATE Transaction SET SalesPrice = v_Price, CustomerID = v_CustomerID, PurchaseDate = SYSDATE WHERE CURRENT OF TransactionCursor; END IF; EXIT WHEN v_TransactionFound; END LOOP;