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.

Slides:



Advertisements
Similar presentations
Virtual training week 4 structured query language (SQL)
Advertisements

Chapter 4B: More Advanced PL/SQL Programming
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.
Creating Triggers.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
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.
Copyright © SUPINFO. All rights reserved Procedures and functions.
Oracle Database Administration Lecture 2 SQL language.
Interacting With The Oracle Server. Objectives After completing this lesson, you should be able to do the following: Write a successful SELECT statement.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
9 Copyright © 2007, Oracle. All rights reserved. Managing Data and Concurrency.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
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.
8 Copyright © 2005, Oracle. All rights reserved. Managing Data.
Handling Exceptions. 2 home back first prev next last What Will I Learn? Describe several advantages of including exception handling code in PL/SQL Describe.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Creating DDL and Database Event Triggers. 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers.
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.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
INSERT Statement. 2 home back first prev next last What Will I Learn? Give examples of why it is important to be able to alter the data in a database.
ITEC 224 Database Programming PL/SQL Lab Cursors.
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.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Cursor FOR Loops. 2 home back first prev next last What Will I Learn? List and explain the benefits of using cursor FOR loops Create PL/SQL code to declare.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
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.
implicit and an explicit cursor
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 SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.
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 Guide to SQL, Eighth Edition Chapter Six Updating Data.
Ch 5. Introducing More Database Objects. Database Objects Table (ch2) View (ch3) Stored Procedure Trigger Function User-defined types.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
Installation Oracle 11g Express 2 double click the "setup" button to install the Oracle.
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.
4 Copyright © 2009, Oracle. All rights reserved. Working with Packages.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
Creating Stored Functions
Creating Database Triggers
Interacting with the Oracle Server
ATS Application Programming: Java Programming
Interacting with the Oracle Server
Manipulating Data.
Using the Set Operators
Database Management Systems 2
Using the Set Operators
Database Management Systems 2
Manipulating Data.
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Using Subqueries to Solve Queries
Prof. Arfaoui. COM390 Chapter 6
IST 318 Database Administration
Using Subqueries to Solve Queries
Using the Set Operators
Presentation transcript:

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 List where user-defined functions can be called from within an SQL statement Describe the restrictions on calling functions from SQL statements

3 home back first prev next last Why Learn It? In this lesson, you learn how to use functions within SQL statements. If the SQL statement processes many rows in a table, the function will execute once for each row processed by the SQL statement. For example, we could calculate the tax to be paid by every employee using just one function.

4 home back first prev next last What is a User-Defined Function? A user-defined function is a function which is created by the PL/SQL programmer. GET_DEPT_NAME and CALCULATE_TAX are examples of user-defined functions, whereas UPPER, LOWER, and LPAD are examples of system defined functions automatically provided by Oracle. Most system functions such as UPPER, LOWER, and LPAD are stored in a package named SYS.STANDARD. Packages will be covered in a later section. These system functions are often called built-in functions.

5 home back first prev next last Advantages of Functions in SQL Statements In the WHERE clause of a SELECT statement, functions can increase efficiency by eliminating unwanted rows before the data is sent to the application. –For example in a school administrative system, we want to fetch only those students whose last names are stored entirely in uppercase. This could be a small minority of the table’s rows. –We code: SELECT * FROM students WHERE student_name = UPPER(student_name); –Without the UPPER function, we would have to fetch all the student rows, transmit them across the network, and eliminate the unwanted ones within the application.

6 home back first prev next last Advantages of Functions in SQL Statements Can manipulate data values –For example, for an end-of-semester social event, we want (just for fun!) to print out the name of every teacher with the characters reversed, so “Mary Jones” becomes “senoJ yraM”. We can create a user-defined function called REVERSE_NAME which does this, then code: SELECT name, reverse_name(name) FROM teachers;

7 home back first prev next last Advantages of Functions in SQL Statements User-defined functions in particular can extend SQL where activities are too complex, too awkward, or unavailable with regular SQL –For example, we want to calculate how long an employee has been working for our business, rounded to a whole number of months. We could create a user- defined function called HOW_MANY_MONTHS to do this. Then, the application programmer can code: SELECT employee_id, how_many_months(hire_date) FROM employees;

8 home back first prev next last Function in SQL Expressions: Example

9 home back first prev next last Where in a SQL Statement Can User-Defined Functions be Used? User-defined functions act just like built-in single- row functions such as UPPER, LOWER and LPAD. They can be used in: –The SELECT column-list of a query –Conditional expressions in the WHERE and HAVING clauses –The ORDER BY and GROUP BY clauses of a query –The VALUES clause of the INSERT statement –The SET clause of the UPDATE statement In short, they can be used ANYWHERE that you have a value or expression!

10 home back first prev next last Where in a SQL Statement Can User-Defined Functions be Used? This example shows the user-defined function TAX being used in four places within a single SQL statement. SELECT employee_id, tax(salary) FROM employees WHERE tax(salary) > (SELECT MAX(tax(salary)) FROM employees WHERE department_id = 20) ORDER BY tax(salary) DESC;

11 home back first prev next last Restrictions on Using Functions in SQL Statements To use a user-defined function within a SQL statement, the function must conform to the rules and restrictions of the SQL language. The function can accept only valid SQL datatypes as IN parameters, and must RETURN a valid SQL datatype. –PL/SQL-specific types such as BOOLEAN and %ROWTYPE are not allowed –SQL size limits must not be exceeded (PL/SQL allows a VARCHAR2 variable to be up to 32KB in size, but SQL allows only 4KB)

12 home back first prev next last Restrictions on Using Functions in SQL Statements Parameters must be specified with positional notation. Named notation (=>)is not allowed. –Example: SELECT employee_id, tax(salary) FROM employees; SELECT employee_id, tax(p_value => salary) FROM employees; The second SELECT statement will cause an error.

13 home back first prev next last Restrictions on Using Functions in SQL Statements Functions called from a SELECT statement cannot contain DML statements Functions called from an UPDATE or DELETE statement on a table cannot query or contain DML on the same table Functions called from any SQL statement cannot end transactions (that is, cannot execute COMMIT or ROLLBACK operations) Functions called from any SQL statement cannot issue DDL (e.g. CREATE TABLE) or DCL (e.g. ALTER SESSION) because they also do an implicit COMMIT Calls to subprograms that break these restrictions are also not allowed in the function.

14 home back first prev next last Example 1 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, 'Frost', SYSDATE, 'SA_MAN', p_sal); RETURN(p_sal + 100); END dml_call_sql; UPDATE employees SET salary = dml_call_sql(2000) WHERE employee_id = 174;

15 home back first prev next last Example 2 The following function queries the EMPLOYEES table: CREATE OR REPLACE FUNCTION query_max_sal(p_dept_id NUMBER) RETURN NUMBER IS v_num NUMBER; BEGIN SELECT MAX(salary) INTO v_num FROM employees WHERE department_id = p_dept_id; RETURN(v_num); END; When used as the following, it returns the “mutating table” error message similar to the previous slide: UPDATE employees SET salary = query_max_sal(department_id) WHERE employee_id = 174;

16 home back first prev next last Terminology Key terms used in this lesson include: –User Defined Function

17 home back first prev next last Summary In this lesson, you learned to: –List the advantages of user-defined functions in SQL statements –List where user-defined functions can be called from within an SQL statement –Describe the restrictions on calling functions from SQL statements

18 home back first prev next last Try It / Solve It The exercises in this lesson cover the following topics: –Listing the advantages of user defined functions in SQL statements –Listing where user-defined functions can be called from within an SQL statement –Describing the restrictions on calling functions from SQL statements