PL/SQL Parameters   «Prev  Next»

Lesson 1

Introduction to PL/SQL Parameters in Oracle 26ai

A PL/SQL parameter is a named, typed part of a procedure or function's interface. It lets a caller supply information, receive a result, or do both. Instead of creating a separate procedure for every order, customer, or calculation, you write one routine and call it with different arguments.

This module explains parameters in Oracle AI Database 26ai, building on the procedures and functions introduced earlier. It also covers SQL*Plus script arguments and parameterized cursors. These mechanisms all help reuse code, but they operate differently: SQL*Plus can substitute text into a script, while PL/SQL subprograms receive arguments through declared parameters.

Module objectives

By the end of this module, you will be able to:

  1. Distinguish SQL*Plus substitution variables, bind variables, and PL/SQL parameters.
  2. Supply arguments when starting a SQL*Plus script.
  3. Explain formal parameters and the actual arguments supplied by a caller.
  4. Declare procedure parameters with IN, OUT, and IN OUT modes.
  5. Distinguish procedure output parameters from a function's return value.
  6. Use parameters to specialize an explicit cursor's query.

Where parameters and variables are used

Identify the mechanism before deciding how to supply a value. The following distinctions will recur throughout the module.

PL/SQL parameters and SQL*Plus inputs
MechanismPurpose and notation
Subprogram parameterA typed declaration such as p_units IN NUMBER receives an argument when a procedure or function is called.
Script argument@example.sql 10 supplies a positional value that SQL*Plus can substitute for &1 in the script.
Substitution variableSQL*Plus replaces references such as &name with text before executing the resulting command.
Bind variableSQL*Plus declares a variable with VARIABLE; SQL and PL/SQL refer to its value using notation such as :v_units.

Substituting text can change a statement's structure. Binding supplies a value separately from that text, so the two approaches are not interchangeable when handling external input. Oracle's SQL*Plus variable documentation explains their different behavior.

The three PL/SQL parameter modes

A parameter's mode describes the direction in which information passes between a caller and a subprogram:

  • IN supplies an input. This is the default mode. The subprogram can read the formal parameter but cannot assign a new value to it.
  • OUT supplies a result to the caller. The caller passes a variable that can receive the result, and the subprogram should assign the intended output.
  • IN OUT supplies an initial value and receives an updated value. For example, a routine could adjust an amount held in the caller's variable.

Output values should be used after successful completion. If a subprogram exits with an unhandled exception, do not assume that its output arguments contain valid results. See Oracle's subprogram parameter rules.

Formal parameters, arguments, and defaults

The parameter declared in a routine's heading is a formal parameter. The value or variable supplied in a call is an actual parameter, also called an argument. Different names help make those roles visible.

A declaration such as p_rate IN NUMBER DEFAULT 0.05 makes that input optional. Omitting its argument uses the default; explicitly passing NULL supplies a null value. A default is a declaration option, not another parameter mode. See the formal parameter declaration reference.

A small procedure-and-caller example

This SQL*Plus example declares a local procedure inside an anonymous PL/SQL block. It requires no sample tables and creates no stored object.

SET SERVEROUTPUT ON

DECLARE
  l_total NUMBER;

  PROCEDURE calculate_total (
    p_units IN NUMBER,
    p_unit_price IN NUMBER,
    p_total OUT NUMBER
  ) IS
  BEGIN
    p_total := p_units * p_unit_price;
  END calculate_total;
BEGIN
  calculate_total(
    p_units => 4,
    p_unit_price => 12.5,
    p_total => l_total
  );
  DBMS_OUTPUT.PUT_LINE(l_total);
END;
/

The expected displayed number is 50. The caller supplies 4 and 12.5 as inputs; the procedure assigns their product to p_total, which supplies the result to l_total. The => notation identifies arguments by parameter name. Positional calls instead follow the declaration order.

SET SERVEROUTPUT ON enables SQL*Plus to display DBMS_OUTPUT messages; the final slash submits the block for execution. Both are client controls. This arithmetic example assumes valid inputs; later routines need validation appropriate to their business rules.

Preparing to use SQL*Plus

SQL*Plus is a command-line client for running SQL, PL/SQL blocks, and scripts. You need an installed client, a reachable Oracle database service, and an authorized account. Its availability depends on the client installation; it should not be assumed to accompany every Oracle environment.

To start without connecting immediately, enter sqlplus /nolog in your operating-system terminal. At the SQL*Plus prompt, enter CONNECT training_user@training_service and supply the password when prompted. Replace those illustrative names with your account and configured service. Oracle documents these options in Starting SQL*Plus.

How functions, cursors, and triggers differ

A function declares a return datatype and uses a RETURN statement to supply its result. A procedure can use a bare RETURN to exit, but cannot attach a function-style result expression to it. RETURN is a PL/SQL statement, not a SQL*Plus command. See Oracle's RETURN reference.

A parameterized explicit cursor accepts values when opened, allowing one query definition to serve different criteria. That differs from passing a cursor variable, such as SYS_REFCURSOR, through a subprogram parameter. Neither creates another parameter mode.

Triggers run in response to database events rather than an ordinary caller-supplied argument list. A trigger can call a parameterized procedure, but its event and row context is a separate interface.

Next, the uses of parameters in SQL*Plus and PL/SQL develops these distinctions before you work through script arguments and procedure declarations.