Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Procedures

Similar presentations


Presentation on theme: "Introduction to Procedures"— Presentation transcript:

1 Introduction to Procedures
Please use speaker notes for additional information! We have been looking at PL/SQL anonymous blocks up until now. An anonymous block is compiled and run each time the code is loaded. An anonymous block is stand-alone code that cannot be called by other blocks. The alternative to writing anonymous blocks is to write a procedure, a function, a package or a trigger which can be called from other blocks. Procedures and functions can be grouped together as subprograms.

2 SQL> edit adddonation
Named blocks SQL> edit adddonation CREATE OR REPLACE PROCEDURE AddDonation (p_idno new_donation.idno%TYPE, p_driveno new_donation.driveno%TYPE, p_contdate new_donation.contdate%TYPE, p_contamt new_donation.contamt%TYPE) AS BEGIN INSERT INTO new_donation(idno, driveno, contdate, contamt) VALUES(p_idno, p_driveno, p_contdate, p_contamt); END AddDonation; / Again, contrast the structure of a named block or subprogram such as a procedure or function with the anonymous blocks that we have been coding. Please check a text or Web site for additional information. In this example, adddonation is the name of the procedure. adddonation Procedure created. This procedure is set up to receive data when it is called. The input will then take that data and add a record to the new_donation table. I now need to code the anonymous block that will call this procedure.

3 SQL> edit call_adddonation
Anonymous block SQL> edit call_adddonation SET VERIFY OFF DECLARE v_idno new_donation.idno%TYPE :='&input_idno'; v_driveno new_donation.driveno%TYPE :='&input_driveno'; v_contdate new_donation.contdate%TYPE :='&input_contdate'; v_contamt new_donation.contamt%TYPE :=&input_contamt; BEGIN IF v_contamt > 20 THEN AddDonation (v_idno, v_driveno, v_contdate, v_contamt); END IF; END; / SET VERIFY ON SQL> SELECT * FROM new_donation; IDNO DRI CONTDATE CONTAMT JAN FEB MAR MAR MAR JUN JUN JUN JUN JUL call_adddonation Enter value for input_idno: 12121 Enter value for input_driveno: 200 Enter value for input_contdate: 30-JUL-00 Enter value for input_contamt: 50 PL/SQL procedure successfully completed. This anonymous block calls AddDonation and passes it the variables v_idno, v_driveno, v_contdate, v_contamt. Note that in AddDonation they come in as p_idno, p_driveno, p_contdate, p_contamt. The names can be the same in the two locations or they can be different. The procedure is called and the record is added to the table from the procedure.

4 SQL> DESC inout_table Name Null? Type
Parameters SQL> DESC inout_table Name Null? Type COL_IN NUMBER COL_OUT NUMBER COL_INOUT NUMBER On the next slide, I will pass parameters to this procedure. SQL> edit proc_calc CREATE OR REPLACE PROCEDURE proc_calc (p_in IN NUMBER, p_out OUT NUMBER, p_inout IN OUT NUMBER) IS BEGIN p_out := p_in + p_in; p_inout := p_in + p_in + p_in; INSERT INTO inout_table (col_in, col_out, col_inout) VALUES (p_in, p_out, p_inout); END; / Parameter modes: IN sends the parameters from the calling program to the procedure. This is the default mode illustrated on the previous two slides. OUT sends the parameters from the procedure back to the calling program. IN OUT passes to and receives from the procedure. In this procedure we see p_in which will receive input from the call procedure, p_out which will hold information to be sent back to the call procedure and p_inout which will receive information and return information. proc_calc Procedure created. It is very important to run the procedure so it is created. If you make changes to the procedure, you must run it again so the procedure will be created with the modifications

5 SQL> edit calc_proc_calc
Parameters SQL> edit calc_proc_calc DECLARE v_in NUMBER :=&input_in; v_out NUMBER :=&input_out; v_inout NUMBER :=&input_inout; BEGIN proc_calc(v_in, v_out, v_inout); INSERT INTO inout_table (col_in, col_out, col_inout) VALUES (v_in, v_out, v_inout); END; / Calls proc_calc and sends the 3 parameters v_in, v_out, v_inout. calc_proc_calc Input truncated to 1 characters Enter value for input_in: 15 old 2: v_in NUMBER :=&input_in; new 2: v_in NUMBER :=15; Enter value for input_out: 72 old 3: v_out NUMBER :=&input_out; new 3: v_out NUMBER :=72; Enter value for input_inout: 99 old 4: v_inout NUMBER :=&input_inout; new 4: v_inout NUMBER :=99; PL/SQL procedure successfully completed. SQL> SELECT * FROM inout_table; COL_IN COL_OUT COL_INOUT The three numbers that are inputted are 15, 72, 99. Notice that entering something for v_out which is being passed to the procedure that has defined the receiving field as OUT does no good. The data cannot be read because p_out in the procedure is defined as OUT. Note that in the notes there is no INSERT in this anonymous block, therefore only one line appears after execution.

6 SQL> SELECT * FROM inout_table; COL_IN COL_OUT COL_INOUT
Processing DECLARE v_in NUMBER :=&input_in; v_out NUMBER :=&input_out; v_inout NUMBER :=&input_inout; BEGIN proc_calc(v_in, v_out, v_inout); INSERT INTO inout_table (col_in, col_out, col_inout) VALUES (v_in, v_out, v_inout); END; / 15 72 99 30 45 15 72 99 CREATE OR REPLACE PROCEDURE proc_calc (p_in IN NUMBER, p_out OUT NUMBER, p_inout IN OUT NUMBER) IS BEGIN p_out := p_in + p_in; p_inout := p_in + p_in + p_in; INSERT INTO inout_table (col_in, col_out, col_inout) VALUES (p_in, p_out, p_inout); END; / 15 99 30 := 45 := 15, 72, and 99 are received by the anonymous block. v_in, v_out and v_inout are passed. The 15 and 99 will be received but the 72 will not be received since the p_out is defined as OUT. Note that v_out has to be part of the call because we want to receive information back in v_out. SQL> SELECT * FROM inout_table; COL_IN COL_OUT COL_INOUT

7 SQL> DESC inout_table Name Null? Type
Parameters SQL> DESC inout_table Name Null? Type COL_IN NUMBER COL_OUT NUMBER COL_INOUT NUMBER SQL> edit proc_calc CREATE OR REPLACE PROCEDURE proc_calc (p_in IN NUMBER, p_out OUT NUMBER, p_inout IN OUT NUMBER) IS BEGIN p_out := p_in + p_in; p_inout := p_inout + p_in; INSERT INTO inout_table (col_in, col_out, col_inout) VALUES (p_in, p_out, p_inout); END; / I have now modified the p_inout to have a different value. On the next slide, I will pass parameters to this procedure.

8 SQL> edit calc_proc_calc
Parameters SQL> edit calc_proc_calc DECLARE v_in NUMBER :=&input_in; v_out NUMBER :=&input_out; v_inout NUMBER :=&input_inout; BEGIN proc_calc(v_in, v_out, v_inout); INSERT INTO inout_table (col_in, col_out, col_inout) VALUES (v_in, v_out, v_inout); END; / Calls proc_calc and sends the 3 parameters v_in, v_out, v_inout. calc_proc_calc Input truncated to 1 characters Enter value for input_in: 15 old 2: v_in NUMBER :=&input_in; new 2: v_in NUMBER :=15; Enter value for input_out: 72 old 3: v_out NUMBER :=&input_out; new 3: v_out NUMBER :=72; Enter value for input_inout: 99 old 4: v_inout NUMBER :=&input_inout; new 4: v_inout NUMBER :=99; PL/SQL procedure successfully completed. SQL> SELECT * FROM inout_table; COL_IN COL_OUT COL_INOUT First I will run through the exercises in the notes. See the next slide for notes on the processing.

9 SQL> SELECT * FROM inout_table; COL_IN COL_OUT COL_INOUT
Processing DECLARE v_in NUMBER :=&input_in; v_out NUMBER :=&input_out; v_inout NUMBER :=&input_inout; BEGIN proc_calc(v_in, v_out, v_inout); INSERT INTO inout_table (col_in, col_out, col_inout) VALUES (v_in, v_out, v_inout); END; / 15 72 99 30 114 15 72 99 CREATE OR REPLACE PROCEDURE proc_calc (p_in IN NUMBER, p_out OUT NUMBER, p_inout IN OUT NUMBER) IS BEGIN p_out := p_in + p_in; p_inout := p_inout + p_in; INSERT INTO inout_table (col_in, col_out, col_inout) VALUES (p_in, p_out, p_inout); END; / 15 99 30 := 114 := Notice that the INSERT in the named procedure is done before the INSERT in the anonymous block. 15, 72 and 99 are passed to the procedure. In fact only 15 and 99 are taken in because p_out cannot be read - it is an output parameter. In the procedure, p_out is calculated using p_in. V_out is in fact set so that it can receive data - if there is data in it the v_out data is ignored. Needless to say, in this example, I did not need to take in the 72 for v_out unless I wanted it in the anonymous block. If I had done the INSERT before the call to the procedure, the 72 would have shown up in the output record/row. SQL> SELECT * FROM inout_table; COL_IN COL_OUT COL_INOUT

10 Processing DECLARE v_in NUMBER :=&input_in; v_out NUMBER :=&input_out; v_inout NUMBER :=&input_inout; BEGIN proc_calc(v_in, v_out, v_inout); INSERT INTO inout_table (col_in, col_out, col_inout) VALUES (v_in, v_out, v_inout); END; / 15 72 99 15 72 99 Show errors can be used to show the details of the error that occurred. CREATE OR REPLACE PROCEDURE proc_calc (p_in IN NUMBER, p_out OUT NUMBER, p_inout IN OUT NUMBER) IS BEGIN p_out := p_out + p_in; p_inout := p_inout + p_in; INSERT INTO inout_table (col_in, col_out, col_inout) VALUES (p_in, p_out, p_inout); END; / 15 99 SQL> edit proc_calc proc_calc Warning: Procedure created with compilation errors. SQL> show errors Errors for PROCEDURE PROC_CALC: LINE/COL ERROR 6/ PL/SQL: Statement ignored 6/ PLS-00365: 'P_OUT' is an OUT parameter and cannot be read Note I changed the calculation of p_out to include p_out as part of the calculation. This is assuming that data is received in p_out and that it can be used in a calculation. However because p_out was defined as OUT it cannot receive data or be used in the calculating part of the assign. It can only be used to receive an answer. The resulting error is shown. Also remember that I had to proc_calc to have the procedure created with the code change. The warning came back. To see the errors I used show errors.

11 Procedure & parameters
SQL> SELECT * FROM donor_proc; IDNO NAME CITY ST YRGOAL CONTACT 11111 Stephen Daniels Seekonk MA John Smith 12121 Jennifer Ames Providence RI Susan Jones 22222 Carl Hersey Providence RI Susan Jones 23456 Susan Ash Fall River MA Amy Costa 33333 Nancy Taylor Fall River MA John Adams 34567 Robert Brooks Fall River MA Amy Costa SQL> CREATE OR REPLACE PROCEDURE in_yrgoal 2 (v_idno IN donor_proc.idno%TYPE) 3 IS 4 BEGIN 5 UPDATE donor_proc 6 SET yrgoal = yrgoal * 1.1 7 WHERE idno = v_idno; 8 END in_yrgoal; 9 / Procedure created. The execute is used to invoke a procedure in SQL*Plus. SQL> execute in_yrgoal(12121); PL/SQL procedure successfully completed. Results of the update. This procedure was created in ORACLE at the SQL prompt. I usually create them in the editor because I am prone to typos, but I wanted to show that it could be done. This is also a good technique for testing a procedure independently. Note that after END, I added the procedure name - this is entirely optional but is frequently used for documentation and clarity. This procedure can be saved under the name in_yrgoal and you can use the editor to make changes. The parameter v_idno has been defined as an IN parameter. When the procedure is executed, from SQL*Plus, the data can be passed to in by specifying execute followed by the procedure name followed by the v_idno. SQL> SELECT * FROM donor_proc; IDNO NAME CITY ST YRGOAL CONTACT 11111 Stephen Daniels Seekonk MA John Smith 12121 Jennifer Ames Providence RI Susan Jones 22222 Carl Hersey Providence RI Susan Jones 23456 Susan Ash Fall River MA Amy Costa 33333 Nancy Taylor Fall River MA John Adams 34567 Robert Brooks Fall River MA Amy Costa

12 Procedures & parameters
SQL> CREATE OR REPLACE PROCEDURE out_yrgoaletc 2 (v_idno IN donor_proc.idno%TYPE, v_name OUT donor_proc.name%TYPE, v_yrgoal OUT donor_proc.yrgoal%TYPE) 5 IS 6 BEGIN 7 SELECT idno, name, yrgoal 8 INTO v_idno, v_name, v_yrgoal 9 FROM donor_proc WHERE idno = v_idno; 11 END; 12 / Warning: Procedure created with compilation errors. SQL> show errors Errors for PROCEDURE OUT_YRGOALETC: LINE/COL ERROR 7/ PL/SQL: SQL Statement ignored 8/ PLS-00403: expression 'V_IDNO' cannot be used as an INTO-target of a SELECT/FETCH statement The parameter v_idno is expecting input from an anonymous block that calls the procedure or from the data passed in the execute. It was defined as IN. Again remember that show errors can be used to see the specific errors involved in the compilation of the procedure. The fix and execution are shown on the next slides.

13 Procedures & parameters
SQL> edit Wrote file afiedt.buf 1 CREATE OR REPLACE PROCEDURE out_yrgoaletc 2 (v_idno IN donor_proc.idno%TYPE, v_name OUT donor_proc.name%TYPE, v_yrgoal OUT donor_proc.yrgoal%TYPE) 5 IS 6 BEGIN 7 SELECT name, yrgoal 8 INTO v_name, v_yrgoal 9 FROM donor_proc WHERE idno = v_idno; 11* END; SQL> / Procedure created. Note that the procedure can be saved under a name. In this code I am simply editing the last code at the SQL prompt. While I was in the editor, I saved the procedure under the name out_yrgoaletc under the directory/folder Orawin95 and the subdirectory/folder bin. This is the default save area. Once the procedure has been saved under a name it can be created one of these ways at the SQL prompt. out_yrgoaletc Procedure created. SQL> START out_yrgoaletc Procedure created.

14 Procedures & parameters
Note that the procedure has 3 parameters so all 3 have to be passed. Since I am executing from SQP*Plus, I need to pass 3 parameters with the execute but in fact only one of the 3 is an IN that can receive data. The other 2 parameters are OUT that will be returning data. Therefore they need to be given names. This is done through the VARIABLE command at the prompt. SQL> VARIABLE s_name VARCHAR2(15) SQL> VARIABLE s_yrgoal NUMBER SQL> execute out_yrgoaletc(11111, :s_name, :s_yrgoal) PL/SQL procedure successfully completed. SQL> PRINT s_name S_NAME Stephen Daniels CREATE OR REPLACE PROCEDURE out_yrgoaletc (v_idno IN donor_proc.idno%TYPE, v_name OUT donor_proc.name%TYPE, v_yrgoal OUT donor_proc.yrgoal%TYPE) IS BEGIN SELECT name, yrgoal INTO v_name, v_yrgoal FROM donor_proc WHERE idno = v_idno; END; / The information in this slide is really beyond the scope of this course. I am presenting it because it clearly shows the difference between something defined as IN and OUT. The OUT data is returned to the named variables and can be seen with PRINT. SQL> PRINT s_yrgoal S_YRGOAL 500


Download ppt "Introduction to Procedures"

Similar presentations


Ads by Google