ORACLE游標 遞迴查詢 子查詢與批量更新示例

2021-05-23 07:26:26 字數 2129 閱讀 2845

--建立臨時表

create table tj_org_new

(deptcode       nvarchar2(255),

orgdesc            nvarchar2(255),

organtype      nvarchar2(255),

levelfact      number(6),

parentdeptcode nvarchar2(255)

)tablespace test

pctfree 10

initrans 1

maxtrans 255

storage

(initial 64k

minextents 1

maxextents unlimited

);--初始化臨時表資料

insert into tj_org_new(deptcode,orgdesc,organtype,levelfact,parentdeptcode) select deptcode,"org",organtype,level,parentdeptcode from "tj_org"

connect by prior deptcode=parentdeptcode 

start with parentdeptcode=1 order by level;

/*使用游標實現資料的批量邏輯處理

*/declare

varuserdeptcode varchar(255); --定義與表字段相同型別

cursor mycursor is --定義游標

select userdeptcode from tj_user;

my_record mycursor%rowtype;  --定義游標記錄型別

counter int :=0;

begin

open mycursor;  --開啟游標

if mycursor%isopen  then  --判斷開啟成功

loop --迴圈獲取記錄集

fetch mycursor into my_record; --獲取游標中的記錄

if mycursor%found then  --游標的found屬性判斷是否有記錄

--進行實際的業務處理begin

if my_record.userdeptcode=90033751 then --網省一級使用者更新

update tj_user set userorgcode=90033751 where userdeptcode=90033751;

dbms_output.put_line(my_record.userdeptcode||'a');

else --非網省一級使用者更新

update tj_user set userorgcode=

(select deptcode from (select deptcode,orgdesc,organtype,parentdeptcode,levelfact from tj_org_new

connect by prior parentdeptcode=deptcode 

start with deptcode=my_record.userdeptcode

order by levelfact) where organtype=1 and levelfact=2)

where (userdeptcode<>90033751) and

(userdeptcode in (select deptcode from tj_org_new

connect by prior parentdeptcode=deptcode 

start with deptcode=my_record.userdeptcode)); 

dbms_output.put_line(my_record.userdeptcode||'b');         

end if;

--進行實際的業務處理end

else

exit;

end if;

end loop;

else

dbms_output.put_line('游標沒有開啟');

end if;

close mycursor;

end;

游標 遞迴 查詢 客戶 子客戶 查詢財務信用

use crm01 go object storedprocedure dbo account3yearstrade script date 07 15 2015 08 34 37 set ansi nulls on goset quoted identifier on goalter proced...

oracle遞迴查詢子節點

通過子節點向根節點追朔.select from persons.dept start with deptid 76 connect by prior paredeptid deptid 通過根節點遍歷子節點 不包含根節點 select from persons.dept start with par...

oracle遞迴查詢子節點

通過子節點向根節點追朔.select from persons.dept start with deptid 76 connect by prior paredeptid deptid通過根節點遍歷子節點 不包含根節點 select from persons.dept start with pare...