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.

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

PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
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.
3 Copyright © 2004, Oracle. All rights reserved. Creating Packages.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
Creating Packages. 2 home back first prev next last What Will I Learn? Describe the reasons for using a package Describe the two components of a package:
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.
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.
Module 2: Using Transact-SQL Querying Tools. Overview SQL Query Analyzer Using the Object Browser Tool in SQL Query Analyzer Using Templates in SQL Query.
PL/SQL and the Table API. Benefits of Server-Side Code Speedy Pizza MENU NAPOLITAINE PIZZA Reduced network traffic Maintainability Data integrity Triggers.
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.
Benefits of PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –List and explain the benefits of PL/SQL –List.
Oracle9 i JDeveloper for Database Developers and DBAs Brian Fry Principal Product Manager Oracle JDeveloper Oracle Corporation.
Copyright © SUPINFO. All rights reserved Procedures and functions.
Stored Procedures Functions Packages
Copyright  Oracle Corporation, All rights reserved. 3 Creating Procedures.
11 Copyright س Oracle Corporation, All rights reserved. ® Overview of PL/SQL.
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.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
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.
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.
PRACTICE OVERVIEW PL/SQL Part Examine this package specification and body: Which statement about the V_TOTAL_BUDGET variable is true? A. It must.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
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.
Database Application Development using PL/SQL Programming.
1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
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.
55 Creating Data Dictionary Views and Standard Packages.
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.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
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.
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.
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.
4 Copyright © 2004, Oracle. All rights reserved. Advanced Interface Methods.
E Copyright © 2006, Oracle. All rights reserved. Using SQL Developer.
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 and Functions
PL/SQL.
Creating Packages.
UNIT - V STORED PROCEDURE.
DBM 405 Innovative Education- -snaptutorial.com
PL/SQL Scripting in Oracle:
PRACTICE OVERVIEW PL/SQL Part - 2.
Database Management Systems 2
Database Management Systems 2
Producing Triggers Schedule: Timing Topic 40 minutes Lecture
PRACTICE OVERVIEW PL/SQL Part - 1.
MATERI PL/SQL Procedures Functions Packages Database Triggers
Prof. Arfaoui. COM390 Chapter 6
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

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 a value. A function can be stored in the database, as a database object, for repeated execution. A function can be stored in the database, as a database object, for repeated execution. A function can be called as part of an expression. A function can be called as part of an expression. A function is a named PL/SQL block that returns a value. A function is a named PL/SQL block that returns a value. A function can be stored in the database, as a database object, for repeated execution. A function can be stored in the database, as a database object, for repeated execution. A function can be called as part of an expression. A function can be called as part of an expression.

Syntax for Creating Functions CREATE [OR REPLACE] FUNCTION function_name (argument1 [mode1] datatype1, argument2 [mode2] datatype2,... RETURN datatype IS|AS PL/SQL Block; CREATE [OR REPLACE] FUNCTION function_name (argument1 [mode1] datatype1, argument2 [mode2] datatype2,... RETURN datatype IS|AS PL/SQL Block;

Creating a Stored Function Using SQL*Plus  1.Enter the text of the CREATE FUNCTION statement in a system editor or word processor and save it as a script file (.sql extension).  2.From SQL*Plus, run the script file to compile the source code into p- code and store both in the database.  1.Enter the text of the CREATE FUNCTION statement in a system editor or word processor and save it as a script file (.sql extension).  2.From SQL*Plus, run the script file to compile the source code into p- code and store both in the database. 3.Invoke the function from an Oracle Server environment to determine whether it executes without error.

Creating a Stored Function Using SQL*Plus: Example SQL> CREATE OR REPLACE FUNCTION get_sal 2 (v_id IN emp.empno%TYPE) 3 RETURN NUMBER 4 IS 5 v_salaryemp.sal%TYPE :=0; 6 BEGIN 7 SELECT sal 8 INTO v_salary 9 FROM emp 10 WHERE empno = v_id; 11 RETURN (v_salary); 12 END get_sal; 13 / SQL> CREATE OR REPLACE FUNCTION get_sal 2 (v_id IN emp.empno%TYPE) 3 RETURN NUMBER 4 IS 5 v_salaryemp.sal%TYPE :=0; 6 BEGIN 7 SELECT sal 8 INTO v_salary 9 FROM emp 10 WHERE empno = v_id; 11 RETURN (v_salary); 12 END get_sal; 13 /

Executing Functions Invoke a function as part of a PL/SQL expression. Invoke a function as part of a PL/SQL expression. Create a host variable to hold the returned value. Create a host variable to hold the returned value. Execute the function. The host variable will be populated by the RETURN value. Execute the function. The host variable will be populated by the RETURN value. Invoke a function as part of a PL/SQL expression. Invoke a function as part of a PL/SQL expression. Create a host variable to hold the returned value. Create a host variable to hold the returned value. Execute the function. The host variable will be populated by the RETURN value. Execute the function. The host variable will be populated by the RETURN value.

Advantages of User-Defined Functions in SQL Expressions Extend SQL where activities are too complex, too awkward, or unavailable with SQL Extend SQL where activities are too complex, too awkward, or unavailable with SQL Query efficiency: functions used in the WHERE clause can filter data Query efficiency: functions used in the WHERE clause can filter data Manipulate character strings Manipulate character strings Provide parallel query execution Provide parallel query execution Extend SQL where activities are too complex, too awkward, or unavailable with SQL Extend SQL where activities are too complex, too awkward, or unavailable with SQL Query efficiency: functions used in the WHERE clause can filter data Query efficiency: functions used in the WHERE clause can filter data Manipulate character strings Manipulate character strings Provide parallel query execution Provide parallel query execution

Removing Functions Using SQL*Plus: Using SQL*Plus: Drop a server-side function. Using Procedure Builder: Using Procedure Builder: Drop a server-side function.Drop a server-side function. Drop a client-side function.Drop a client-side function. Using SQL*Plus: Using SQL*Plus: Drop a server-side function. Using Procedure Builder: Using Procedure Builder: Drop a server-side function.Drop a server-side function. Drop a client-side function.Drop a client-side function.

Removing a Server-Side Function  Using SQL*Plus Syntax Syntax Example Example  Using SQL*Plus Syntax Syntax Example Example SQL> DROP FUNCTION get_salary; Function dropped. SQL> DROP FUNCTION get_salary; Function dropped. DROP FUNCTION function_name

Procedure or Function? Procedure(DECLARE)BEGINEXCEPTIONEND; IN argument OUT argument IN OUT argument CallingEnvironment CallingEnvironmentFunction(DECLARE)BEGINEXCEPTIONEND; IN argument

Comparing Procedures and Functions Procedure Execute as a PL/SQL statement No RETURN datatype Can return one or more values Function Invoke as part of an expression Must contain a RETURN datatype Must return a value

Benefits of Stored Procedures and Functions Improved performance Improved performance Improved maintenance Improved maintenance Improved data security and integrity Improved data security and integrity Improved performance Improved performance Improved maintenance Improved maintenance Improved data security and integrity Improved data security and integrity

Creating Packages

Overview of Packages Group logically related PL/SQL types, items, and subprograms Group logically related PL/SQL types, items, and subprograms Consist of two parts: Consist of two parts: SpecificationSpecification BodyBody Cannot be called, parameterized, or nested Cannot be called, parameterized, or nested Allow Oracle8 to read multiple objects into memory at once Allow Oracle8 to read multiple objects into memory at once Group logically related PL/SQL types, items, and subprograms Group logically related PL/SQL types, items, and subprograms Consist of two parts: Consist of two parts: SpecificationSpecification BodyBody Cannot be called, parameterized, or nested Cannot be called, parameterized, or nested Allow Oracle8 to read multiple objects into memory at once Allow Oracle8 to read multiple objects into memory at once

Advantages of Packages Modularity Modularity Easier application design Easier application design Information hiding Information hiding Added functionality Added functionality Better performance Better performance Overloading Overloading Modularity Modularity Easier application design Easier application design Information hiding Information hiding Added functionality Added functionality Better performance Better performance Overloading Overloading

Developing a Package Procedure A declaration Procedure B definition Packagespecification Packagebody Procedure A definition 5

Developing a Package Procedure A declaration Procedure B definition Packagespecification Packagebody Procedure A definition 5

Developing a Package Text file Source code p-code Edit Store in database Compile 1 2 System Editor ProcedureBuilder Invoke

Developing a Package Saving the text of the CREATE PACKAGE statement in two different text files facilitates later modifications to the package. Saving the text of the CREATE PACKAGE statement in two different text files facilitates later modifications to the package. A package specification can exist without a package body, but a package body cannot exist without a package specification. A package specification can exist without a package body, but a package body cannot exist without a package specification. If you have incorporated a standalone procedure into a package, you should drop your standalone procedure. If you have incorporated a standalone procedure into a package, you should drop your standalone procedure. Saving the text of the CREATE PACKAGE statement in two different text files facilitates later modifications to the package. Saving the text of the CREATE PACKAGE statement in two different text files facilitates later modifications to the package. A package specification can exist without a package body, but a package body cannot exist without a package specification. A package specification can exist without a package body, but a package body cannot exist without a package specification. If you have incorporated a standalone procedure into a package, you should drop your standalone procedure. If you have incorporated a standalone procedure into a package, you should drop your standalone procedure.

CREATE [OR REPLACE] PACKAGE package_name IS | AS public type and item declarations subprogram specifications END package_name; CREATE [OR REPLACE] PACKAGE package_name IS | AS public type and item declarations subprogram specifications END package_name; Creating the Package Specification SyntaxSyntax

COMM_PACKAGE package G_COMM RESET_COMM procedure declaration Packagespecification 1 2 Declaring Public Constructs

Creating a Package Specification: Example SQL>CREATE OR REPLACE PACKAGE comm_package IS 2 g_comm NUMBER := 10; --initialized to 10 3 PROCEDURE reset_comm 4 (v_comm INNUMBER); 5 END comm_package; 6 /

Declaring a Global Variable or a Public Procedure SQL>EXECUTE comm_package.g_comm := 5 SQL>EXECUTE comm_package.reset_comm(8)

Creating the Package Body SyntaxSyntax CREATE [OR REPLACE] PACKAGE BODY package_name IS | AS private type and item declarations subprogram bodies END package_name; CREATE [OR REPLACE] PACKAGE BODY package_name IS | AS private type and item declarations subprogram bodies END package_name;

G_COMM RESET_COMM procedure declaration VALIDATE_COMM function definition Packagespecification Packagebody RESET_COMM procedure definition COMM_PACKAGE package Public and Private Constructs

Creating a Package Body: Example SQL>CREATE OR REPLACE PACKAGE BODY comm_package IS 2 FUNCTION validate_comm 3 (v_comm IN NUMBER) RETURN BOOLEAN 4 IS 5 v_max_comm NUMBER; 6 BEGIN 7 SELECT MAX(comm) 8 INTO v_max_comm 9 FROM emp; 10 IF v_comm > v_max_comm THEN RETURN(FALSE); 11 ELSE RETURN(TRUE); 12 END IF; 13 END validate_comm; 14 END comm_package; 15 /

SQL>PROCEDURE reset_comm 2 (v_comm IN NUMBER) 3 IS 4 v_validBOOLEAN; 5 BEGIN 6 v_valid := validate_comm(v_comm); 7 IF v_valid = TRUE THEN 8 g_comm := v_comm; 9 ELSE 10 RAISE_APPLICATION_ERROR 11(-20210,'Invalid commission'); 12 END IF; 13 END reset_comm; 14 END comm_package; 15 / Creating a Package Body: Example

Invoking Package Constructs  Example 1: Invoke a function from a procedure within the same package. CREATE OR REPLACE PACKAGE BODY comm_package IS... PROCEDURE reset_comm(v_comm IN NUMBER) IS v_validBOOLEAN; BEGIN v_valid := validate_comm(v_comm); IF v_valid = TRUE THEN g_comm := v_comm; ELSE RAISE_APPLICATION_ERROR (-20210, 'Invalid comm'); END IF; END reset_comm; END comm_package;

 Example 2: Invoke a package procedure from SQL*Plus.  Example 3: Invoke a package procedure in a different schema.  Example 4: Invoke a package procedure in a remote database.  Example 2: Invoke a package procedure from SQL*Plus.  Example 3: Invoke a package procedure in a different schema.  Example 4: Invoke a package procedure in a remote database. Invoking Package Constructs SQL>EXECUTE comm_package.reset_comm(1500); SQL>EXECUTE scott.comm_package.reset_comm(1500); SQL>EXECUTE (1500);

Referencing a Global Variable Within the Package Example 1 CREATE OR REPLACE PACKAGE BODY comm_package IS... PROCEDURE reset_comm(v_comm IN NUMBER) IS v_validBOOLEAN; BEGIN v_valid := validate_comm(v_comm); IF v_valid = TRUE THEN g_comm := v_comm; ELSE RAISE_APPLICATION_ERROR (-20210,'Invalid comm'); END IF; END reset_comm; END comm_package;

DROP PACKAGE package_name Removing Packages To remove the package specification and the body: To remove the package body: DROP PACKAGE BODY package_name

SummarySummary A function is a named PL/SQL block that must return a value. A function is a named PL/SQL block that must return a value. A function is invoked as part of an expression. A function is invoked as part of an expression. A stored function can be called in SQL statements. A stored function can be called in SQL statements. A function is a named PL/SQL block that must return a value. A function is a named PL/SQL block that must return a value. A function is invoked as part of an expression. A function is invoked as part of an expression. A stored function can be called in SQL statements. A stored function can be called in SQL statements.

SummarySummary Improve organization, management, security, and performance. Improve organization, management, security, and performance. Group related procedures and functions together. Group related procedures and functions together. Change a package body without affecting a package specification. Change a package body without affecting a package specification. Grant security access to the entire package. Grant security access to the entire package. Improve organization, management, security, and performance. Improve organization, management, security, and performance. Group related procedures and functions together. Group related procedures and functions together. Change a package body without affecting a package specification. Change a package body without affecting a package specification. Grant security access to the entire package. Grant security access to the entire package.