Beruflich Dokumente
Kultur Dokumente
A procedure:
• Is a type of subprogram that performs an action
• Can be stored in the database as a schema object
• Promotes reusability and maintainability
1 Edit 2 Load
file.sql
3 Create (compile and store)
4 Execute
Use SHOW ERRORS
for compilation errors
Parameters:
• Are declared after the subprogram name in the
PL/SQL header
• Pass or communicate data between the caller and
the subprogram
• Are used like local variables but are dependent on
their parameter-passing mode:
– An IN parameter (the default) provides values for a
subprogram to process.
– An OUT parameter returns a value to the caller.
– An IN OUT parameter supplies an input value, which
may be returned (output) as a modified value.
Modes
IN (default)
Calling
OUT
environment
IN OUT
Procedure
EXECUTE raise_salary(176,10)
DECLARE
emp_name employees.last_name%TYPE;
emp_sal employees.salary%TYPE;
BEGIN
query_emp(171, emp_name, emp_sal); ...
END;
Calling environment
phone_no (before the call) phone_no (after the call)
'8006330575' '(800)633-0575'
• Positional:
– Lists the actual parameters in the same order as the
formal parameters
• Named:
– Lists the actual parameters in arbitrary order and
uses the association operator (=>) to associate a
named formal parameter with its actual parameter
• Combination:
– Lists some of the actual parameters as positional
and some as named
IN OUT IN OUT
Default mode Must be Must be specified
specified
Value is passed into Returned to Passed into
subprogram calling subprogram; returned
environment to calling environment
Formal parameter acts Uninitialized Initialized variable
as a constant variable
Actual parameter can Must be a Must be a variable
be a literal, expression, variable
constant, or initialized
variable
Can be assigned a Cannot be Cannot be assigned
default value assigned a default value
a default value
SET SERVEROUTPUT ON
CREATE PROCEDURE add_department_noex(
name VARCHAR2, mgr NUMBER, loc NUMBER) IS
BEGIN
INSERT INTO DEPARTMENTS (department_id,
department_name, manager_id, location_id)
VALUES (DEPARTMENTS_SEQ.NEXTVAL, name, mgr, loc);
DBMS_OUTPUT.PUT_LINE('Added Dept: '||name);
END;
• Example:
DROP PROCEDURE raise_salary;
• Easy maintenance
• Improved data security and integrity
• Improved performance
• Improved code clarity