Copyright  Oracle Corporation, 1998. All rights reserved. 4 Creating Functions.

Slides:



Advertisements
Similar presentations
Chapter 9: Advanced SQL and PL/SQL Topics Guide to Oracle 10g.
Advertisements

1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
4 Copyright © 2004, Oracle. All rights reserved. Creating a Basic Form Module.
3 Copyright © 2004, Oracle. All rights reserved. Working in the Forms Developer Environment.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored 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.
8 Copyright © 2004, Oracle. All rights reserved. Creating LOVs and Editors.
Advanced Databases Advanced PL/SQL Programming: Procedure, Function and Package.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Oracle9 i JDeveloper for Database Developers and DBAs Brian Fry Principal Product Manager Oracle JDeveloper Oracle Corporation.
Copyright  Oracle Corporation, All rights reserved. 5 CMIS Powell Oracle Designer: Design Editor and Building the Database and Table API CMIS.
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.
Copyright Ó Oracle Corporation, All rights reserved. 22 Running a Form Builder Application.
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.
3 Copyright © 2004, Oracle. All rights reserved. Working in the Forms Developer Environment.
Database control Introduction. The Database control is a tool that used by the database administrator to control the database. To enter to Database control.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
4 Copyright © 2004, Oracle. All rights reserved. Creating a Basic Form Module.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
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.
Database Application Development using PL/SQL Programming.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
10 Copyright © 2004, Oracle. All rights reserved. Creating Noninput Items.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
3 Copyright © 2004, Oracle. All rights reserved. Working in the Forms Developer Environment.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
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 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.
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.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT 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.
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.
Copyright Ó Oracle Corporation, All rights reserved Debugging Triggers.
15 Copyright © 2004, Oracle. All rights reserved. Debugging 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.
8 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
19 Copyright © 2004, Oracle. All rights reserved. Coding PL/SQL Triggers.
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.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
E Copyright © 2006, Oracle. All rights reserved. Using SQL Developer.
13 Copyright © 2004, Oracle. All rights reserved. Migrating SQL Statements.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
C Copyright © 2009, Oracle. All rights reserved. Using SQL Developer.
3 A Guide to MySQL.
Creating Stored Functions
SQL and SQL*Plus Interaction
Creating Stored Procedures and Functions
PL/SQL Scripting in Oracle:
Database Management Systems 2
Creating Noninput Items
Prof. Arfaoui. COM390 Chapter 6
Presentation transcript:

Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions

4-2 Copyright  Oracle Corporation, All rights reserved. Objectives After completing this lesson, you should be able to do the following: Describe the uses of functions Create client-side and server-side functions Invoke a function Remove a function Differentiate between a procedure and a function After completing this lesson, you should be able to do the following: Describe the uses of functions Create client-side and server-side functions Invoke a function Remove a function Differentiate between a procedure and a function

4-3 Copyright  Oracle Corporation, All rights reserved. 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 database object, for repeated execution. A function can be called as part of an expression. 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 called as part of an expression.

4-4 Copyright  Oracle Corporation, All rights reserved. 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; The PL/SQL block must have at least one RETURN statement.

4-5 Copyright  Oracle Corporation, All rights reserved. Creating a Function SQL*Plus Code OracleProcedureBuilder Code Editor 1 SQL> START file.sql Save 2 Source code Execute p-code Compile Oracle

4-6 Copyright  Oracle Corporation, All rights reserved. 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.

4-7 Copyright  Oracle Corporation, All rights reserved. Creating a Stored Function Using SQL*Plus: Example SQL> CREATE OR REPLACE FUNCTION get_sal 2 (v_id INemp.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 INemp.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 /

4-8 Copyright  Oracle Corporation, All rights reserved. Creating a Function Using Procedure Builder Procedure Builder allows you to: Create a client-side function Create a server-side function Drag and drop functions between client and server Procedure Builder allows you to: Create a client-side function Create a server-side function Drag and drop functions between client and server

4-9 Copyright  Oracle Corporation, All rights reserved. Creating Functions Using Procedure Builder: Example Return the tax based on a specified value. FUNCTION tax (v_value IN NUMBER) RETURN NUMBER IS BEGIN RETURN (v_value *.08); END tax; FUNCTION tax (v_value IN NUMBER) RETURN NUMBER IS BEGIN RETURN (v_value *.08); END tax;

4-10 Copyright  Oracle Corporation, All rights reserved. Executing Functions Invoke a function as part of a PL/SQL expression. Create a host variable to hold the returned value. Execute the function. The host variable will be populated by the RETURN value. Invoke a function as part of a PL/SQL expression. Create a host variable to hold the returned value. Execute the function. The host variable will be populated by the RETURN value.

4-11 Copyright  Oracle Corporation, All rights reserved. Executing Functions in SQL*Plus: Example Calling environment GET_SAL function v_id 7934 RETURN v_salary START get_salary.sql SQL> START get_salary.sql Function created Function created. SQL> VARIABLE g_salary number SQL> EXECUTE :g_salary := get_sal(7934) PL/SQL procedure successfully completed. SQL> EXECUTE :g_salary := get_sal(7934) PL/SQL procedure successfully completed. SQL> PRINT g_salary G_SALARY SQL> PRINT g_salary G_SALARY

4-12 Copyright  Oracle Corporation, All rights reserved. Executing Functions in Procedure Builder: Example Display the tax based on a specified value. PL/SQL>.CREATE NUMBER x PRECISION 4 PL/SQL> :x := tax(1000); PL/SQL> TEXT_IO.PUT_LINE (TO_CHAR(:x));80 PL/SQL>.CREATE NUMBER x PRECISION 4 PL/SQL> :x := tax(1000); PL/SQL> TEXT_IO.PUT_LINE (TO_CHAR(:x));80 Calling environment TAX function v_value 1000 RETURN (computed value)

4-13 Copyright  Oracle Corporation, All rights reserved. Advantages of User-Defined Functions in SQL Expressions Extend SQL where activities are too complex, too awkward, or unavailable with SQL Can increase efficiency, by using them in the WHERE clause to filter data, as opposed to filtering the data in the applicationCan increase efficiency, by using them in the WHERE clause to filter data, as opposed to filtering the data in the application Can manipulate character strings Extend SQL where activities are too complex, too awkward, or unavailable with SQL Can increase efficiency, by using them in the WHERE clause to filter data, as opposed to filtering the data in the applicationCan increase efficiency, by using them in the WHERE clause to filter data, as opposed to filtering the data in the application Can manipulate character strings

4-14 Copyright  Oracle Corporation, All rights reserved. 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 clauses VALUES clauses of the INSERT command SET clause of the UPDATE command Select list of a SELECT command Condition of the WHERE and HAVING clauses CONNECT BY, START WITH, ORDER BY, and GROUP BY clauses VALUES clauses of the INSERT command SET clause of the UPDATE command

4-15 Copyright  Oracle Corporation, All rights reserved. Calling Functions from SQL Expressions: Restrictions A user-defined function must be a stored function. A user-defined function must be a ROW function, not a GROUP function. A user-defined function only takes IN parameters, not OUT, or IN OUT. Datatypes must be valid SQL datatypes, CHAR, DATE, or NUMBER. Datatypes cannot be PL/SQL types such as BOOLEAN, RECORD, or TABLE. A user-defined function must be a stored function. A user-defined function must be a ROW function, not a GROUP function. A user-defined function only takes IN parameters, not OUT, or IN OUT. Datatypes must be valid SQL datatypes, CHAR, DATE, or NUMBER. Datatypes cannot be PL/SQL types such as BOOLEAN, RECORD, or TABLE.

4-16 Copyright  Oracle Corporation, All rights reserved. Calling Functions from SQL Expressions: Restrictions INSERT, UPDATE, or DELETE commands are not allowed. Calls to subprograms that break the above restriction are not allowed. INSERT, UPDATE, or DELETE commands are not allowed. Calls to subprograms that break the above restriction are not allowed.

4-17 Copyright  Oracle Corporation, All rights reserved. Removing Functions Using SQL*Plus: Drop a server-side function. Using Procedure Builder: – Drop a server-side function. – Delete a client-side function. Using SQL*Plus: Drop a server-side function. Using Procedure Builder: – Drop a server-side function. – Delete a client-side function.

4-18 Copyright  Oracle Corporation, All rights reserved. Removing a Server-Side Function Using SQL*Plus Syntax Example Using SQL*Plus Syntax Example DROP FUNCTION function_name SQL> DROP FUNCTION get_sal; Function dropped. SQL> DROP FUNCTION get_sal; Function dropped.

4-19 Copyright  Oracle Corporation, All rights reserved. Removing Server-Side Functions Using Procedure Builder 1.Connect to the database. 2.Expand the Database Objects node. 3.Expand the schema of the owner of the function. 4.Expand the Stored Program Units node. 5.Click the function you want to drop. 6.Click Delete in the Object Navigator. 7. Click Yes to confirm. Using Procedure Builder 1.Connect to the database. 2.Expand the Database Objects node. 3.Expand the schema of the owner of the function. 4.Expand the Stored Program Units node. 5.Click the function you want to drop. 6.Click Delete in the Object Navigator. 7. Click Yes to confirm.

4-20 Copyright  Oracle Corporation, All rights reserved. Removing a Client-Side Function Using Procedure Builder 1. Expand the Program Units node. 2. Click the function you want to remove. 3. Click Delete in the Object Navigator. 4.Click Yes to confirm. Using Procedure Builder 1. Expand the Program Units node. 2. Click the function you want to remove. 3. Click Delete in the Object Navigator. 4.Click Yes to confirm.

4-21 Copyright  Oracle Corporation, All rights reserved. Procedure or Function? Procedure(DECLARE)BEGINEXCEPTIONEND; IN argument OUT argument IN OUT argument CallingEnvironment CallingEnvironmentFunction(DECLARE)BEGINEXCEPTIONEND; IN argument

4-22 Copyright  Oracle Corporation, All rights reserved. Comparing Procedures and Functions Procedure Execute as a PL/SQL statement No RETURN datatype Can return none, one or many values Function Invoke as part of an expression Must contain a RETURN datatype Must return a single value

4-23 Copyright  Oracle Corporation, All rights reserved. Benefits of Stored Procedures and Functions Improved performance Improved maintenance Improved data security and integrity Improved performance Improved maintenance Improved data security and integrity

4-24 Copyright  Oracle Corporation, All rights reserved. Summary A function is a named PL/SQL block that must return a value. A function is invoked as part of an expression. 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 invoked as part of an expression. A stored function can be called in SQL statements.

4-25 Copyright  Oracle Corporation, All rights reserved. Practice Overview Creating stored functions Invoking a stored function from a SQL statement Invoking a stored function from a stored procedure Creating stored functions Invoking a stored function from a SQL statement Invoking a stored function from a stored procedure

4-26 Copyright  Oracle Corporation, All rights reserved.