从ref游标获取批量收集时获取不一致的数据类型错误

时间:2017-09-26 09:59:17

标签: oracle plsql oracle10g

从ref cursor

获取批量收集时,我收到的数据类型错误不一致

这是我的代码

create or replace function test_func(c in int) return number as

 v_stmt varchar2(4000);
 v_insert varchar2(4000);
k_col_name varchar2(50);
v_gs_tab_name varchar2(100);
min_k_val varchar2(50);
max_k_val varchar2(50);
mid_k_val varchar2(50);
nxt_mid_k_val varchar2(50);
type l_cursor_type is ref cursor;
 l_cursor l_cursor_type;
 type t_source is table of rec_source;
  m_source t_source:=t_source();
 dat_typ varchar2(50);

 begin

 select DISTINCT data_type INTO dat_typ from all_tab_cols where column_name 
 in (SELECT cols.column_name FROM all_constraints cons, all_cons_columns 
 cols
 WHERE cons.constraint_type='P' and cols.table_name=+v_tab_name AND 
  CONS.OWNER='SOURCE'
   AND cons.constraint_name = cols.constraint_name AND cons.owner = 
   cols.owner);

     dml_str:='create or replace type dwbi_land.rec_source as object (id '||dat_typ||')';
      dbms_output.put_line(dml_str);
         execute immediate dml_str;
   dml_str:='create or replace type dwbi_land.t_source as table of rec_source';
   dbms_output.put_line(dml_str);
    execute immediate dml_str;

    begin 
       select 'select * from '||v_gs_tab_name
         ||' where '||k_col_name||' between ('||min_k_val||') and 
       ('||mid_k_val||')'
       into v_stmt
       from dual;

       dbms_output.put_line(v_stmt);

        execute immediate v_stmt bulk collect into m_source;

        v_insert:='insert into '||v_tab_name||' values '||v_stmt||''; 

        dbms_output.put_line(v_insert);

       open l_cursor for v_stmt;
        loop
        fetch l_cursor bulk collect into m_source;
        exit when l_cursor%NOTFOUND;
        m_source.extend;
       forall i in 1..m_source.count 
      execute immediate v_insert using m_source(i).id; 
    commit; 
    exit when m_source.count=0; 
  end loop; 

  select 'select * from '||v_gs_tab_name
         ||' where '||k_col_name||' between ('||nxt_mid_k_val||') and ('||max_k_val||')'
  into v_stmt
  from dual;

  dbms_output.put_line(v_stmt);

  v_insert:='insert into '||v_tab_name||' values '||v_stmt||'';

   dbms_output.put_line(v_insert);

  open l_cursor for v_stmt using 1; 
  loop 
    fetch l_cursor bulk collect into m_source;
    forall i in 1..m_source.count
      execute immediate v_insert using m_source(i).id;
    commit;
    exit when m_source.count=0;
  end loop;
end;

在fetch l_cursor中,我将收集到m_source部分 Ora-00932:数据类型不一致:预期 - 得到 - 欢迎任何建议

以前我无法发布整个代码,现在我发布了整个代码

2 个答案:

答案 0 :(得分:0)

您缺少变量声明,但似乎可以将其简化为:

begin 
  v_stmt := 'insert into ' || v_tab_name 
            || ' select id from '||v_gs_tab_name
            || ' where '||k_col_name||' between :1 and :2;

  dbms_output.put_line(v_stmt);

  execute immediate v_stmt using min_k_val, mid_k_val;
  execute immediate v_stmt using nxt_mid_k_val, max_k_val;
END;

答案 1 :(得分:-1)

你能扩展你的编码吗? wats v_stmt的数据类型?

示例代码:

begin
v_stmt:= 'select * from ''||v_gs_tab_name||'' where ''||k_col_name||'' between (''||nxt_mid_k_val||'') and (''||max_k_val||'')'' into v_stmt from dual'
Dbms_output.put_line(v_stmt);
V_insert:='insert into '||v_tab_name||' values '||v_stmt||'';
dbms_output.put_line(v_insert);

    open l_cursor for v_stmt using 1; loop fetch l_cursor bulk collect into m_source;

    forall i in 1..m_source.count

    execute immediate v_insert using m_source(i).id;

    commit;

    exit when m_source.count=0;

    end loop;

end;