日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

游标、过程、函数、包

發(fā)布時(shí)間:2025/3/8 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 游标、过程、函数、包 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?一)游標(biāo):
??????????? 1)每次聲明、打開、推進(jìn)、關(guān)閉游標(biāo)
declare
cursor c is select .........;--聲明
begin
open c;--打開
loop
exit when..........;
fetch c into .........;--推進(jìn)
......
.....
end loop;
close c;--關(guān)閉
end;
???????????? 2)使用游標(biāo)變量更加簡潔
declare
cursor c is select..........;聲明
begin
for rec_c in c loop????? --打開、推進(jìn)、關(guān)閉 全包括
exit when......
end loop;
end;

???????????? 3)動態(tài)游標(biāo);

???????????? declare
type c_ref is ref cursor;??? ---------------------聲明動態(tài)游標(biāo)
v_c_ref c_ref;????????????????? -----------------------定義一個動態(tài)游標(biāo)
type rec is record(????????? ------------------------聲明記錄
id?? sun_department.id%type,??????????????????????
department? sun_department.department%type);
v_rec rec;????????????????????????????? --------------------定義一個記錄;
begin
open v_c_ref for????????????????????????? --------------------打開動態(tài)游標(biāo)并付集合值
select id,department from sun_department where id<100;
fetch v_c_ref into v_rec;???????????????????????????????? -------------將游標(biāo)起到記錄中
while v_c_ref%found loop?????????????????????????????? ----------------------只要游標(biāo)中有值就一直輸出;
dbms_output.put_line(v_rec.id||'?? '||v_rec.department);
fetch? v_c_ref into v_rec;???????????????????????????????? ---------------------游標(biāo)的取出值放到記錄中;
end loop;?????????????????????????????????????????????????
close v_c_ref;
end;

?

二)過程

? create or replace? procedure???? test_p(id in number,name? out? varchar2)----參數(shù)in? out??? in out

??? is

????? i number:=0;????????????????????????????????? -----------------------------------------------無declare?? 為局部變量;

????? cursor? c is? select? sun.name from sun where sun.id=id;

????? begin???????????????????????????????????????????????

????? for v_c in c???????????????????????????????????? ----------------------------------------------------------游標(biāo)變量;

???? ?loop

???????? name:=v_c.name;

??????????-- dbms_output.put_line('name'||name);??

????? end loop;

?????? end test_p;?????????????????????????????????????????

三)函數(shù)

?????? create? or replace function test_f(id in number,name out varchar2)??

???????? return? ?number? is?????????????????????????????????????? ----------------------------------------------return? 類型

?????????? i?? number:=0;?

??????????? val?? number;

??????????? cursor????c? ?is? select s.id?? ?from sun.department? s?? where id<10 for update of id;

?????????? begin

????????????? open c;

?????????????? loop

?????????????? fetch c into val;

??????????????? if (c%rowcount<>0)then

??????????????? update?? sun_department set?? id:=100? where?? current of? c;

?????????????? else

??????????????????? update?? sun_department set?? id:=1 where current of?? c;

??????????????? end? if;

??????????????????i:=c%rowcount

??????????????? end loop;

end;

?

?

????????? end;

?

四)包

?????
create or replace package pack_test??????? --package specification start--
is
?? type get_test_cursor is ref cursor;????? --定義動態(tài)游標(biāo)
?? function getcur return get_test_cursor;? --函數(shù)聲明【函數(shù)返回類型為動態(tài)游標(biāo)】
end pack_test;????????????????????????????
/????????????????????????????????????????? --package specification? end--

create or replace package body pack_test?? --package body start --
is
? function getcur return get_test_cursor
? is
??? mycur get_test_cursor;???????????????? --用動態(tài)游標(biāo)定義變量
? begin
??? open mycur???????????????????????????? --打開并與具體數(shù)據(jù)關(guān)聯(lián)
??? for select * from? sun_department;
??? return mycur;????????????????????????? --返回游標(biāo)
? end getcur;
end pack_test;
/????????????????????????????????????????? --package body end --

declare????????????????????????????????? -- package test start --
??? testcur pack_test.get_test_cursor;
??? temp? sun_department%rowtype;??????????????????? --記錄變量定義
? begin
??? testcur := pack_test.getcur();?????? --方法調(diào)用
??? loop
????? fetch testcur? into temp;
???????? dbms_output.put_line(temp.id || ' : ' ||? temp.department);
????? exit when testcur%notfound;
??? end loop;
??? close testcur;
end;
/??????????????????????????????????????? -- package test end --

轉(zhuǎn)載于:https://www.cnblogs.com/sunxiangfu/archive/2008/08/27/4261150.html

總結(jié)

以上是生活随笔為你收集整理的游标、过程、函数、包的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。