Arvind Lairenjam
Arvind Lairenjam

Reputation: 981

In oracle 10g, how do I accept user input in a loop?

I have the following pl/sql block:

BEGIN
  FOR I IN 1 .. 5
  LOOP
    INSERT INTO TEST_TABLE VALUES('&slno',SYSDATE);
  END LOOP;
END;

When I executed the above block it is taking only one input. But it is supposed to take 5 input in total. What is missing in my code? Can any one help me please?

Upvotes: 3

Views: 2618

Answers (2)

Nick Krasnov
Nick Krasnov

Reputation: 27251

Only after all substitution variables are parsed and substituted(by the client) a final command, query or pl/sq block is sent to the database engine for execution. To that end, it is not possible to repeatedly prompt in a loop. So you might, as an alternative to the @be here now answer, rewrite your pl/sql block as follows:

SQL> set verify off

SQL> declare
  2    type T_variables is table of varchar2(11);
  3    l_varlist T_variables;
  4  begin
  5    select v
  6      bulk collect into l_varlist -- Assume that there are not so many of them
  7      from ( select '&var1' as v from dual union all
  8             select '&var2' from dual union all
  9             select '&var3' from dual
 10           ) ;
 11  
 12    for i in l_varlist.first..l_varlist.last
 13    loop
 14      insert into test_table(col1, col2)
 15        values(l_varlist(i), sysdate);
 16    end loop;
 17  end;
 18  
 19  /
Enter value for var1: value #1
Enter value for var2: value #2
Enter value for var3: value #3

PL/SQL procedure successfully completed.

SQL> commit;

Commit complete.

SQL> select * 
  2    from test_table;

COL1        COL2                                                                
----------- ---------                                                           
value #1    07-DEC-12                                                           
value #2    07-DEC-12                                                           
value #3    07-DEC-12  

Upvotes: 1

Kirill Leontev
Kirill Leontev

Reputation: 10941

It is a substitution variable - an sql*plus feature - and it doesn't work this way. You have to call undefine &slno or accept slno ... to make it take another input, but these are also sqlplus, not pl/sql commands, so you won't be able to invoke them in a loop,

Probably the only thing you can do here is

INSERT INTO TEST_TABLE VALUES('&slno1',SYSDATE);
INSERT INTO TEST_TABLE VALUES('&slno2',SYSDATE);
INSERT INTO TEST_TABLE VALUES('&slno3',SYSDATE);
INSERT INTO TEST_TABLE VALUES('&slno4',SYSDATE);
INSERT INTO TEST_TABLE VALUES('&slno5',SYSDATE);

update: Fortunately, you can work this around by generaing a list of separate sequential statements accepting independent inputs:

22:38:59 @> conn system/sys@oars_sandbox
Connected.
22:39:01 SYSTEM@oars_sandbox> @s:\test
Enter value for var1: a
Enter value for var2: b
Enter value for var3: c
22:39:06 SYSTEM@oars_sandbox> commit;
22:39:11 SYSTEM@oars_sandbox> select * from test_table;

COL1       COL2
---------- -------------------
a          07.12.2012 22:39:10
b          07.12.2012 22:39:11
c          07.12.2012 22:39:11
22:39:17 SYSTEM@oars_sandbox> get s:\test
  1  set echo off
  2  set define off
  3  set termout off
  4  set feedback off
  5  set timing off
  6  spool s:\123.sql
  7  begin
  8    for i in 1 .. 3 loop
  9      dbms_output.put_line('insert into test_table values(''&var'||i||''', sysdate);');
 10    end loop;
 11  end;
 12  /
 13  spool off
 14  set define "&"
 15  set termout on
 16* @s:\123.sql
22:39:24  17  .
22:39:58 SYSTEM@oars_sandbox> get s:\123.sql
  1  insert into test_table values('&var1', sysdate);
  2  insert into test_table values('&var2', sysdate);
  3* insert into test_table values('&var3', sysdate);
22:40:04 SYSTEM@oars_sandbox>

Upvotes: 2

Related Questions