Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.

Slides:



Advertisements
Similar presentations
L/O/G/O Creating Packages. Objectives After completing this lesson, you should be able to do the following: –Describe packages and list their components.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
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.
Creating Triggers.
3 Copyright © 2004, Oracle. All rights reserved. Creating Packages.
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.
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.
Copyright © SUPINFO. All rights reserved Procedures and functions.
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.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
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.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
PRACTICE OVERVIEW PL/SQL Part Examine this package specification and body: Which statement about the V_TOTAL_BUDGET variable is true? A. It must.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/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.
10 Copyright © 2004, Oracle. All rights reserved. Creating Noninput Items.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
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.
CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
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 
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.
SQL.. AN OVERVIEW lecture3 1. Overview of SQL 2  Query: allow questions to be asked of the data and display only the information required. It can include.
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
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.
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.
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.
SQL Triggers, Functions & Stored Procedures Programming Operations.
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.
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.
Creating Stored Functions
Creating Stored Procedures
Creating Database Triggers
Creating Stored Procedures and Functions
Interacting with the Oracle Server
Creating Packages.
UNIT - V STORED PROCEDURE.
Database Management Systems 2
PRACTICE OVERVIEW PL/SQL Part - 2.
Database Management Systems 2
SQL .. An overview lecture3.
Creating Noninput Items
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
PRACTICE OVERVIEW PL/SQL Part - 1.
Prof. Arfaoui. COM390 Chapter 9
Prof. Arfaoui. COM390 Chapter 6
IST 318 Database Administration
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

Lecture 8 Creating Stored Functions

Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of Function  System Defined Function  User Defined Function  Creating User Defined functions  Syntax  Sample Program  Invoke a function  Syntax  Remove a function  Syntax  Differentiate between a procedure and a function  View function in Data Dictionary

Functions What is Function  Is a named PL/SQL block that returns a value  Can be stored in the database as a schema object for repeated execution  Is called as part of an expression or is used to provide a parameter value Types of Function System Defined Function User Defined Function System define Function “Functions that are defined by system known as system defined function” Note System defined function can’t be changed. SUM, AVG, MIN, MAX, COUNT User Defined Function “Functions that are defined by user through coding known as User define function” Note User defined function can be changed

Functions Advantages of FunctionsModularity Functions provide modularity; that is, they let you break a program down into manageable, well-defined modules.Reusability Function is a named PL/SQL Program it can be reused. Maintainability. A Function can be used with confidence in any number of applications. If its definition changes, only the subprogram is affected. This simplifies maintenance.

Functions Declaring Function Syntax CREATE [OR REPLACE] FUNCTION function_name [(parameter1 [mode1] datatype1,...)] RETURN datatype IS|AS [local_variable_declarations; …] BEGIN -- actions; RETURN expression; END [function_name];Note   CREATE FUNCTION create or replace a function.  [OR REPLACE] option indicates that if the function exists it will be dropped and replaced with the new version created by the statement  RETURN datatype is the type of data that function return size is not mentioned.  BEGIN, END specifies the execution block.  Function must have one RETURN clause in Header and one RETURN clause in body  Only use IN Parameter Mode in functions as functions are designed to return a single value. PL/SQL Block Function Header

Stored Function: Example

How Function Executes func.sql 12 3 EditLoad Create (compile and store) Execute Use SHOW ERRORS for compilation errors 4

Ways to Execute Functions Using Bind Variable To Execute Function

Ways to Execute Functions Using Variables to Obtain Result

Ways to Execute Functions Use in a SQL statement (subject to restrictions) Use Local Variable to Obtain Result

Locations to Call User-Defined Functions  User-defined functions act like built-in single-row functions and can be used in  The SELECT list or clause of a query  Conditional expressions of the WHERE and HAVING clauses  The CONNECT BY, START WITH, ORDER BY, and GROUP BY clauses of a query  The VALUES clause of the INSERT statement  The SET clause of the UPDATE statement

Function in SQL Expressions

Limitations of calling Functions from SQL Expression  User-defined functions that are callable from SQL expressions must  Be stored in the database  Accept only IN parameters with valid SQL data types, not PL/SQL-specific types  Return valid SQL data types, not PL/SQL-specific types  When calling functions in SQL statements:  You must own the function or have the EXECUTE privilege

Restrictions on Calling Functions from SQL: Example CREATE OR REPLACE FUNCTION dml_call_sql(sal NUMBER) RETURN NUMBER IS BEGIN INSERT INTO employees(employee_id, last_name, , hire_date, job_id, salary) VALUES(1, 'Frost', SYSDATE, 'SA_MAN', sal); RETURN (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-04091: table PLSQL.EMPLOYEES is mutating, trigger/function may not see it ORA-06512: at "PLSQL.DML_CALL_SQL", line 4

Controlling Side Effects When Calling Functions from SQL Expressions  Functions called from:  A SELECT statement cannot contain DML statements  An UPDATE or DELETE statement on a table T cannot query or contain DML on the same table T  SQL statements cannot end transactions (that is, cannot execute COMMIT or ROLLBACK operations)  Note: Calls to subprograms that break these restrictions are also not allowed in the function.

Removing Functions  Removing a stored function:  You can drop a stored function by using the following syntax:  Example:  All the privileges that are granted on a function are revoked when the function is dropped.  The CREATE OR REPLACE syntax is equivalent to dropping a function and re-creating it. Privileges granted on the function remain the same when this syntax is used. DROP FUNCTION function_name DROP FUNCTION get_sal;

Viewing Functions in the Data Dictionary  Information for PL/SQL functions is stored in the following Oracle data dictionary views:  You can view source code in the USER_SOURCE table for subprograms that you own, or the ALL_SOURCE table for functions owned by others who have granted you the EXECUTE privilege.  You can view the names of functions by using USER_OBJECTS. SELECT text FROM user_source WHERE type = 'FUNCTION' ORDER BY line; SELECT object_name FROM user_objects WHERE object_type = 'FUNCTION';

Procedures Versus Functions Procedures Execute as a PL/SQL statement Do not contain RETURN clause in the header Can return values (if any) in output parameters Can contain a RETURN statement without a value Functions Invoke as part of an expression Must contain a RETURN clause in the header Must return a single value Must contain at least one RETURN statement

Summary   What is Function?  Types of Function  System Defined Function  User Defined Function  Creating User Defined functions  Syntax  Sample Program  Invoke a function  Syntax  Remove a function  Syntax  Differentiate between a procedure and a function  View function in Data Dictionary