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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

oracle-扫盲贴:存储过程实现增删改查

發(fā)布時間:2023/12/9 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle-扫盲贴:存储过程实现增删改查 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原文引入:http://blog.csdn.net/yangzhawen/article/details/8617179

?

  

oracle-掃盲貼:存儲過程實現(xiàn)增刪改查

分類:?oracle?5382人閱讀?評論(0)?收藏?舉報

為公司一個項目沒有接觸過oracle的程序員準(zhǔn)備的一個oracle如何使用proc實現(xiàn)增刪改查,簡單示例:

?

create table t1
(
sid number not null primary key,
sname varchar2(10)
)
tablespace test;


declare
a number :=1;
begin
loop?
insert into t1 values(a,'snow');
a:=a+1;
exit when a=100;
end loop;
end;


----1.insert


create or replace procedure proc_insert
(
sid number,
sname varchar2
)
is?
begin
? insert into scott.t1(sid,sname) values(sid,sname);
? ?dbms_output.put_line(' 影響的行數(shù): ? '||sql%rowcount);
? commit;
end
;


set serveroutput on
exec proc_insert(101,'snow');

----2.update

create or replace procedure proc_update
(
isid in number ,
nsname in varchar2?
)
is?
begin
? update scott.t1 set sname=nsname where sid=isid;
If ?SQL%Found ?Then
? ? DBMS_OUTPUT.PUT_LINE('更新成功!');
Else
? ? DBMS_OUTPUT.PUT_LINE('更新失敗!');
End ?If;
? commit;
end
;


set serveroutput on
exec proc_update(101,'ocpyang');


----3.delete


create or replace procedure proc_delete
(
isid in number?
)
is?
begin
? delete scott.t1 ?where sid=isid;
If ?SQL%Found ?Then
? ? DBMS_OUTPUT.PUT_LINE('刪除成功!');
Else
? ? DBMS_OUTPUT.PUT_LINE('刪除失敗!');
End ?If;
? commit;
end
;


set serveroutput on
exec proc_delete(101);

--------------4.select

--4.1變量(select ....into):單行查詢操作


create or replace procedure proc_select0
(isid in t1.sid%type ) ?--輸入?yún)?shù)
as
osid t1.sid%type; ?--變量?
osname ?t1.sname%type; ? --變量?
begin
select sid,sname into osid, osname from t1 where sid=isid;
dbms_output.put_line(' 編號為'||osid|| ' , 的職工姓名為 ?'||osname );
exception
when no_data_found then
dbms_output.put_line('沒有符合條件的記錄!');
when too_many_rows then
dbms_output.put_line('返回的行數(shù)太多!');
when others then
dbms_output.put_line('發(fā)生意外錯誤!');
end;


set serveroutput on
exec proc_select0 (101);


---4.2顯示游標(biāo):返單行單列記錄?

create or replace procedure proc_select1
(isid in t1.sid%type ) ?--輸入?yún)?shù)
as
cursor a is select sname from t1 where sid=isid;
osname t1.sname%type;
begin
open a;
fetch a into osname;?
if a%found then
dbms_output.put_line( '職工姓名為:'||osname ); ?--游標(biāo)結(jié)果集中只有一列
else
dbms_output.put_line('沒有符合條件的記錄!');
end if;
close a;
end;
? ? ? ??
set serveroutput on
exec proc_select1 (101);

--4.3顯示游標(biāo):返回單行多列記錄
create or replace procedure proc_select2
(isid in t1.sid%type ) ?--輸入?yún)?shù)
as
cursor a is select * from t1 where sid=isid ;
osname t1%rowtype;
begin
open a;
fetch a into osname;?
if a%found then?
dbms_output.put_line( '職工的編號為:'||osname.sid||';'||'的職工姓名為 ?'||osname.sname );
else
dbms_output.put_line('沒有符合條件的記錄!');
end if;
close a;
end;
??
? ? ??
set serveroutput on
exec proc_select2 (101);

---4.4顯示游標(biāo)(loop循環(huán)):返回多行多列記錄

/*

exit when語句一定要緊跟在fetch之后。必避免多余的數(shù)據(jù)處理。?
處理邏輯需要跟在exit when之后。這一點需要多加小心。?
循環(huán)結(jié)束后要記得關(guān)閉游標(biāo)。

*/

--方法1:基于表的記錄變量接收游標(biāo)數(shù)據(jù)


create or replace procedure proc_select31
--(isid in t1.sid%type ) ?--輸入?yún)?shù)
as
cursor a is select * from t1 ;
osname t1%rowtype;
begin
open a;
loop
fetch a into osname;
exit when a%notfound;
dbms_output.put_line( '職工的編號為:'||osname.sid||';'||'的職工姓名為 ?'||osname.sname );
end loop;
close a;
end;
??
? ? ? ??
set serveroutput on
exec proc_select31 ;


--方法2:基于游標(biāo)的記錄變量接收游標(biāo)數(shù)據(jù)


create or replace procedure proc_select32
as
cursor a is select * from t1 ;
cur_record a%rowtype;
begin
open a;
loop
fetch a into cur_record;?
exit when a%notfound;
dbms_output.put_line( '職工的編號為:'||cur_record.sid||';'||'的職工姓名為 ?'||cur_record.sname );
end loop;
close a;
end;
??
? ??
set serveroutput on
exec proc_select32 ;


--方法3:基于集合變量的接收游標(biāo)數(shù)據(jù)?


create or replace procedure proc_select33
as
cursor a is select * from t1 ;
type cur_table_type is table of a%rowtype index by binary_integer;
cur_table cur_table_type;
i int;
begin
open a;
loop
i:=a%rowcount+1;
fetch a into cur_table(i);?
exit when a%notfound;
dbms_output.put_line( '職工的編號為:'||cur_table(i).sid||';'||'的職工姓名為 ?'||cur_table(i).sname );
end loop;
close a;
end;
??? ??
set serveroutput on
exec proc_select33 ;

---4.5顯示游標(biāo)(while....loop循環(huán)):返回多行多列記錄


/*

游標(biāo)打開后,必須執(zhí)行一次fetch語句,游標(biāo)的屬性才會起作用。所以使用while 循環(huán)時,
就需要在循環(huán)之前進(jìn)行一次fetch動作。?
而且數(shù)據(jù)處理動作必須放在循環(huán)體內(nèi)的fetch方法之前。循環(huán)體內(nèi)的fetch方法要放在最后。否則就會多處理一次。
while循環(huán)是游標(biāo)里最復(fù)雜的一種.

*/

create or replace procedure proc_select4
--(isid in t1.sid%type ) ?--輸入?yún)?shù)
as
cursor a is select * from t1 ;
osname t1%rowtype;
begin
open a;
fetch a into osname;?
while a%found loop ?--循環(huán)之前做個fetch
dbms_output.put_line( '職工的編號為:'||osname.sid||';'||'的職工姓名為 ?'||osname.sname );
end loop;
close a;
end;
?? ? ? ??
set serveroutput on
exec proc_select4 ;


---4.6顯示游標(biāo)(for循環(huán))(適合多個記錄):返回多行多列記錄


游標(biāo)使用for循環(huán)不用open、fetch、close關(guān)閉游標(biāo).


--方法1:典型for循環(huán)


create or replace procedure proc_select5
as
cursor a is select * from t1 ;
begin
for ?res in a loop
dbms_output.put_line( '職工的編號為:'||res.sid||';'||'的職工姓名為 ?'||res.sname );
end loop;
end;

set serveroutput on
exec proc_select5 ;


--方法2:簡單for循環(huán)


create or replace procedure proc_select6
as
begin
for ?res in ( select * from t1 ) loop
dbms_output.put_line( '職工的編號為:'||res.sid||';'||'的職工姓名為 ?'||res.sname );
end loop;
end;


set serveroutput on
exec proc_select6 ;


----4.7 ref游標(biāo)(loop循環(huán))


/***

怎么使用 ?REF游標(biāo) ?
?①聲明REF 游標(biāo)類型,確定REF 游標(biāo)類型;
? ⑴強類型REF游標(biāo):指定retrun type,REF 游標(biāo)變量的類型必須和return type一致。
? ?語法:Type ? REF游標(biāo)名 ? IS ? Ref Cursor Return ?結(jié)果集返回記錄類型;
? ⑵弱類型REF游標(biāo):不指定return type,能和任何類型的CURSOR變量匹配,用于獲取任何結(jié)果集。
? ?語法:Type ? REF游標(biāo)名 ? IS ? Ref Cursor;
?②聲明Ref 游標(biāo)類型變量;
? 語法:變量名 ?已聲明Ref 游標(biāo)類型;
??
?③打開REF游標(biāo),關(guān)聯(lián)結(jié)果集 ;
? 語法:Open ? Ref 游標(biāo)類型變量 ? For ? 查詢語句返回結(jié)果集;
??
?④獲取記錄,操作記錄;
? 語法:Fetch ? ?REF游標(biāo)名 InTo ? 臨時記錄類型變量或?qū)傩灶愋妥兞苛斜?#xff1b;
??
?⑤關(guān)閉游標(biāo),完全釋放資源;
? 語法:Close ? REF游標(biāo)名;


能夠使用ref弱類型REF游標(biāo)就不要使用強類型REF游標(biāo)

***/

--案例1:ref弱類型游標(biāo):loop循環(huán)


create or replace procedure proc_select8
(
choice in varchar2
)
as
TYPE cur IS REF CURSOR; ?--聲明游標(biāo)類型為ref
a cur; ? ? --聲明變量為ref游標(biāo)類型
osname t1%rowtype;
begin
if ?choice='full' then
open a for select * from t1;
loop
fetch a into osname;?
exit when a%notfound;
dbms_output.put_line( '職工的編號為:'||osname.sid||';'||'的職工姓名為 ?'||osname.sname );
end loop;
elsif choice='top' then
open a for select * from t1 where rownum<10;
loop
fetch a into osname;?
exit when a%notfound;
dbms_output.put_line( '職工的編號為:'||osname.sid||';'||'的職工姓名為 ?'||osname.sname );
end loop;
else
? dbms_output.put_line('請輸入正確值full或top!謝謝配合!');
return;
end if;
close a;
end;
??
? ? ??
set serveroutput on
exec proc_select8('full') ;
exec proc_select8('top') ;


--案例2:ref強類型游標(biāo):loop循環(huán)


create or replace procedure proc_select9
as
TYPE cur IS REF CURSOR RETURN t1%RowType; ?--聲明游標(biāo)類型為ref
a cur; ? ? --聲明變量為ref游標(biāo)類型
osname t1%rowtype;
begin
open a for select * from t1;?
loop
fetch a into osname;
exit when a%notfound;
dbms_output.put_line( '職工的編號為:'||osname.sid||';'||'的職工姓名為 ?'||osname.sname );
end loop;
close a;
end;


? ?
set serveroutput on
exec proc_select9 ;

轉(zhuǎn)載于:https://www.cnblogs.com/meimao5211/p/3385665.html

總結(jié)

以上是生活随笔為你收集整理的oracle-扫盲贴:存储过程实现增删改查的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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