oracle 動態游標

2021-08-30 14:25:09 字數 1983 閱讀 9104

今天寫了個動態游標 使用傳入引數

關於游標分類以及用法:

思路就是先拼好sql 然後開動態游標

oralce10g也支援正規表示式 呵呵 剛剛好可以實現動態傳入引數

procedure tj_pda_testdata(v_indicator in varchar) is

type rc is ref cursor;

l_cursor rc;

l_sqlstr varchar(2000);

l_sqlstr_new varchar(2000);

l_raw_use raw_pda_testdata_tj%rowtype;

l_date varchar(20);

begin

select to_char(sysdate, 'yyyy-mm-dd') into l_date from dual;

l_sqlstr := 'select a.region_id,

sum(count) totle,

''$v_date'' creatdate,

''$v_indicator'' indicator,

sum(case lev when 1 then count else 0 end) lev1,

sum(case lev when 2 then count else 0 end) lev2,

sum(case lev when 3 then count else 0 end) lev3,

sum(case lev when 4 then count else 0 end) lev4,

sum(case lev when 5 then count else 0 end) lev5,

sum(case lev when 6 then count else 0 end) lev6

from (select zhiju_id region_id, $v_indicator lev, count(1) count

from raw_pda_testdata_use t

where creatdate like ''$v_date%''

and zhiju_id != ''-1''

group by zhiju_id, $v_indicator) a

group by region_id';

select regexp_replace(l_sqlstr, '\$v_date', l_date) into l_sqlstr from dual;

select regexp_replace(l_sqlstr, '\$v_indicator', v_indicator) into l_sqlstr_new from dual;

open l_cursor for l_sqlstr;

loop

fetch l_cursor

into l_raw_use;

exit when l_cursor%notfound;

begin

insert into raw_pda_testdata_tj

values

(l_raw_use.region_id,

l_raw_use.creatdate,

l_raw_use.indicator,

l_raw_use.totle,

l_raw_use.lev1,

l_raw_use.lev2,

l_raw_use.lev3,

l_raw_use.lev4,

l_raw_use.lev5,

l_raw_use.lev6);

commit;

exception

when others then

rollback;

end;

end loop;

close l_cursor;

exception

when others then

null;

end; -- procedure

oracle動態游標

declare v col1 varchar2 254 v col2 varchar2 254 v sql varchar2 1024 type my cursor is ref cursor v cur my cursor begin v sql select 1,2 from dual wher...

Oracle動態游標入門

說明 下面的儲存過程在oracle817下全部通過測試,編譯和執行均是正確的 一 最簡單的乙個動態游標 create or replace procedure test cur isstrsql1 varchar 1000 type tcur is ref cursor cur tcur ac wh...

Oracle動態游標入門

一 最簡單的乙個動態游標 create or replace procedure test cur isstrsql1 varchar 1000 type tcur is ref cursor cur tcur ac where varchar2 100 ac varchar2 100 begin ...