您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

动态RefCursor的定义与调用

2025/11/19 16:15:58发布33次查看
1.定义: type ref_cur is ref cursor; 2.动态cursor作为out参数 存储过程的实现 举个最简单的例子,根据table name动态获取cursor procedure p_get_cur(i_table_name in varchar2, o_ref_cur out ref_cur) as begin if upper(i_table_name) = 't_bank_accou
1.定义:
type ref_cur is ref cursor;
2.动态cursor作为out参数 存储过程的实现
举个最简单的例子,根据table name动态获取cursor
procedure p_get_cur(i_table_name in varchar2, o_ref_cur out ref_cur) as
begin
if upper(i_table_name) = 't_bank_account' then
open o_ref_cur for
select bank_account
from t_bank_account
where bank_account is not null;
elsif upper(i_table_name) = 't_fb_payment_jp' then
open o_ref_cur for
select bank_account
from t_fb_payment_jp
where bank_account is not null;
end if;
end p_get_cur;
3.调用,用项目中mask 口座番号的功能为例,跟大家分享下:
procedure p_bank_acco_mask(i_table_name in varchar2,
i_coulumn in varchar,
o_error_str in out varchar2,
o_result in out number,
m_has_exception in out boolean) as
v_update_sql varchar(800);
v_update_sql2 varchar(800);
i int := 1;
v_bank_account varchar2(50);
v_bank_account_new varchar2(50);
v_char varchar(1);
v_bank_account_curs ref_cur;
v_select_sql varchar2(800);
begin
o_result := pkg_ls_pub_code_cst.batch_result__success;
begin
p_get_cur(i_table_name,v_bank_account_curs);
savepoint point;
v_update_sql2 := '';
loop
fetch v_bank_account_curs into v_bank_account;
exit when v_bank_account_curs%notfound;
begin
for i in 1 .. length(v_bank_account) loop
v_select_sql:='select substr('||i_coulumn||','||i||',1) from '|| i_table_name ||' where '||i_coulumn||'='''||v_bank_account||''' and rownum v_char:=f_get_char(v_select_sql);
if v_char is null then
return;
end if;
if v_char='2' or v_char='3' then--1
v_char:='1';
elsif v_char='5' or v_char='6' then--4
v_char:='4';
elsif v_char='8' or v_char='9' then--7
v_char:='7';
end if;
v_bank_account_new:=v_bank_account_new||v_char;
end loop;
--should support bank_code='9900'
if i_table_name = 't_dt_account_jp' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( head_bank_code ' || ',' ||
'''9900''' || ',' || '''12340-01111281''' || ',' ||
v_bank_account_new ||' ) where' || i_coulumn ||'='''|| v_bank_account||'''';
elsif i_table_name = 't_cash_bank' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( cash_bank ' || ',' ||
'''9900''' || ',' || '''12340-01111281''' || ',' ||
v_bank_account_new ||' ) where' || i_coulumn ||'='''|| v_bank_account||'''';
elsif i_table_name = 't_cash_bank_log' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( cash_bank ' || ',' ||
'''9900''' || ',' || '''12340-01111281''' || ',' ||
v_bank_account_new ||' ) where' || i_coulumn ||'='''|| v_bank_account||'''';
elsif i_table_name = 't_agm_agent' then
v_update_sql := ' update ' || i_table_name ||
' set comm_account = ' || v_bank_account_new ||'' ||
' where comm_pay_bank_code ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set comm_pay_branch_code =''12340'', ' ||
' comm_account = ''01111281'' ' ||
' where comm_pay_bank_code = ''9900''';
elsif i_table_name = 't_shoshiharairirekijouhou_tbl' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( substr(bank_account_no,2,4) ' ||
',' || '''9900''' || ',' || '''12340-01111281''' ||
',' || v_bank_account_new ||' ) where' || i_coulumn ||'='''|| v_bank_account||'''';
elsif i_table_name = 't_nyushutukinrirekijouhou' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( substr(kouza_no,2,4) ' ||
',' || '''9900''' || ',' || '''12340-01111281''' ||
',' || v_bank_account_new ||' ) where' || i_coulumn ||'='''|| v_bank_account||'''';
elsif i_table_name = 't_account_apply_tbl' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( substr(account_no,1,4) ' ||
',' || '''9900''' || ',' || '''12340-01111281''' ||
',' || v_bank_account_new ||' ) where' || i_coulumn ||'='''|| v_bank_account||'''';
elsif i_table_name = 't_lsif_others' then
v_update_sql := ' update ' || i_table_name || ' set kouzaino = ' ||
v_bank_account_new ||'' || ' where bankcn ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set sitencnjyo =''123'', ' ||
' sitencnka = ''40'', ' ||
' kouzaino = ''01111281'' ' ||
' where bankcn = ''9900''';
elsif i_table_name = 't_lsif_getumatu_nenmatu_master' then
v_update_sql := ' update ' || i_table_name || ' set kouzaino = ' ||
v_bank_account_new ||'' || ' where bankcn ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set sitencn =''12340'', ' ||
' kouzaino = ''01111281'' ' ||
' where bankcn = ''9900''';
elsif i_table_name = 't_lsif_kaikei' then
v_update_sql := ' update ' || i_table_name || ' set wuzano = ' ||
v_bank_account_new ||'' ||
' where gankowumeyishouwucn ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set shiteyinmeyisyouwucn =''123'', ' ||
' wuzano = ''01111281'' ' ||
' where gankowumeyishouwucn = ''9900''';
elsif i_table_name = 't_lsif_idouextr' and i_coulumn = 'kouzaino_zen' then
v_update_sql := ' update ' || i_table_name ||
' set kouzaino_zen = ' || v_bank_account_new ||'' ||
' where ginkoucn_zen ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set shitencne_zen =''123'', ' ||
' shitencnge_zen = ''40'', ' ||
' kouzaino_zen = ''01111281'' ' ||
' where ginkoucn_zen = ''9900''';
elsif i_table_name = 't_lsif_idouextr' and i_coulumn = 'kouzaino_go' then
v_update_sql := ' update ' || i_table_name || ' set kouzaino_go = ' ||
v_bank_account_new ||'' ||
' where ginkoucn_go ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set shitencne_go =''123'', ' ||
' shitencnge_go = ''40'', ' ||
' kouzaino_go = ''01111281'' ' ||
' where ginkoucn_go = ''9900''';
elsif i_table_name = 't_diagnosis_charge_data' then
v_update_sql := ' update ' || i_table_name || ' set account_no = ' ||
v_bank_account_new ||'' || ' where bank_cn ''9900'' and '|| i_coulumn ||'='''|| v_bank_account||'''';
v_update_sql2 := ' update ' || i_table_name ||
' set branch_cn =''123'', ' ||
' account_no = ''01111281'' ' ||
' where bank_cn = ''9900''';
elsif i_table_name = 't_ife_monthly_getumatu_master' then
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( bankcn ' || ',' ||
'''9900''' || ',' || '''12340-01111281''' || ',' ||
v_bank_account_new ||' ) where '|| i_coulumn ||'='''|| v_bank_account||'''';
else
v_update_sql := ' update ' || i_table_name || ' set ' || i_coulumn ||
' = decode( bank_code ' || ',' ||
'''9900''' || ',' || '''12340-01111281''' || ',' ||
v_bank_account_new ||' ) where '|| i_coulumn ||'='''|| v_bank_account||'''';
end if;
v_bank_account_new:='';--clear data
execute immediate v_update_sql;
if v_update_sql2 is not null then
execute immediate v_update_sql2;
end if;
pkg_pub_scd_ci.p_batch_commit();
exception
when others then
rollback to point;
o_result := pkg_ls_pub_code_cst.batch_result__fail;
o_error_str := i_table_name || ',';
pkg_pub_scd_ci.p_log_error('p_bank_acco_mask ,failed to mask table:' ||
i_table_name || ',error info:' ||
sqlerrm || '-------update sql=' ||
v_update_sql);
m_has_exception := true;
end;
end loop;
close v_bank_account_curs;
end;
end p_bank_acco_mask;
动态创建cursor的函数原理同上,其他带参数的cursor具体可以参考下面的:
--procedure返回记录集:
----------------------声明一个package--------------
create or replace package pkg_test
as
typemyrctypeis ref cursor;
procedure get_r(p_id number,p_rc out myrctype); --package中声明名为get 的procedure(只有接口没内容)
end pkg_test;
-----------------声明package body,即上面package中的内容,包括procedure get---------------------
create or replace package body pkg_test
as
procedure get_r(p_id number,p_rc out myrctype)
is
sqlstr varchar2 (500);
begin
if p_id = 0 then
open p_rc for
select id, name, sex, address, postcode, birthday
from student;
else
sqlstr :=
'select id,name,sex,address,postcode,birthday
from student where id=:w_id'; --w_id是个参数,
--以下 p_rc是个ref cursor游标类型,而且是out型参数,即可返回一个记录集了。using p_id就是替换上面sql中:w_id值拉:)
open p_rc for sqlstr using p_id; 
end if;
end get;
end pkg_test;
--function返回记录集的例子,原理和上面相同,而是用function的return值来返回记录集。
函数返回记录集:
建立带ref cursor定义的包和包体及函数:
复制代码 代码如下:
create or replace
package pkg_test as
type myrctype is ref cursor;
function get_r(intid number) return myrctype;
end pkg_test;
/
create or replace
package body pkg_test as
--函数体
function get_r(intid number) return myrctype is
rc myrctype; --定义ref cursor变量
sqlstr varchar2(500);
begin
if intid=0 then
--静态测试,直接用select语句直接返回结果
open rc for select id,name,sex,address,postcode,birthday from student;
else
--动态sql赋值,用:w_id来申明该变量从外部获得
sqlstr := 'select id,name,sex,address,postcode,birthday from student where id=:w_id';
--动态测试,用sqlstr字符串返回结果,用using关键词传递参数
open rc for sqlstr using intid;
end if;
return rc;
end get;
end pkg_test;
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product