Stored Procedures and Functions
A stored procedure is a named PL/SQL block that can accept input parameters and return output parameters. It is stored in the database and can be executed by name.
CREATE [OR REPLACE] PROCEDURE procedure_name (
parameter1 IN data_type,
parameter2 OUT data_type
) AS
-- Declaration section
BEGIN
-- Executable statements
EXCEPTION
-- Exception handling
END;
/
|
Parameter Modes
IN : The parameter is passed to the procedure.
OUT : The parameter is returned from the procedure.
IN OUT : The parameter is passed to the procedure and can be returned with a modified value.
|
A function is a named PL/SQL block that returns a single value. It is stored in the database and can be called from SQL statements or other PL/SQL blocks.
CREATE [OR REPLACE] FUNCTION function_name (
parameter1 IN data_type
) RETURN data_type AS
-- Declaration section
BEGIN
-- Executable statements
RETURN value;
EXCEPTION
-- Exception handling
END;
/
|
Functions must contain a RETURN statement.
Functions can be called in SQL queries:
SELECT function_name(column1) FROM table_name;
|
Calling a Stored Procedure
|
DECLARE
output_variable data_type;
BEGIN
procedure_name(input_value, output_variable);
-- Use output_variable
END;
/
|
|
DECLARE
return_value data_type;
BEGIN
return_value := function_name(input_value);
-- Use return_value
END;
/
Or directly in a SQL query:
SELECT function_name(column1) FROM table_name;
|