Is there a way to set the parameter default value in sqlplus script without user input?
For example, I have SQL script sessions.sql:
SET VERIFY OFF
SET TERMOUT OFF
DEFINE uname = '& 1'
COLUMN search_uname new_value search_uname
SELECT CASE WHEN '& uname' = '' THEN '%' ELSE UPPER ('& uname') END AS search_uname
FROM dual;
SET TERMOUT ON
SELECT sid, serial, username FROM v $ session WHERE username LIKE '& search_uname';
And I want to call it from sqlplus as follows:
SQL> @sessions
Enter value for 1:
SID SERIAL # USERNAME
---------- ---------- ------------------------------
56 20577 CONTEXT
.....
236 rows selected.
SQL> @sessions ""
SID SERIAL # USERNAME
---------- ---------- ------------------------------
56 20577 CONTEXT
.....
236 rows selected.
SQL> @sessions SDE
SID SERIAL # USERNAME
---------- ---------- ------------------------------
113 56675 SDE
165 64881 SDE
.....
43 rows selected.
SQL>
I can pass an empty parameter value when I am asked to enter it, or I can pass an empty parameter after the script name through ". But this behavior is very annoying. Some IF DEFINED value" & 1 "will be very useful.
Do you have any tips or tricks on how this should be achieved in order to apply the WHERE clauses in sqlplus script wheter parameter is defined or not without unnecessary user interaction?
Decision
According to an article related to Martin I, the previous script was modified, which will work without the need for parameter values:
SET VERIFY OFF
SET TERMOUT OFF
column 1 new_value 1
SELECT '' "1" FROM dual WHERE ROWNUM = 0;
define uname = '& 1'
SET TERMOUT ON
SELECT sid, serial #, username FROM v $ session
WHERE username LIKE UPPER (DECODE ('& uname', '', '%', '& uname'));
undefine 1
adrive
source share