PL/SQL Parameters   «Prev  Next»

Lesson 3How to use parameters in SQL*Plus
Objective Use parameters when starting a SQL*Plus script.

Passing Parameters to SQL*Plus Scripts in Oracle 26ai

You can pass arguments to a SQL*Plus script when starting SQL*Plus from the operating system or when running a file with START or @ inside an existing session. The script refers to those arguments by position: &1 represents the first argument, &2 the second, and so on. This lets one saved query serve several requests without editing the file each time.

In Oracle AI Database 26ai, these references are SQL*Plus substitution variables. The client inserts their text before sending the resulting SQL to the database. They differ from typed PL/SQL parameters and from bind variables such as :customer_id. This lesson follows one customer query through both ways of supplying arguments.

Prepare a script with positional parameters

The example assumes that SQL*Plus is installed and available on your PATH. You also need an authorized account named petstore, a configured Oracle Net alias named TRAINING, and an existing CUSTOMER table with character columns named FIRSTNAME, LASTNAME, and STATE. These are teaching names, not resources created by starting SQL*Plus.

Save the following text as customer.sql in your working directory:

SET DEFINE ON
SET VERIFY ON
SELECT firstname, lastname, state
FROM customer
WHERE firstname LIKE '&1%'
   OR state = '&2';

The first argument supplies a first-name prefix. The second supplies a state code. The % wildcard allows additional characters after the prefix. Because the conditions use OR, a row qualifies when either condition matches; the customer does not have to meet both.

SET DEFINE ON enables ampersand substitution, while SET VERIFY ON displays the substitutions. The SELECT ends with a semicolon. Do not add a slash after it: that would execute the buffered query again.

Pass arguments when starting SQL*Plus

From PowerShell, in the folder containing customer.sql, enter:

sqlplus petstore@TRAINING "@customer.sql" Amy HI

petstore@TRAINING identifies the login and connection alias. The separate "@customer.sql" argument tells SQL*Plus which file to run; quoting it passes the at-sign token literally through PowerShell. Amy supplies &1, and HI supplies &2.

SQL*Plus prompts for the omitted password, then runs the script after connecting. Enter the password at that prompt, where it is not displayed. Keep passwords out of the command and ordinary script arguments. Oracle documents both the logon syntax and startup script arguments in Starting SQL*Plus.

Oracle 26ai SQL*Plus workflow: save customer.sql, pass arguments at startup, inspect substitution and the SQL buffer, then rerun with START or @.
Pass script arguments at SQL*Plus startup or with START and @. SQL*Plus substitutes &1 and &2 before executing the query; LIST displays the SQL buffer without executing it.

Read the substitution output

With VERIFY enabled, SQL*Plus shows the relevant lines before and after substitution. For the preceding arguments, an illustrative excerpt is:

old 3: WHERE firstname LIKE '&1%'
new 3: WHERE firstname LIKE 'Amy%'
old 4:    OR state = '&2'
new 4:    OR state = 'HI'

The query therefore selects customers whose first name starts with Amy or whose state is HI. The returned rows depend on your table's contents. The diagram and excerpt explain the process; they do not establish a particular result count.

VERIFY is a display setting, not input validation. SET VERIFY OFF hides the old/new lines but still allows substitution. Oracle's substitution-variable guide explains this client-side processing.

Run the script again with START or @

Once connected, enter the following at the SQL*Plus prompt:

LIST
START customer.sql J CA

LIST displays the SQL buffer without executing it. START then reads the script file again, supplying J as the prefix and CA as the state. A matching row now has a first name beginning with J or a state of CA.

The shorter form below is an alternative to START, not an additional required step:

@customer.sql J CA

Both forms use the same file and positional argument order. Specify the .sql extension explicitly in these examples so the filename is clear.

Pass an argument containing spaces

Inside SQL*Plus, group a multiword argument with double quotes:

START customer.sql "Mary Ann" CA

The first argument is the complete prefix Mary Ann; CA remains the second. Those double quotes group the argument. The single quotes already present in customer.sql delimit the resulting SQL character literal. These quoting roles are distinct.

This example is entered inside SQL*Plus. When launching from an operating-system shell, that shell also processes quoting. Do not assume that PowerShell, Command Prompt, and a Unix shell handle every quoted argument identically.

Inspect retained values and clear them

Arguments supplied by START become defined substitution variables in the current session. If you later omit arguments, existing values can be reused. A missing argument therefore does not always produce a prompt. Prompting occurs when SQL*Plus encounters an undefined reference while substitution is enabled.

After the Mary Ann example, inspect the two values and remove them when finished:

DEFINE 1
DEFINE 2
UNDEFINE 1 2

The DEFINE commands display the definitions. UNDEFINE removes them, so a later invocation without arguments can prompt for fresh values. For repeatable work, supply the complete intended argument list. Script settings such as VERIFY also persist after the file finishes. See Oracle's START reference and UNDEFINE reference.

Choose the correct execution command

Script execution and SQL-buffer commands
CommandWhat it does
START or @Reads and executes a script file, accepting arguments after its name.
LIST or LDisplays the SQL buffer without executing it.
RUNLists and executes the buffered SQL statement or PL/SQL block.
/Executes the buffer without first listing it.

RUN and slash do not accept a script filename and argument list. They also do not rerun every SQL*Plus command from the file. The buffer can still contain substitution references, so buffer execution does not disable substitution. See Oracle's LIST and RUN documentation.

Keep substitution separate from typed inputs

A script argument supplies text; its position does not declare a NUMBER, DATE, or VARCHAR2 parameter. The surrounding SQL determines how that text is interpreted. A numeric comparison should not depend on an implicitly converted quoted string. Date input needs an explicit conversion format rather than an assumption about the session's date format.

Use trusted demonstration values for this script. Double quotes around an argument neither escape arbitrary SQL content nor make substitution safe for untrusted application input. An application should pass such data through an appropriate bind-variable interface. Merely changing &1 to :1 does not create that interface.

Troubleshoot an unexpected run

If SQL*Plus cannot find the script, check the current directory and filename. If it uses an unexpected filter, inspect the numbered definitions and the argument order. If diagnostics are absent, check VERIFY separately from DEFINE: one controls substitution messages, the other substitution itself. A table-access error requires checking the connected schema, object names, and permissions.

The next lesson applies parameters within PL/SQL subprograms, where formal declarations define the inputs and outputs of procedures and functions.


SEMrush Software 3 SEMrush Banner 3