Oracle動態游標入門

2021-04-14 15:07:31 字數 2312 閱讀 7377

說明:下面的儲存過程在oracle817下全部通過測試,編譯和執行均是正確的

一、最簡單的乙個動態游標:

create or replace procedure test_cur

isstrsql1 varchar(1000);

type tcur is ref cursor;

cur tcur;

ac_where varchar2(100);

ac varchar2(100);

begin

ac_where := '(52228,52230)';

open cur for 'select bill_id from bill_main where bill_id in '|| ac_where;

loop

fetch cur into ac;

exit when cur%notfound;

dbms_output.put_line(ac);

end loop;

close cur;

end test_cur;

二、動態游標中使用動態的sql語句並執行:

create or replace procedure test_cur

(p_orgid_wins string)is

strsql1 varchar2(1000);

type my_curtype is ref cursor;

cur_1 my_curtype;--指示cur_1的型別為my_curtype,而my_curtype是游標型別

ac_where varchar2(100);

ac varchar2(100);

begin

ac_where := '(52228,52230)';

open cur_1 for 'select bill_id from bill_main where bill_id in '|| ac_where;--開啟動態游標

loop

fetch cur_1 into ac;

exit when cur_1%notfound;

strsql1:='delete bill_main where bill_id='||ac;

dbms_output.put_line(strsql1);

dbms_output.put_line(ac);

execute immediate strsql1;--執行乙個動態的sql語句

commit;

end loop;

close cur_1;

end test_cur;

三、動態游標中執行動態dql語句:

create or replace procedure test_cur

(p_orgid_wins string)is

strsql1 varchar2(1000);

strsql2 varchar2(1000);

type my_curtype is ref cursor;

cur_1 my_curtype;--指示cur_1的型別為my_curtype,而my_curtype是游標型別

ac_where varchar2(100);

t_to_orgid number;

t_bill_id number;

begin

ac_where := '(98978,98980)';

strsql1:='select bill_id,to_orgid from bill_main where bill_id in '|| ac_where;

dbms_output.put_line(strsql1);

open cur_1 for strsql1;--開啟動態游標

loop

fetch cur_1 into t_bill_id,t_to_orgid;

exit when cur_1%notfound;

dbms_output.put_line('t_to_orgid='||t_to_orgid);

strsql2:='delete bill_main where bill_id='||t_bill_id;

strsql2:=strsql2|| 'and start_no='||'16506';

dbms_output.put_line(strsql2);

dbms_output.put_line(t_bill_id);

execute immediate strsql1;--執行乙個動態的sql語句

commit;

end loop;

close cur_1;

end test_cur; 

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 ...

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 動態游標

今天寫了個動態游標 使用傳入引數 關於游標分類以及用法 思路就是先拼好sql 然後開動態游標 oralce10g也支援正規表示式 呵呵 剛剛好可以實現動態傳入引數 procedure tj pda testdata v indicator in varchar is type rc is ref c...