0

In an Oracle sqlplus script, I can use single script parameters as

set serveroutput on
define arg = '&1'
begin
    dbms_output.putline('&arg.');
end;
/

And in a Unix shell, I can iterate over given script arguments and pass that to an sqlplus script with

for x in "$@"; do
    sqlplus user/passwd@sid @script.sql "$x"
done

Is there a similar way in Oracle sqlplus? Something like

for arg in &* loop
    dbms_output.putline('&arg.');
end loop;

where arg would take values of &1, &2, &3, ... This means, when I call

$ sqlplus user/passwd@sid @script.sql a b c

I would see as output

a
b
c
5
  • 1
    you can do this using PL/SQL inside sqlplus. Is it ok for you ?? Commented Aug 20, 2021 at 9:51
  • I don't understand, but if there's a way, I would be interested. Commented Aug 20, 2021 at 9:56
  • Just to be clear. The list of arguments might contain any number of variables , and they are split by a whitespace . Am I correct ? Commented Aug 20, 2021 at 9:58
  • @RobertoHernandez I updated the question, maybe it's clearer now. Commented Aug 20, 2021 at 11:20
  • I post an answer. Let me know if it covers what you are looking for Commented Aug 20, 2021 at 11:21

1 Answer 1

1

Let's see if this is what you are looking for.

  1. We have an undetermined list of parameters
  2. All of them are split by whitespace ( as it were a list on bash input parameters )
  3. Do something with all of them, independently of the number of parameters
  4. In order for sqlplus to deal with this, all of the parameters must be enclosed with double quotes, so that the code will interpret them as only one parameter.

Script

set serveroutput on size unlimited
declare
vstring  varchar2(4000) := '&1';
vcounter pls_integer;
vrecord  varchar2(20);
BEGIN
vcounter := regexp_count( vstring , ' ') + 1;
for var in 1..vcounter 
loop
    if var = 1 
    then
        vrecord := regexp_substr( vstring, '[^ ]+', 1 , 1 );
        dbms_output.put_line(vrecord);
    elsif var > 1 and var <= vcounter 
    then 
        vrecord := regexp_substr( vstring, '[^ ]+', 1 , var );
        dbms_output.put_line(vrecord); 
    end if;
end loop;   
end;
/

I have this script stored as iteration.sql. So, let's run it with a set of parameters

SQL>  @iteration.sql "2"
old   2: vstring  varchar2(4000) := '&1';
new   2: vstring  varchar2(4000) := '2';
2

PL/SQL procedure successfully completed.

SQL> @iteration.sql "2 m3 88 99 ajkd8 0 88 aa"
old   2: vstring  varchar2(4000) := '&1';
new   2: vstring  varchar2(4000) := '2 m3 88 99 ajkd8 0 88 aa';
2
m3
88
99
ajkd8
0
88
aa

PL/SQL procedure successfully completed.
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, this is a manual approach, but not really what I expected. Sorry for being not clear enough upfront.
@"Olaf Dietsche" , can you put a real example ? It is not very clear to me what you are expecting. You have a set of variable input parameters. Where are they coming from ?? are they always 3 or can be 1 or 2 or n ? your script runs with three or more input parameters and you want a line for each one ?
Printing the parameters is just an example, this could be any function or procedure to call with this parameter. And this could be any number of parameters, not just three. Sometimes it's just one parameter, sometimes many more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.