Download presentation
Presentation is loading. Please wait.
1
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...
2
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.
3
SQL> @ calc_func_calc Enter value for input_idno: 12121
Functions SQL> select * from new_donation_one order by idno; IDNO DRI CONTDATE CONTAMT JAN JUN MAR JUN MAR func_calc1 Function created. 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 had a contamt of 75 so v_return_donation is twice that or 150. With the contamt was 10 so the ELSE is taken so v_return_donation is 1.5 times or 15.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.