oracle呼叫子儲存過程 游標迴圈例項

2022-05-06 04:06:09 字數 3001 閱讀 1393

一,有子節點的部門的子節點的排序,呼叫子儲存過程

create or replace procedure "pro_init_sort" as

cursor cur_department_all is select * from tbl_department;

var_count number ;

var_out_count number := 0;

begin

for department_row in cur_department_all loop

select count(1) into var_count from tbl_department where unit_id = department_row.unit_id and department_supercode = department_row.department_code ;

--上述查詢的是有所有的子節點的部門

if var_count != 0 then

--dbms_output.put_line(department_row.unit_id||'-'||department_row.department_code||'-'||var_count);

pro_init_department_sort(department_row.unit_id , department_row.department_id) ;

end if ;

var_out_count := var_out_count + 1 ;

end loop ;

dbms_output.put_line('總數:'||var_out_count);

end pro_init_sort ;

create or replace procedure "pro_init_department_sort"

( unit_id in number ,

super_code in number

)-- 初始化tbl_department表的department_sort欄位 以同department_supercode方式查詢使用rownum值更新department_sort欄位

as -- cursor cur_department is select * from tbl_department where unit_id = target_unit_id and department_supercode = target_supercode order by department_sort asc;

cursor cur_department is select rownum rn , d.* from tbl_department d where unit_id = unit_id and department_supercode = super_code;

begin

for department_row in cur_department loop

update tbl_department set department_sort = department_row.rn where department_id = department_row.department_id ;

-- null ;

end loop ;

-- null;

end pro_init_department_sort;

注意:count(1)和count(*)

在資料記錄都不為空的時候查詢出來結果上沒有差別的.

但當count(1)查詢的那列有空的時候空的是要被去掉的不記入統計中.這樣查詢出來的結果是不一樣的.

二,沒有子節點的排序

create or replace procedure "int_sort_n" as

p_out number;

p_count number:=0;

cursor cur_department is select t.unit_id from tbl_department t group by t.unit_id order by t.unit_id;

begin

for dep_row in cur_department loop

select count(1) into p_out from tbl_department t where t.unit_id = dep_row.unit_id and t.department_supercode = 0;

dbms_output.put_line('uid--'||dep_row.unit_id||'--部門--'||p_out);

int_deparment_sort(dep_row.unit_id);

p_count := p_count + 1;

end loop;

dbms_output.put_line('總數:'||p_count);

end;

create or replace procedure int_deparment_sort(unit_id2 in number) as

cursor department_all is select rownum rn,t.* from tbl_department t where t.unit_id = unit_id2 and t.department_supercode = 0 order by t.department_id;

begin

for dep_row in department_all loop

--dbms_output.put_line('---'||unit_id2);

update tbl_department t set t.department_sort = dep_row.rn where t.department_id = dep_row.department_id;

end loop;

end int_deparment_sort;

oracle儲存過程呼叫游標例子

1 首先你需要建立乙個包,並定義你返回的游標的型別 儲存過程 create or replace package test pkg is 定義游標 type t cur is ref cursor 儲存過程宣告 procedure test proc p cur in out t cur end t...

oracle儲存過程,游標

oracle儲存過程,游標 2010 07 07 13 01 create or replace procedure p tb task log is 功能 插入任務到任務日誌表 v task start date date v task end date date v sql code numbe...

oracle 儲存過程 游標

create or replace procedure exception3 as 使用者自定義異常 e too high sal exception 宣告自定義異常 v sal employees.salary type begin select salary into v sal from em...