Oracle Stored Procedures and Functions

Slides:



Advertisements
Similar presentations
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
Advertisements

A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Advanced SQL: Stored Procedures Instructor: Mohamed Eltabakh 1.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
Introduction to PL/SQL Chapter 9. Objectives Explain the need for PL/SQL Explain the benefits of PL/SQL Identify the different types of PL/SQL blocks.
Bordoloi and Bock PL/SQL : INTRODUCTION. Bordoloi and BockPL/SQL PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational.
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.
Advanced Databases Advanced PL/SQL Programming: Procedure, Function and Package.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Copyright © SUPINFO. All rights reserved Procedures and functions.
Stored Procedures Functions Packages
 Allows sophisticated data processing  Build complex business logic in a modular fashion  Use over and over  Execute rapidly – little network traffic.
 Allows sophisticated data processing  Build complex business logic in a modular fashion  Use over and over  Execute rapidly – little network traffic.
Copyright  Oracle Corporation, All rights reserved. 3 Creating Procedures.
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.
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.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
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.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Passing Parameters. 2 home back first prev next last What Will I Learn? List the types of parameter modes Create a procedure that passes parameters Identify.
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.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Creating Procedures. PL/SQL Program Construct Tools Constructs Anonymous Block Application procedures or functions Application packages Application Triggers.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
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 Procedures and Functions Pemrograman Basis Data MI2183.
What Are Subprograms? Subprograms are named PL/SQL blocks that can take parameters and be invoked. Subprograms allow decomposition of a program into logical.
STORED PROCEDURE & STORED FUNCTION Politeknik Telkom 2012.
1 Chapter 5: Advanced PL/SQL Programming. 2 Anonymous PL/SQL Programs Write code in text editor, execute it in SQL*Plus Code can be stored as text in.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Creating Stored Functions
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
PL/pgSQL
Creating Stored Procedures and Functions
Difference between Oracle PL/SQL and MySQL
PL/SQL.
UNIT - V STORED PROCEDURE.
Database Application Development
Introduction to PL/SQL
User-Defined Functions
PL/SQL Scripting in Oracle:
PRACTICE OVERVIEW PL/SQL Part - 2.
Database Management Systems 2
Handling Exceptions.
Procedures Organized by Farrokh Alemi, Ph.D. Narrated by Yara Alemi
PRACTICE OVERVIEW PL/SQL Part - 1.
Chapter 8 Advanced SQL.
PL/SQL Declaring Variables.
Chapter 8 Advanced SQL Pearson Education © 2009.
Oracle Stored Procedures and Functions
Procedures Oracle & MySQL
Prof. Arfaoui. COM390 Chapter 6
SQL Stored Procedures and Functions Presented by: Dr. Samir Tartir
Database Application Development
Presentation transcript:

Oracle Stored Procedures and Functions

What can you do with PL/SQL? Allows sophisticated data processing Build complex business logic in a modular fashion Use over and over Execute rapidly – little network traffic Stored procedures Functions Triggers

Stored Procedures Defined set of actions written using PL/SQL When called, the procedure performs actions Can be called directly from other blocks Two parts Procedure specification or header Procedure body

PROCEDURES 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 OR REPLACE PROCEDURE name [(parameter[, parameter, ...])] AS [local declarations] BEGIN executable statements [EXCEPTION exception handlers] END [name];

PROCEDURES A procedure may have 0 to many parameters. Every procedure has two parts: The header portion, which comes before AS (sometimes you will see IS—they are interchangeable), keyword (this contains the procedure name and the parameter list), The body, which is everything after the IS keyword. The word REPLACE is optional. When the word REPLACE is not used in the header of the procedure, in order to change the code in the procedure, it must be dropped first and then re-created. Bordoloi and Bock

Example: Procedure CREATE OR REPLACE PROCEDURE hello IS BEGIN Greetings VARCHAR(20); BEGIN Greetings:= 'Hello World'; DBMS_OUTPUT.PUT_LINE(greetings); END hello;

Example: Procedure CREATE OR REPLACE PROCEDURE Discount AS CURSOR c_group_discount IS SELECT distinct s.course_no, c.description FROM section s,enrollment e,course c WHERE s.section_id = e.section_id AND c.course_no = s.course_no GROUP BY s.course_no, c.description, e.section_id, s.section_id HAVING COUNT(*) >=8; BEGIN FOR r_group_discount IN c_group_discount LOOP UPDATE course SET cost = cost * .95 WHERE course_no = r_group_discount.course_no; DBMS_OUTPUT.PUT_LINE ('A 5% discount has been given to'|| r_group_discount.course_no||' '|| r_group_discount.description ); END LOOP; END; Bordoloi and Bock

Calling a Procedure In order to execute a procedure in SQL*Plus use the following syntax: EXECUTE Procedure_name set serveroutput on size 4000 EXECUTE hello; Another way to execute it is from another PL/SQL block: BEGIN hello; END; To display output

Arguments increase_salary_find_tax( increase_percent IN NUMBER:=7, A value can be passed to a procedure when it is called (input) Must specify datatype Example (not actually a procedure): increase_salary_find_tax( increase_percent IN NUMBER:=7, sal IN OUT NUMBER, tax OUT NUMBER) IN means the procedure can read an incoming value from that parameter when the procedure is called OUT means the procedure can use that parameter to send a value back to what called it increase_percent has a default value of 7

Arguments Following is a procedure with arguments: CREATE OR REPLACE PROCEDURE increase (oldprice NUMBER, percent NUMBER := 5, newprice OUT NUMBER) IS BEGIN newprice:=oldprice+oldprice*percent/100; END increase;

Calling a Procedure with Arguments DECLARE price_increase NUMBER(6,2) := 20; newp NUMBER(6,2) := 0; BEGIN DBMS_OUTPUT.PUT_LINE('Current price: '|| price_increase); increase(oldprice=>price_increase,newprice=>newp); DBMS_OUTPUT.PUT_LINE ('Price after increase: '|| newp); END; We should see a new price of 21

PARAMETERS Parameters are the means to pass values to and from the calling environment to the server. These are the values that will be processed or returned via the execution of the procedure. There are three types of parameters: IN, OUT, and IN OUT. Modes specify whether the parameter passed is read in or a receptacle for what comes out.

Types of Parameters Calling Environ-ment Procedure IN Argument OUT Argument IN OUT Argument DECLARE …. BEGIN EXCEPTION END; Calling Environ-ment

FORMAL AND ACTUAL PARAMETERS Formal parameters are the names specified within parentheses as part of the header of a module. Actual parameters are the values— expressions specified within parentheses as a parameter list—when a call is made to the module. The formal parameter and the related actual parameter must be of the same or compatible data types.

MATCHING ACTUAL AND FORMAL PARAMETERS Two methods can be used to match actual and formal parameters: positional notation and named notation. Positional notation is simply association by position: The order of the parameters used when executing the procedure matches the order in the procedure’s header exactly. Named notation is explicit association using the symbol => Syntax: formal_parameter_name => argument_value In named notation, the order does not matter. If you mix notation, list positional notation before named notation.

MATCHING ACTUAL AND FORMAL PARAMETERS Bordoloi and Bock

FUNCTIONS Functions are a type of stored code and are very similar to procedures. The significant difference is that a function is a PL/SQL block that returns a single value. Functions can accept one, many, or no parameters, but a function must have a return clause in the executable section of the function. The datatype of the return value must be declared in the header of the function. A function is not a stand-alone executable in the way that a procedure is: It must be used in some context. You can think of it as a sentence fragment. A function has output that needs to be assigned to a variable, or it can be used in a SELECT statement.

FUNCTIONS The syntax for creating a function is as follows: CREATE [OR REPLACE] FUNCTION function_name (parameter list) RETURN datatype IS BEGIN <body> RETURN (return_value); END; Bordoloi and Bock

FUNCTIONS The function does not necessarily have to have any parameters, but it must have a RETURN value declared in the header, and it must return values for all the varying possible execution streams. The RETURN statement does not have to appear as the last line of the main execution section, and there may be more than one RETURN statement (there should be a RETURN statement for each exception). A function may have IN, OUT, or IN OUT parameters. but you rarely see anything except IN parameters. Bordoloi and Bock

Example CREATE OR REPLACE FUNCTION show_description (i_course_no number) RETURN varchar2 AS v_description varchar2(50); BEGIN SELECT description INTO v_description FROM course WHERE course_no = i_course_no; RETURN v_description; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN('The Course is not in the database'); WHEN OTHERS RETURN('Error in running show_description'); END; Bordoloi and Bock

Making Use Of Functions In a anonymous block SET SERVEROUTPUT ON DECLARE v_description VARCHAR2(50); BEGIN v_description := show_description(&sv_cnumber); DBMS_OUTPUT.PUT_LINE(v_description); END; In a SQL statement SELECT course_no, show_description(course_no) FROM course; Bordoloi and Bock

Example The IF-THEN construct allows for error checking CREATE OR REPLACE FUNCTION discount (amount NUMBER, percent NUMBER:=5) RETURN NUMBER IS BEGIN IF (amount>=0) THEN return (amount*percent/100); ELSE return(0); END IF; END discount; The IF-THEN construct allows for error checking

Example : Calling the Function DECLARE current_amt NUMBER:=100; incorrect_amt NUMBER:=-5; BEGIN DBMS_OUTPUT.PUT_LINE(' Order and Discount'); DBMS_OUTPUT.PUT_LINE(current_amt || ' '|| discount(current_amt)); DBMS_OUTPUT.PUT_LINE(incorrect_amt||' '||discount(incorrect_amt)); END;

Example Write a PL/SQL function that accepts price and onhand values, checks to be sure they are both greater than 0 and multiplies them together. If they are less than 0 return 0. CREATE OR REPLACE FUNCTION total_amount (price NUMBER, onhand NUMBER) RETURN NUMBER IS BEGIN IF (price>0 AND onhand>0) THEN return (price*onhand); ELSE return(0); END IF; END total_amount;