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.

Slides:



Advertisements
Similar presentations
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Advertisements

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.
Chapter 9: Advanced SQL and PL/SQL Topics Guide to Oracle 10g.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
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.
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.
SAGE Computing Services Customised Oracle Training Workshops and Consulting Are you making the most of PL/SQL? Hints and tricks and things you may have.
Copyright © SUPINFO. All rights reserved Procedures and functions.
Copyright  Oracle Corporation, All rights reserved. 3 Creating Procedures.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
Interacting With The Oracle Server. Objectives After completing this lesson, you should be able to do the following: Write a successful SELECT statement.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
Cursor. Cursors Operations Cursors adalah suatu nama area yang berisi hasil dari suatu statement SQL. 1. OPEN 2. FETCH 3. CLOSE.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
9 Copyright © 2007, Oracle. All rights reserved. Managing Data and Concurrency.
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.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
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.
8 Copyright © 2005, Oracle. All rights reserved. Managing Data.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
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.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Using Functions in SQL Statements. 2 home back first prev next last What Will I Learn? List the advantages of user-defined functions in SQL statements.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
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.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
Creating Functions. Overview of Stored Functions A function is a named PL/SQL block that returns a value. A function is a named PL/SQL block that returns.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
STORED PROCEDURE & STORED FUNCTION Politeknik Telkom 2012.
9 Copyright © 2007, Oracle. All rights reserved. Creating Stored Procedures and Functions.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
4 Copyright © 2009, Oracle. All rights reserved. Working with Packages.
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.
Creating Stored Procedures
Creating Database Triggers
Interacting with the Oracle Server
Creating Stored Procedures and Functions
Interacting with the Oracle Server
UNIT - V STORED PROCEDURE.
Handling Exceptions.
Database Management Systems 2
PROC SQL, Overview.
PRACTICE OVERVIEW PL/SQL Part - 2.
Database Management Systems 2
Creating Noninput Items
Using CASE Value expression
PRACTICE OVERVIEW PL/SQL Part - 1.
Prof. Arfaoui. COM390 Chapter 6
IST 318 Database Administration
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

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 in the database as a schema object for repeated execution A function is called as part of an expression

V 12 NE - Oracle 2006 Syntax for Creating Functions The PL/SQL block must have at least one RETURN statement CREATE [OR REPLACE] FUNCTION function_name [(parameter1 [mode1] datatype1, parameter2 [mode2] datatype2,...)] RETURN datatype IS | AS PL/SQL Block;

V 12 NE - Oracle 2006 CREATE OR REPLACE FUNCTION get_sal (p_id IN employees.employee_id%TYPE) RETURN NUMBER IS v_salary employees.salary%TYPE; BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = p_id; RETURN v_salary; END get_sal; / Example :

V 12 NE - Oracle 2006 Executing Functions Invoke a function as part of a PL/SQL expression Create a variable to hold the returned value Execute the function. The variable will be populated by the value returned through a RETURN statement

V 12 NE - Oracle 2006 Locations to Call User-Defined Functions Select list of a SELECT command Condition of the WHERE and HAVING clauses CONNECT BY, START WITH, ORDER BY, and GROUP BY clause VALUES clause of the INSERT command SET clause of the UPDATE command

V 12 NE - Oracle 2006 Invoking Functions in SQL Expressions : Example CREATE OR REPLACE FUNCTION tax(p_value IN NUMBER) RETURN NUMBER IS BEGIN RETURN(p_value * 0.08); END tax; / SELECT employee_id, last_name, salary, tax(salary) FROM employees WHERE tax(salary) > (SELECT MAX (tax(salary)) FROM employees WHERE department_id = 30) ORDER BY tax(salary) DESC;

V 12 NE - Oracle 2006 Restrictions on Calling Functions from SQL Expressions Be a stored function Accept only IN parameters Accept only valid SQL data types, not PL/SQL specific types, as parameters Return data types that are valid SQL data types, not PL/SQL specific types Functions called from SQL expressions cannot contain DML statements Functions called from UPDATE/DELETE statements on a table T cannot contain DML, query on the same table T Functions called from SQL statements cannot contain statements that end the transactions Calls to subprograms that break the previous restriction are not allowed in the function

V 12 NE - Oracle 2006 Restrictions on Calling from SQL CREATE OR REPLACE FUNCTION dml_call_sql(p_sal NUMBER) RETURN NUMBER IS BEGIN INSERT INTO employees(employee_id, last_name, , hire_date, job_id, salary) VALUES(1,’employee RETURN (p_sal + 100); END; / UPDATE employees SET salary = dml_call_sql(2000) WHERE employee_id = 170; UPDATE employees SET salary = dml_call_sql(2000) * ERROR at line 1: ORA : table PLSQL EMPLOYEES is mutating, trigger/function may not see it ORA : at “PLSQLDML_CALL_SQL”,line 4

V 12 NE - Oracle 2006 Example DECLARE v_tax number; v_sal emp.salary%TYPE; BEGIN SELECT salary into v_sal FROM emp WHERE id = 97001; v_tax := Tax(v_sal); EXCEPTION WHEN NO_DATA_FOUND THEN v_tax := 0; END; FUNCTION Tax(v_value IN NUMBER) RETURN NUMBER IS BEGIN RETURN(v_value *.07); END Tax;

V 12 NE - Oracle 2006 Removing Functions Drop a stored function Syntax: DROP FUNCTION function_name Example: DROP FUNCTION get_sal; All the privileges granted on a function are revoked when the function is dropped The CREATE OR REPLACE syntax is equivalent to dropping a function and recreating it. Privileges granted on the function remain the same when this syntax is used

V 12 NE - Oracle 2006 Procedure or Function? Calling environment Procedure IN parameter OUT parameter IN OUT parameter (DECLARE) BEGIN EXCEPTION END; Function IN parameter (DECLARE) BEGIN EXCEPTION END; Calling environment

V 12 NE - Oracle 2006 Comparing Procedures and Functions ProceduresFunctions Execute as a PL/SQL statement Invoke as part of an expression Do not contain RETURN clause in the header Must contain a RETURN clause in the header Can return none, one, or many values Must return a single value Can contain a RETURN statement Must contain at least one RETURN statement

V 12 NE - Oracle 2006