读取" n"的数组来自用户输入的元素

时间:2014-03-29 19:17:35

标签: plsql

如何动态读取n个元素的数组并在plsql中显示元素。

下面是我的代码(我是plsql编程的新手)

set serveroutput on
set verify on
declare
   type myarray is table of number index by binary_integer;
   x myarray;
   i pls_integer;
   n number;

begin
   -- populate array
   dbms_output.put_line('Enter number of array elements');
   n := &n;
   dbms_output.put_line('Enter elements one by one');
   for i in 1..n loop
  dbms_output.get_lines(&&x(i),n);
   end loop;
   i :=0;

   -- print array
   loop
           i := i + 1;
           begin
                   dbms_output.put_line(x(i));
           exception
                   when no_data_found then exit;
           end;
   end loop;

   end;
   /
  quit;

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,想尝试一下。

你可以通过一种野蛮的方法来做到这一点,这种方法包括在你想要为数组添加值时使用shell调用客户端(即sqlplus)。我写了一个小包,用于将数组的值存储在表中。然后,您可以更改存储值的方式,但原则将保持不变:

 -- this package uses "execute immediate" statements for everything
 -- because it assumes the temporary table T_MY_ARRAY can be non-existent.
 create or replace package my_array
  authid current_user
is
  TYPE a_TBL_Number IS TABLE OF Number INDEX BY BINARY_INTEGER; 
  procedure init;
  procedure add(v in number);
  procedure display;
  function to_var return a_TBL_Number;
end my_array;
/
create or replace package body my_array
is     
  procedure init is
  -- create table if needed, then make sure its empty;
  begin
    begin
      execute immediate 'create table T_MY_ARRAY(c1 number)';
    exception when others then
      -- dbms_output.put_line(sqlerrm);
      -- we're never sure the temp. table already exists. So create it and catch if existing.
      null;
    end;
    execute immediate 'truncate table T_MY_ARRAY';
  end init;

  procedure add(v in number) is
  -- add new value
  begin
    execute immediate 'insert into T_MY_ARRAY (c1) values ('||v||')';
  end add;

  function to_var return a_TBL_Number is
  -- hand out an array with the values
    t_TBL_n a_TBL_Number; 
  begin
    execute immediate 'select c1 from T_MY_ARRAY ' bulk collect into t_TBL_n;
    return t_TBL_n;
  end to_var;

  procedure display is
    t_TBL_n a_TBL_Number; 
  begin
    t_TBL_n:=my_array.to_var();
    for i in 1..t_TBL_n.count loop
      dbms_output.put_line(t_TBL_n(i));
    end loop;
  end display;

end my_array;
/

然后,你可以在shell中调用它(这里是旧的ksh):

read SQL_CONNECT?"Enter your SQL connection string:"
read n?"Enter number of array elements:"
# first initialize the array:
sqlplus -s ${SQL_CONNECT} <<_EOF
  set serveroutput off
  set termout off
  set feedback off
  begin
    my_array.init;
  end;
/
_EOF

# then loop on the given number of elements
typeset -i10 i=0
while [[ ${i} -lt ${n} ]]
do
  i=i+1
  echo iter:${i}
  read MY_VALUE?"Enter elements one by one:"
  sqlplus -s ${SQL_CONNECT} <<_EOF
  set serveroutput off
  set termout off
  set feedback off
  begin
    my_array.add(${MY_VALUE});
  end;
/
commit;
_EOF
done

# at the end, use stored values to display result:
  sqlplus -s ${SQL_CONNECT} <<_EOF
  set serveroutput on
  set feedback off
  begin
    dbms_output.put_line('---------------');
    dbms_output.put_line('your input was:');
    my_array.display;
  end;
/
_EOF