Sub topic 3
Creating and Replacing a Stored Procedure
Assume you know basic SQL and anonymous PL/SQL blocks. You also need CREATE PROCEDURE privilege and an employees table with employee_id and salary columns.
A stored procedure is a named PL/SQL program saved in the database. A procedure performs an action, such as updating rows or validating data. Unlike a function, it does not have to return a value.
The basic structure
Use CREATE OR REPLACE PROCEDURE to create a procedure or replace its existing definition:
CREATE OR REPLACE PROCEDURE procedure_name (
parameter_name IN data_type
) AS
local_variable VARCHAR2(100);
BEGIN
-- executable statements
EXCEPTION
WHEN OTHERS THEN
-- error handling
RAISE;
END procedure_name;
/The parameter list is optional. The declaration section follows AS or IS. The executable section begins with BEGIN, and the optional exception section handles errors.
The slash on its own line is a client command. Tools such as SQL*Plus, SQLcl, and many IDEs use it to send the completed CREATE PROCEDURE statement to Oracle. It is not part of the PL/SQL language itself.
A procedure with an anchored parameter type
%TYPE anchors a parameter's type to a table column or another variable. This keeps the procedure aligned with the column definition if that definition changes.
CREATE OR REPLACE PROCEDURE raise_salary (
p_employee_id IN employees.employee_id%TYPE,
p_percent IN NUMBER DEFAULT 10
) AS
BEGIN
UPDATE employees
SET salary = salary * (1 + p_percent / 100)
WHERE employee_id = p_employee_id;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found: ' || p_employee_id);
END IF;
END raise_salary;
/The IN mode makes both parameters inputs. p_percent has a default value of 10, so callers can omit it. SQL%ROWCOUNT reports how many rows the most recent SQL statement affected.
RAISE_APPLICATION_ERROR stops normal execution and returns a user-defined error. Error numbers must be between -20000 and -20999.
What Oracle stores
After successful compilation, Oracle stores the procedure's source and compiled representation in the schema. The procedure is available for later calls until it is dropped or replaced.
| Statement | Purpose |
|---|---|
| `CREATE PROCEDURE` | Creates a new procedure and fails if that name already exists |
| `CREATE OR REPLACE PROCEDURE` | Creates a new procedure or replaces the existing definition |
| `DROP PROCEDURE` | Removes the procedure from the schema |
The procedure's name and parameter signature form its interface. Keep that interface stable when other code calls it.
Calling the procedure
Call a procedure as a statement inside a PL/SQL block:
BEGIN
raise_salary(
p_employee_id => 100,
p_percent => 5
);
END;
/If employee 100 exists, the procedure increases the salary by five percent. If it does not exist, Oracle raises error ORA-20001.
Exercise
What happens when raise_salary(100); is called without a value for p_percent?
Show answer
The procedure uses the default value of 10, so it attempts to increase employee 100's salary by ten percent.