2017-09-26 38 views
0

收集,同時獲取大部分來自裁判光標收集我得到不一致的數據類型錯誤獲取不一致的數據類型的錯誤,同時獲取大部分來自裁判光標

這裏是我的代碼

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; 

在獲取l_cursor散裝收集到m_source部分得到 Ora-00932:不一致的數據類型:預計 - 得到 - 歡迎任何建議

以前我無法發佈整個代碼,現在我已發佈整個代碼

回答

-1

你可以plz擴展你的編碼... 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; 
+0

嗨,現在我已經更新了我的代碼,請驗證 – Tilak

+0

只要您是使用動態查詢,您應該將它分配到變量中,以便分配,以便在(''|| k_col_name ||'')之間使用--- ** v_stmt:='select * from''|| v_gs_tab_name ||'' || nxt_mid_k_val ||'')和(''|| || max_k_val ||'')''從雙「 Dbms_output」轉換爲v_stmt。 PUT_LINE(v_stmt); V_insert:='insert into'|| v_tab_name ||'值'|| v_stmt ||''; dbms_output.put_line(v_insert); ***希望它能幫助你。 –

+0

hi @Tilak,我希望解決方案對你有用...... !!! –

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;