Introduction to Procedures

Slides:



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

Oracle PL/SQL IV Exceptions Packages.
Reports Using SQL Script Please check speaker notes for additional information!
Group functions using SQL Additional information in speaker notes!
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!
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
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.
Chapter 9: Advanced SQL and PL/SQL Topics Guide to Oracle 10g.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
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.
SQL Use of Functions Character functions Please use speaker notes for additional information!
SQL functions - numeric and date Speaker notes contain additional information!
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
More on variables with Oracle’s SQL*Plus As always, speaker notes will contain additional information!
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
XML and DTD Please you speaker notes for additional information!
More on views Please refer to speaker notes for additional information!
SQL and Conditions Speaker notes will provide additional information!
Manipulating data within PL/SQL Please use speaker notes for additional information!
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.
Indexes in Oracle An Introduction Please check speaker notes for additional information!
Introduction to Oracle - SQL Additional information is available in speaker notes!
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
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.
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.
Introduction to PL/SQL As usual, use speaker notes for additional information!
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
Sequential Processing to Update a File Please use speaker notes for additional information!
More SQL functions As usual,there is additional information in the speaker notes!
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.
Oracle: Programming Languages SQL and PL/SQL Introduction Toomas Lepikult
Creating Stored Functions
Working in the Forms Developer Environment
SQL and SQL*Plus Interaction
Using SQL*Plus.
Creating Stored Procedures and Functions
Introduction to Triggers
Introduction to Triggers
More on Procedures (Internal/Local procedures)
Using SQL*Plus.
PL/SQL Scripting in Oracle:
MySQL - Creating donorof database offline
Introduction to Functions
Please use speaker notes for additional information!
Handling Exceptions.
PRACTICE OVERVIEW PL/SQL Part - 2.
Chapter 2 - Introduction to C Programming
Please use speaker notes for additional information!
Introduction to Views and Reports
ORACLE.
Please use speaker notes for additional information!
Please use speaker notes for additional information!
PRACTICE OVERVIEW PL/SQL Part - 1.
Using SQL*Plus.
Using screens and adding two numbers - addda.cbl
More and Still More on Procedures and Functions
Prof. Arfaoui. COM390 Chapter 6
Running a Java Program using Blue Jay.
Please use speaker notes for additional information!
Introduction to C Programming
Presentation transcript:

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.

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. SQL> @ 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.

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 ----- --- --------- --------- 11111 100 07-JAN-99 25 12121 200 23-FEB-99 40 23456 100 03-MAR-99 20 33333 300 10-MAR-99 10 22222 100 14-MAR-99 10 12121 100 04-JUN-99 50 11111 200 12-JUN-99 35 23456 300 14-JUN-99 10 12121 300 10-JUN-99 75 12121 200 30-JUL-00 50 SQL> @ 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.

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. SQL> @ 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

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. SQL> @ 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 --------- --------- --------- 15 30 45 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.

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 15 30 45 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 := 15 + 15 45 := 15 + 15 + 15 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 --------- --------- --------- 15 30 45

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.

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. SQL> @ 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 --------- --------- --------- 15 30 114 First I will run through the exercises in the notes. See the next slide for notes on the processing.

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 15 30 114 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 := 15 + 15 114 := 99 + 15 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 --------- --------- --------- 15 30 114

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 SQL> @ proc_calc Warning: Procedure created with compilation errors. SQL> show errors Errors for PROCEDURE PROC_CALC: LINE/COL ERROR -------- ------------------------------------------------------------ 6/5 PL/SQL: Statement ignored 6/14 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 use @ proc_calc to have the procedure created with the code change. The warning came back. To see the errors I used show errors.

Procedure & parameters SQL> SELECT * FROM donor_proc; IDNO NAME CITY ST YRGOAL CONTACT ----- --------------- ---------- -- --------- ------------ 11111 Stephen Daniels Seekonk MA 500 John Smith 12121 Jennifer Ames Providence RI 400 Susan Jones 22222 Carl Hersey Providence RI Susan Jones 23456 Susan Ash Fall River MA 100 Amy Costa 33333 Nancy Taylor Fall River MA 50 John Adams 34567 Robert Brooks Fall River MA 50 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 500 John Smith 12121 Jennifer Ames Providence RI 440 Susan Jones 22222 Carl Hersey Providence RI Susan Jones 23456 Susan Ash Fall River MA 100 Amy Costa 33333 Nancy Taylor Fall River MA 50 John Adams 34567 Robert Brooks Fall River MA 50 Amy Costa

Procedures & parameters SQL> CREATE OR REPLACE PROCEDURE out_yrgoaletc 2 (v_idno IN donor_proc.idno%TYPE, 3 v_name OUT donor_proc.name%TYPE, 4 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 10 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/4 PL/SQL: SQL Statement ignored 8/9 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.

Procedures & parameters SQL> edit Wrote file afiedt.buf 1 CREATE OR REPLACE PROCEDURE out_yrgoaletc 2 (v_idno IN donor_proc.idno%TYPE, 3 v_name OUT donor_proc.name%TYPE, 4 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 10 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. SQL> @ out_yrgoaletc Procedure created. SQL> START out_yrgoaletc Procedure created.

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