Presentation is loading. Please wait.

Presentation is loading. Please wait.

More and Still More on Procedures and Functions

Similar presentations


Presentation on theme: "More and Still More on Procedures and Functions"— Presentation transcript:

1 More and Still More on Procedures and Functions
Please use speaker notes for additional information! This is only an introduction to Procedure and Functions. Details would be appropriate for a more advanced course.

2 More on Procedures and Functions
SET VERIFY OFF DECLARE v_idno new_donation.idno%TYPE :='&input_idno'; v_driveno new_donation.driveno%TYPE :='&input_driveno'; v_contamt new_donation.contamt%TYPE :=&input_contamt; v_newcontamt new_donation.contamt%TYPE; v_code VARCHAR2(2); FUNCTION CalcNewDon (f_code VARCHAR2, f_contamt NUMBER) RETURN NUMBER IS f_newcontamt new_donation.contamt%TYPE; BEGIN IF f_code = 'EX' THEN f_newcontamt := f_contamt * 1.3; ELSE IF f_code = 'VG' THEN f_newcontamt := f_contamt * 1.2; IF f_code = 'OK' THEN f_newcontamt := f_contamt * 1.1; f_newcontamt := f_contamt; END IF; RETURN f_newcontamt; END CalcNewDon; PROCEDURE AddDonProc (p_idno new_donation.idno%TYPE, p_driveno new_donation.driveno%TYPE, p_contamt new_donation.contamt%TYPE) AS INSERT INTO new_donation(idno, driveno, contamt) VALUES(p_idno, p_driveno, p_contamt); END AddDonProc; Data passed to the function. Result of the function - return number. The internal function CalcNewDon will return a calculated amount in the variable f_newcontamt. The amount returns depends on the function code that is passed to the function when it is executed. The procedure AddDonProc receives the identification number, the drive number, and the contribution amount when the procedure is called. This information is transferred over and stored in p_idno, p_driveno and p_contamt and then used to INSERT a line in the table. This slide will start to look at PL/SQL code that has both an internal procedure and an internal function to try and clarify the differences. The examples are from the notes! This shows the DECLARE part of the PL/SQL code. The next slide will show the BEGIN. Note that the name in the END statement is not required - it can help to clarify when there are multiple procedures and functions.

3 More on Procedures and Functions
The data passed goes to f_code and f_contamt. BEGIN IF v_contamt > 500 THEN v_code := 'EX'; v_newcontamt := CalcNewDon(v_code, v_contamt); ELSE IF v_contamt > 250 THEN v_code := 'VG'; IF v_contamt > 100 THEN v_code := 'OK'; v_code := 'NG'; END IF; AddDonProc (v_idno, v_driveno, v_newcontamt); END; / SET VERIFY ON F_newcontamt comes back and is known here as v_newcontamt. The processing starts with the BEGIN on this page. Depending on the contribution amount that is inputed by the user, a code is set and the function is passed the code and the inputed amount. The results of the calculation in the function will be returned as f_contamt and stored in v_newcontamt. After the function has returned the result. The AddDonProc is called and is passed the information needed to write a record. The information passed includes the calculated amount from the function. The procedure then INSERTS a new record and the processing is complete. The data passed goes to p_idno, p_driveno and p_contamt.

4 More on Procedures and Functions
call_adddonproc6 Enter value for input_idno: 11111 Enter value for input_driveno: 100 Enter value for input_contamt: 700 PL/SQL procedure successfully completed. SQL> SELECT * FROM new_donation; IDNO DRI CONTDATE CONTAMT JAN FEB MAR MAR MAR JUN JUN JUN JUN 11 rows selected. The input is 11111, 100 and Since the inputted contribution amount is greater than 700 the code EX is passed to the function along with the amount. In the function, if the code is EX then the contribution amount is multiplied by 1.3 (700 * 1.3 = 910). The result of 910 is returned. Then the identification number of 11111, the drive number of 100 and the new contribution amount of 910 is passed to the procedure which inserts the record as shown. Note that the processing begins with the BEGIN of call_adddonproc6. This anonymous block has the code to get the calculation result from the function and call the procedure to do the INSERT. Notice that the Insert made 11 rows - originally there were 10.

5 More on Procedures and Functions
call_adddonproc6 Enter value for input_idno: 22222 Enter value for input_driveno: 200 Enter value for input_contamt: 300 PL/SQL procedure successfully completed. SQL> SELECT * FROM new_donation; IDNO DRI CONTDATE CONTAMT JAN FEB MAR MAR MAR JUN JUN JUN JUN 12 rows selected. In this example, the input is 22222, 200, and The contribution amount is not greaer than 500, but it is greater than 250 so the code VG is passed to the function along with the amount. In the function the code VG causes the contribution amount to be multiplied by 1.2 (300 times 1.2 = 360) The result is returned and then passed to the procedure along with the identification number and the drive number to be inserted into the table. The inserted row brings the total from 11 to 12.

6 Still More - Another example of internal procedures and functions
SQL> DESC testprfn; Name Null? Type FLD NUMBER(3) FLD VARCHAR2(10) FLD NUMBER(5) FLD VARCHAR2(10) SQL> SELECT * FROM testprfn; FLD1 FLD FLD3 FLD4 123 Boston John 234 Providence Ann 345 Fall River Linda 456 Boston Lawrence 567 Fall River Susan Notice that there is a testprfn and a testprfn2 which have the same layout and data.

7 Still More - Another example of internal procedures and functions
BEGIN OPEN fld_cursor; FETCH fld_cursor into v_fld1, v_fld2, v_fld3, v_fld4; WHILE fld_cursor%FOUND LOOP v_fldtot := calc_num(v_fld1, v_fld3); change_fld4(v_fldtot, v_fld4); UPDATE testprfn2 SET fld4 = v_fld4 WHERE CURRENT of fld_cursor; dbms_output.put_line(v_fld1||' '||v_fld4||' '||v_fldtot); END LOOP; CLOSE fld_cursor; END; / SET SERVEROUTPUT OFF SET SERVEROUTPUT ON DECLARE v_fld1 testprfn2.fld1%TYPE; v_fld2 testprfn2.fld2%TYPE; v_fld3 testprfn2.fld3%TYPE; v_fld4 testprfn2.fld4%TYPE; v_fldtot testprfn2.fld4%TYPE; CURSOR fld_cursor IS SELECT * FROM testprfn2 FOR UPDATE; PROCEDURE change_fld4 (p_fldtot IN testprfn2.fld1%TYPE, p_fld4 IN OUT testprfn2.fld4%TYPE) AS BEGIN IF p_fldtot > AND LENGTH (p_fld4) < 8 THEN p_fld4 := ('*'||p_fld4||'*'); END IF; END change_fld4; FUNCTION calc_num (f_fld1 NUMBER, f_fld3 NUMBER) RETURN NUMBER IS f_fldtot testprfn2.fld4%TYPE; f_fldtot := f_fld3 - f_fld1; RETURN f_fldtot; END calc_num; Variables that will be filled from the FETCH. Variable that will be used to hold the results from the function. Cursor that will be filled when it is opened and will hold the data from testprfn2. Function that will return receive the contents of field 1 and field 3 and return the results of a subtraction. Procedure that will test the total returned from the function and the length of field 4 to determine if the contents of field 4 should be changed. Main code (anonymous block) that handles the cursor, the function, the procedure - all of the processing. The begin of the main anonymous block starts the processing. It opens the cursor which fills it according to the SELECT. It then Fetches the first record and starts the loop. The function is executed and the result of the calculation is returned. Then the procedure is called and passed the result of the function and another piece of data. After the function, the file is updated. Notice it is updated even if no change has been made (not very efficient) - a code could have been passed back from the function to determine if the update should take place. The results are displayed and the next record is fetched and the loop is continued until all records in the cursor have been processed. For a more detailed analysis, please see the notes.

8 Still More - Another example of internal procedures and functions
SQL> edit procfnc2 This line brought up the editor with the code shown on the previous slide. procfnc2 123 John 12222 234 Ann 23222 345 *Linda* 34222 456 Lawrence 45222 567 *Susan* 56222 PL/SQL procedure successfully completed. SQL> select * from testprfn2; FLD1 FLD FLD3 FLD4 123 Boston John 234 Providence Ann 345 Fall River *Linda* 456 Boston Lawrence 567 Fall River *Susan* Results of displaying the processing on the screen. Note that where the total (field 3 -field1) was greater than and the name was less than 8 the results are displayed. Update file. Note that the third and fifth records were updated. For details on how results, see the next slide.

9 Still More - Another example of internal procedures and functions
fld3 - fld1 = = which is not > so even though the length of fld4 is less than 8 the change is not made. SQL> select * from testprfn2; FLD1 FLD FLD3 FLD4 123 Boston John 234 Providence Ann 345 Fall River *Linda* 456 Boston Lawrence 567 Fall River *Susan* fld3 - fld1 = = which is not > so even though the length of fld4 is less than 8 the change is not made. fld3 - fld1 = = which is greater than and the length of fld4 is less than 8 so the change is made. fld3 - fld1 = = which is greater than but the length of fld4 is equal to 8 so the change is not made. This shows the results of the processing that is done in the function and in the procedure. The function does the calculation and the procedure does the checking. fld3 - fld1 = = which is greater than and the length of fld4 is less than 8 so the change is made.


Download ppt "More and Still More on Procedures and Functions"

Similar presentations


Ads by Google