Introduction to Functions

Slides:



Advertisements
Similar presentations
PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Relational example using donor, donation and drive tables For additional information see the speaker notes!
PL/SQL - Using IF statements Please use speaker notes for additional information!
More on IF statements Use speaker notes for additional information!
Table maintenance revisited (again) Please use speaker notes for additional information!
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.
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
 Allows sophisticated data processing  Build complex business logic in a modular fashion  Use over and over  Execute rapidly – little network traffic.
 Allows sophisticated data processing  Build complex business logic in a modular fashion  Use over and over  Execute rapidly – little network traffic.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
More on relational databases, including 1 to 1, 1 to many and many to many relationships Please use speaker notes for additional information!
Relationships and Advanced Query Concepts Using Multiple Tables Please use speaker notes for additional information!
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.
Manipulating data within PL/SQL Please use speaker notes for additional information!
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
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.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
26 Mar 04 1 Application Software Practical 5/6 MS Access.
Exceptions in PL/SQL Please use speaker notes for additional information!
Stored Procedures. Definition a stored procedure is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database.
Introduction to Oracle - SQL Additional information is available in speaker notes!
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.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Introduction to PL/SQL As usual, use speaker notes for additional information!
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
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.
What Are Subprograms? Subprograms are named PL/SQL blocks that can take parameters and be invoked. Subprograms allow decomposition of a program into logical.
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
Creating Stored Functions
Chapter 2 Anonymous Block
A Guide to SQL, Seventh Edition
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
PL/pgSQL
Creating Stored Procedures and Functions
Introduction to PL/SQL Programing
PL/SQL.
Chapter 10: Void Functions
Introduction to Triggers
More on Procedures (Internal/Local procedures)
Introduction to PL/SQL
Handling Exceptions.
Database Programming PL SQL.
Introduction to Procedures
Using SQL*Plus.
MySQL - Creating donorof database offline
Oracle Stored Procedures and Functions
Please use speaker notes for additional information!
PRACTICE OVERVIEW PL/SQL Part - 2.
Database Management Systems 2
Handling Exceptions.
Creating Noninput Items
PL/SQL Declaring Variables.
MATERI PL/SQL Procedures Functions Packages Database Triggers
More and Still More on Procedures and Functions
Oracle Stored Procedures and Functions
Prof. Arfaoui. COM390 Chapter 6
SQL Stored Procedures and Functions Presented by: Dr. Samir Tartir
Chapter 10: Void Functions
© 2014, Mike Murach & Associates, Inc.
Presentation transcript:

Introduction to Functions Please use speaker notes for additional information! A function like a procedure receives arguments from the calling program. The difference is that a function is part of an expression and returns a single value to the calling program for its use. A stored function is a named PL/SQL block just as a procedure is. A function can be invoked and passed paramenters. A function must return a value to the calling block, therefore it is used to compute a value frequently. This show matches the handout. More information on functions is in the more on presentation...

Functions CREATE OR REPLACE FUNCTION func_calc1 (v_idno new_donation_one.idno%TYPE) RETURN NUMBER IS v_return_donation new_donation_one.contamt%TYPE; v_contamt new_donation_one.contamt%TYPE; BEGIN SELECT contamt INTO v_contamt FROM new_donation_one WHERE idno = v_idno; IF v_contamt > 10 THEN v_return_donation := v_contamt * 2; RETURN v_return_donation; ELSE v_return_donation := v_contamt * 1.5; END IF; END func_calc1; / SQL> edit func_calc1 Func_calc1 is the function and calc_func_calc is the anonymous block that causes the function to be executed. SQL> edit calc_func_calc SET SERVEROUTPUT ON DECLARE v_idno new_donation_one.idno%TYPE :='&input_idno'; v_new_goal new_donation_one.contamt%TYPE; BEGIN v_new_goal := func_calc1(v_idno); dbms_output.put_line('New amount: '||TO_CHAR(v_new_goal)); END; / SET SERVEROUTPUT OFF This slide shows both the function and the anonymous block that causes the function to be run. Note that the assignment statement assigns the number that is returned by the function to v_new_goal. The function is called and the identification number is sent. The SELECT in the function retrieves the contribution amount for that donation. The IF statement than calculates the v_return_donation depending on v_contamt. The result of the calculation is returned to the anonymous block which can then use the amount as needed. Note that the select is getting a single record. This works because the donation table that is being used has only one donation per id. The number to be returned is v_return_donation.

SQL> @ calc_func_calc Enter value for input_idno: 12121 Functions SQL> select * from new_donation_one order by idno; IDNO DRI CONTDATE CONTAMT ----- --- --------- --------- 11111 100 07-JAN-99 25 12121 300 10-JUN-99 75 22222 100 14-MAR-99 10 23456 300 14-JUN-99 10 33333 300 10-MAR-99 10 SQL> @ func_calc1 Function created. SQL> @ calc_func_calc Enter value for input_idno: 12121 old 2: v_idno new_donation_one.idno%TYPE :='&input_idno'; new 2: v_idno new_donation_one.idno%TYPE :='12121'; New amount: 150 PL/SQL procedure successfully completed. Enter value for input_idno: 22222 new 2: v_idno new_donation_one.idno%TYPE :='22222'; New amount: 15 Remember the anonymous block displays the value assigned to v_new_goal by the function. IF v_contamt > 10 THEN v_return_donation := v_contamt * 2; RETURN v_return_donation; ELSE v_return_donation := v_contamt * 1.5; END IF; Note that 12121 had a contamt of 75 so v_return_donation is twice that or 150. With 22222 the contamt was 10 so the ELSE is taken so v_return_donation is 1.5 times or 15.