9 Copyright © 2007, Oracle. All rights reserved. Creating Stored Procedures and Functions.

Slides:



Advertisements
Similar presentations
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
Advertisements

Creating Triggers.
3 Copyright © 2004, Oracle. All rights reserved. Creating Packages.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
Programming in Oracle with PL/SQL
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
9 Copyright © 2009, Oracle. All rights reserved. Introducing Stored Procedures and Functions.
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
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.
Distributed Database Applications COSC 5050 Week Three.
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.
Copyright  Oracle Corporation, All rights reserved. 3 Creating Procedures.
11 Copyright س Oracle Corporation, All rights reserved. ® Overview of PL/SQL.
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 Oracle11g : PL/SQL Programming Chapter 6 Functions.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to 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.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
Using SQL in PL/SQL ITEC 224 Database Programming.
Using Oracle-Supplied Packages. 2 home back first prev next last What Will I Learn? Describe two common uses for the DBMS_OUTPUT server-supplied package.
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.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
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.
Declaring PL/SQL Variables
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Creating Procedures. PL/SQL Program Construct Tools Constructs Anonymous Block Application procedures or functions Application packages Application Triggers.
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
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.
STORED PROCEDURE & STORED FUNCTION Politeknik Telkom 2012.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
ITEC 224 Database Programming
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Creating Stored Functions
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
Program with PL/SQL Lesson 4.
Creating Stored Procedures
Interacting with the Oracle Server
Creating Stored Procedures and Functions
Interacting with the Oracle Server
UNIT - V STORED PROCEDURE.
Introduction to PL/SQL
Handling Exceptions.
Database Management Systems 2
PL/SQL Scripting in Oracle:
PRACTICE OVERVIEW PL/SQL Part - 2.
Database Management Systems 2
Creating Noninput Items
PL/SQL Declaring Variables.
Creating Stored Procedures and Functions
Procedures Oracle & MySQL
Creating Stored Procedures and Functions
Presentation transcript:

9 Copyright © 2007, Oracle. All rights reserved. Creating Stored Procedures and Functions

Copyright © 2007, Oracle. All rights reserved Objectives After completing this lesson, you should be able to do the following: Differentiate between anonymous blocks and subprograms Create a simple procedure and invoke it from an anonymous block Create a simple function Create a simple function that accepts a parameter Differentiate between procedures and functions

Copyright © 2007, Oracle. All rights reserved Procedures and Functions Are named PL/SQL blocks Are called PL/SQL subprograms Have block structures similar to anonymous blocks: –Optional declarative section (without the DECLARE keyword) –Mandatory executable section –Optional section to handle exceptions

Copyright © 2007, Oracle. All rights reserved Differences Between Anonymous Blocks and Subprograms Anonymous BlocksSubprograms Unnamed PL/SQL blocksNamed PL/SQL blocks Compiled every timeCompiled only once Not stored in the databaseStored in the database Cannot be invoked by other applications Named and, therefore, can be invoked by other applications Do not return valuesSubprograms called functions must return values. Cannot take parametersCan take parameters

Copyright © 2007, Oracle. All rights reserved Procedure: Syntax CREATE [OR REPLACE] PROCEDURE procedure_name [(argument1 [mode1] datatype1, argument2 [mode2] datatype2,...)] IS|AS procedure_body;

Copyright © 2007, Oracle. All rights reserved Procedure: Example... CREATE TABLE dept AS SELECT * FROM departments; CREATE PROCEDURE add_dept IS v_dept_id dept.department_id%TYPE; v_dept_name dept.department_name%TYPE; BEGIN v_dept_id:=280; v_dept_name:='ST-Curriculum'; INSERT INTO dept(department_id,department_name) VALUES(v_dept_id,v_dept_name); DBMS_OUTPUT.PUT_LINE(' Inserted '|| SQL%ROWCOUNT ||' row '); END;

Copyright © 2007, Oracle. All rights reserved

Copyright © 2007, Oracle. All rights reserved Invoking the Procedure BEGIN add_dept; END; / SELECT department_id, department_name FROM dept WHERE department_id=280;

Copyright © 2007, Oracle. All rights reserved Function: Syntax C REATE [OR REPLACE] FUNCTION function_name [(argument1 [mode1] datatype1, argument2 [mode2] datatype2,...)] RETURN datatype IS|AS function_body;

Copyright © 2007, Oracle. All rights reserved Function: Example CREATE FUNCTION check_sal RETURN Boolean IS v_dept_id employees.department_id%TYPE; v_empno employees.employee_id%TYPE; v_sal employees.salary%TYPE; v_avg_sal employees.salary%TYPE; BEGIN v_empno:=205; SELECT salary,department_id INTO v_sal,v_dept_id FROM employees WHERE employee_id= v_empno; SELECT avg(salary) INTO v_avg_sal FROM employees WHERE department_id=v_dept_id; IF v_sal > v_avg_sal THEN RETURN TRUE; ELSE RETURN FALSE; END IF; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; END;

Copyright © 2007, Oracle. All rights reserved Invoking the Function BEGIN IF (check_sal IS NULL) THEN DBMS_OUTPUT.PUT_LINE('The function returned NULL due to exception'); ELSIF (check_sal) THEN DBMS_OUTPUT.PUT_LINE('Salary > average'); ELSE DBMS_OUTPUT.PUT_LINE('Salary < average'); END IF; END; /

Copyright © 2007, Oracle. All rights reserved Passing a Parameter to the Function DROP FUNCTION check_sal; CREATE FUNCTION check_sal(p_empno employees.employee_id%TYPE) RETURN Boolean IS v_dept_id employees.department_id%TYPE; v_sal employees.salary%TYPE; v_avg_sal employees.salary%TYPE; BEGIN SELECT salary,department_id INTO v_sal,v_dept_id FROM employees WHERE employee_id=p_empno; SELECT avg(salary) INTO v_avg_sal FROM employees WHERE department_id=v_dept_id; IF v_sal > v_avg_sal THEN RETURN TRUE; ELSE RETURN FALSE; END IF; EXCEPTION...

Copyright © 2007, Oracle. All rights reserved Invoking the Function with a Parameter BEGIN DBMS_OUTPUT.PUT_LINE('Checking for employee with id 205'); IF (check_sal(205) IS NULL) THEN DBMS_OUTPUT.PUT_LINE('The function returned NULL due to exception'); ELSIF (check_sal(205)) THEN DBMS_OUTPUT.PUT_LINE('Salary > average'); ELSE DBMS_OUTPUT.PUT_LINE('Salary < average'); END IF; DBMS_OUTPUT.PUT_LINE('Checking for employee with id 70'); IF (check_sal(70) IS NULL) THEN DBMS_OUTPUT.PUT_LINE('The function returned NULL due to exception'); ELSIF (check_sal(70)) THEN... END IF; END; /

Copyright © 2007, Oracle. All rights reserved Summary In this lesson, you should have learned how to: Create a simple procedure Invoke the procedure from an anonymous block Create a simple function Create a simple function that accepts parameters Invoke the function from an anonymous block

Copyright © 2007, Oracle. All rights reserved Practice 9: Overview This practice covers the following topics: Converting an existing anonymous block to a procedure Modifying the procedure to accept a parameter Writing an anonymous block to invoke the procedure

Copyright © 2007, Oracle. All rights reserved Practice 9: Overview