Note:
You may need to set up data structures, similar to the following, for certain examples to work:CREATE TABLE Emp_tab ( Empno NUMBER(4) NOT NULL, Ename VARCHAR2(10), Job VARCHAR2(9), Mgr NUMBER(4), Hiredate DATE, Sal NUMBER(7,2), Comm NUMBER(7,2), Deptno NUMBER(2)); CREATE OR REPLACE PROCEDURE fire_emp1(Emp_id NUMBER) AS BEGIN DELETE FROM Emp_tab WHERE Empno = Emp_id; END; VARIABLE Empnum NUMBER;
Procedures can be called from many different environments. For example:
This section includes some common examples of calling procedures from within these environments.
A procedure or trigger can call another stored procedure. For example, included in the body of one procedure might be the following line:
. . . Sal_raise(Emp_id, 200); . . .
This line calls the Sal_raise procedure. Emp_id is a variable within the context of the procedure. Recursive procedure calls are allowed within PL/SQL: A procedure can call itself.
A procedure can be called interactively from an Oracle Database tool, such as SQL*Plus. For example, to call a procedure named SAL_RAISE, owned by you, you can use an anonymous PL/SQL block, as follows:
BEGIN Sal_raise(7369, 200); END;
Note:
Interactive tools, such as SQL*Plus, require you to follow these lines with a slash (/) to run the PL/SQL block.
An easier way to run a block is to use the SQL*Plus statement EXECUTE, which wraps BEGIN and END statements around the code you enter. For example:
EXECUTE Sal_raise(7369, 200);
Some interactive tools allow session variables to be created. For example, when using SQL*Plus, the following statement creates a session variable:
VARIABLE Assigned_empno NUMBER
After defined, any session variable can be used for the duration of the session. For example, you might run a function and capture the return value using a session variable:
EXECUTE :Assigned_empno := Hire_emp('JSMITH', 'President', 1032, SYSDATE, 5000, NULL, 10); PRINT Assigned_empno; ASSIGNED_EMPNO -------------- 2893
A 3GL database application, such as a precompiler or an OCI application, can include a call to a procedure within the code of the application.
To run a procedure within a PL/SQL block in an application, simply call the procedure. The following line within a PL/SQL block calls the Fire_emp procedure:
Fire_emp1(:Empnun);
In this case, :Empno is a host (bind) variable within the context of the application.
To run a procedure within the code of a precompiler application, you must use the EXEC call interface. For example, the following statement calls the Fire_emp procedure in the code of a precompiler application:
EXEC SQL EXECUTE BEGIN Fire_emp1(:Empnum); END; END-EXEC;
References to procedures and packages are resolved according to the algorithm described in the "Rules for Name Resolution in SQL Statements" section of Chapter 2, "Designing Schema Objects".
If you are the owner of a standalone procedure or package, then you can run the standalone procedure or packaged procedure, or any public procedure or packaged procedure at any time, as described in the previous sections. If you want to run a standalone or packaged procedure owned by another user, then the following conditions apply:
EXECUTE Jward.Fire_emp (1043); EXECUTE Jward.Hire_fire.Fire_emp (1043);
When you call a procedure, specify a value or parameter for each of the procedure's arguments. Identify the argument values using either of the following methods, or a combination of both:
For example, these statements each call the procedure Sal_raise to increase the salary of employee number 7369 by 500:
Sal_raise(7369, 500); Sal_raise(Sal_incr=>500, Emp_id=>7369); Sal_raise(7369, Sal_incr=>500);
The first statement identifies the argument values by listing them in the order in which they appear in the procedure specification.
The second statement identifies the argument values by name and in an order different from that of the procedure specification. If you use argument names, then you can list the arguments in any order.
The third statement identifies the argument values using a combination of these methods. If you use a combination of order and argument names, then values identified in order must precede values identified by name.
If you used the DEFAULT option to define default values for IN parameters to a subprogram (see the Oracle Database PL/SQL User's Guide and Reference),then you can pass different numbers of actual parameters to the first subprogram, accepting or overriding the default values as you please. If an actual value is not passed, then the corresponding default value is used. If you want to assign a value to an argument that occurs after an omitted argument (for which the corresponding default is used), then you must explicitly designate the name of the argument, as well as its value.
< < Debugging Stored Procedures