0

为了从用户本身接受数组变量,我正在寻找类似下面的东西,我知道下面的代码是错误的,但这只是为了给出我的要求的想法。

DECLARE 
  type names_array IS VARRAY(5) OF VARCHAR2(10); 
  names names_array;
BEGIN 
  FOR i in 1..5 LOOP
     accept names(i);
  END LOOP;

  FOR j in 1 .. 5 LOOP 
     dbms_output.put_line(names(j)); 
  END LOOP; 
END; 
/
4

1 回答 1

1

It will be difficult to implement using PL/SQL.But we could using substitution variables in SQL Plus.

I create two sql scripts: the first is main.sql and another is script_insert.sql:

[oracle@db input]$ cat main.sql
accept colu prompt "Please enter value, enter 'done' when no more values: "
set term off verify off
column script new_value v_script
select case '&colu'
        when 'done' then ''
        else '@script_insert &colu'
        end as script
from dual;
set term on
@&v_script.
[oracle@db input]$ 

[oracle@db input]$ cat script_insert.sql
insert into array_table values ('&1');
@main
[oracle@db input]$ 

Next we should create a table other than using an array:

SQL> create table array_table(colu varchar2(30));

Table created.

SQL>

Now, we could execute it:

SQL> @main
Please enter value, enter 'done' when no more values: A

1 row created.

Please enter value, enter 'done' when no more values: B

1 row created.

Please enter value, enter 'done' when no more values: Hello

1 row created.

Please enter value, enter 'done' when no more values: "Hello World"

1 row created.

Please enter value, enter 'done' when no more values: done
SQL> select * from array_table;

COLU
------------------------------
A
B
Hello
Hello World

SQL>

We got it,you should using a table other than array because only PL/SQL support it.And you shouldn't using substitution variables in loop! The finally, why don't you implement this by C/Python/Java in your program?If so, you'll be more relaxed.

Refer:How to create a menu in SQLPlus or PL/SQL

于 2013-08-16T08:53:47.420 回答