PL / SQL游标和不同大小的数组

时间:2016-08-31 08:55:06

标签: arrays oracle plsql cursor

我想将光标的内容存储在关联的数组中(通过binary_integer表索引)。但是在同一个数组中,我还想存储一个附加变量,比如一个布尔值。

我的光标每行有n个元素,数组被定义为有n + 1个元素(n与cursorelements具有相同的%类型),最后一个是布尔值。

我想要的是这样的事情

for cursorrow in cursor(...) 
loop
  array(row i) := cursorrow, boolean_variable;
end loop;

| 1 | 2 | ... | N | N + 1 | := | 1 | 2 | ... | n |,| 1 |

不幸的是我无法让它发挥作用。 有谁知道怎么做?

3 个答案:

答案 0 :(得分:0)

由于记录类型与表结构相差1个字段(即布尔值),因此不能将游标行分配给1个赋值语句中的记录类型。您需要分别为每个表列执行此操作:

for cursorrow in cursor(...) 
loop
  array(row i).col1 := cursorrow.col1;
  array(row i).col2 := cursorrow.col2;
  ...
  array(row i).coln := cursorrow.coln;
  array(row i).boolean_variable := some_boolean_value;
end loop;

答案 1 :(得分:0)

如评论中所述,您可以创建记录并执行此操作。您可以使用以下程序来满足您的要求。

create or replace procedure proc_test
as

  cursor cur_tab is 
   select a  --have selected only 1 column..you can choose many
    from test;

  TYPE  var_temp  IS TABLE OF cur_tab%ROWTYPE INDEX BY PLS_INTEGER;
  v_var              var_temp; 

 /**You can add columns selected in you cursor here in your record****/
 TYPE abc IS RECORD
  (
   id          varchar2(100), 
   orig_name   boolean      
   );

  TYPE  xx IS TABLE OF abc ;

  -- initialization of record
  v_xx    xx := xx() ;  

  t   boolean:=TRUE;    

 begin


  open cur_tab;

   fetch cur_tab bulk collect into v_var;

  close cur_tab;

  for i in 1..v_var.count
   loop

     v_xx.extend;

     v_xx(i).id := v_var(i).a;
     v_xx(i).orig_name := t;

      dbms_output.put_line (v_xx(i).id ||'----'||sys.diutil.bool_to_int(v_xx(i).orig_name));

     ---OR

      dbms_output.put_line (v_xx(i).id ||'----'||case when v_xx(i).orig_name = true then 'TRUE' ELSE 'FALSE' end );

   end loop;

  exception
  when others then 
  null;

 end;   

呼叫:

execute proc_test;

答案 2 :(得分:0)

如何定义由%rowtype记录和布尔值组成的复合记录类型?

测试设置:

create table demo_table
( some_id integer primary key
, some_name varchar2(30) not null unique
, some_type varchar2(10) not null );

insert all
    into demo_table values (1, 'One', 'X' )
    into demo_table values (2, 'Two', 'Y' )
    into demo_table values (3, 'Three', 'Z' )
select * from dual;

测试: (编辑:在循环中添加了dbms_output条消息)

declare
    subtype demo_rectype is demo_table%rowtype;

    type demo_rec is record
    ( details demo_rectype
    , somecheck boolean );

    type demo_tt is table of demo_rec index by pls_integer;
    t_demo demo_tt;

    cursor c_demo is select * from demo_table;
begin
    for r in c_demo
    loop
        t_demo(c_demo%rowcount).details := r;
        t_demo(c_demo%rowcount).somecheck := dbms_random.value < 0.5;

        dbms_output.put_line
        ( t_demo(c_demo%rowcount).details.some_name || ': ' ||
          case t_demo(c_demo%rowcount).somecheck
              when true then 'TRUE'
              when false then 'FALSE'
              else 'NULL'
          end );
    end loop;

    dbms_output.new_line();
    dbms_output.put_line('Array t_demo contains ' || t_demo.count || ' items.');
end;

输出:

One: FALSE
Two: FALSE
Three: TRUE

Array t_demo contains 3 items.