从引用游标中获取批量收集时出现不一致的数据类型错误
Posted
技术标签:
【中文标题】从引用游标中获取批量收集时出现不一致的数据类型错误【英文标题】:Getting inconsistent data type error while fetching bulk collect from ref cursor 【发布时间】:2017-09-26 09:59:17 【问题描述】:这是我的代码
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】:您缺少变量的声明,但似乎您可以将其简化为:
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;
【讨论】:
【参考方案2】:你能扩展你的编码吗... 是 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;
【讨论】:
每当您使用动态查询时,您都应该将其分配到变量中,以便分配,以便 --- v_stmt:= 'select * from ''||v_gs_tab_name||'' where ' '||k_col_name||'' 在 (''||nxt_mid_k_val||'') 和 (''||max_k_val||'')'' 之间从 dual' Dbms_output.put_line(v_stmt) 到 v_stmt; v_insert:='插入到'||v_tab_name||'值'||v_stmt||''; dbms_output.put_line(v_insert);* 希望对你有所帮助。以上是关于从引用游标中获取批量收集时出现不一致的数据类型错误的主要内容,如果未能解决你的问题,请参考以下文章
将 std::vector 与结构一起使用时出现不完整的类型错误