首先編寫(xiě)存儲(chǔ)過(guò)程的整體結(jié)構(gòu),如下:
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序定制開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了河間免費(fèi)建站歡迎大家使用!
create or replace procedure test_proc is
v_date date; --變量定義
begin
select sysdate into v_date from dual;
end test_proc;
2
定義游標(biāo):
create or replace procedure test_proc is
v_date date; --定義變量
cursor cur is select * from ldcode; --定義游標(biāo)
begin
select sysdate into v_date from dual;
end test_proc;
3
編寫(xiě)for循環(huán):
create or replace procedure test_proc is
v_date date; --定義變量
cursor cur is select * from ldcode where rownum10; --定義游標(biāo)
begin
select sysdate into v_date from dual;
--游標(biāo)for循環(huán)開(kāi)始
for temp in cur loop --temp為臨時(shí)變量名,自己任意起
Dbms_Output.put_line(temp.Code); --輸出某個(gè)字段,使用"變量名.列名"即可。
end loop;
--游標(biāo)for循環(huán)結(jié)束
end test_proc;
4
測(cè)試運(yùn)行,點(diǎn)擊【DBMS Output】標(biāo)簽頁(yè)查看結(jié)果如下圖:
END
二、帶參數(shù)的游標(biāo)for循環(huán)
1
定義帶參數(shù)的游標(biāo):
cursor cur(v_codetype ldcode.Codetype%TYPE) is
select * from ldcode where codetype = v_codetype; --定義游標(biāo)
定義游標(biāo)格式:
cursor 游標(biāo)名稱(chēng)(變量定義) is 查詢(xún)語(yǔ)句;
注意:
where條件中的變量名v_codetype要與游標(biāo)定義cur(v_codetype ldcode.Codetype%TYPE)中的一致。
2
編寫(xiě)for循環(huán)部分:
--游標(biāo)for循環(huán)開(kāi)始
for temp in cur('llmedfeetype') loop
--temp為臨時(shí)變量名,自己任意起
--cur('llmedfeetype')為"游標(biāo)名稱(chēng)(傳入的變量)"
Dbms_Output.put_line(temp.Code); --輸出某個(gè)字段,使用"變量名.列名"即可。
end loop;
--游標(biāo)for循環(huán)結(jié)束
3
測(cè)試運(yùn)行,點(diǎn)擊【DBMS Output】標(biāo)簽頁(yè)查看結(jié)果如下圖:
使用loop循環(huán),比如:
for item in (select a,b,c from table_a where 條件) loop
insert into table_b(a,b,c) values (item.a,item.b,item.c);
end loop;
也可以使用索引表循環(huán),以上只是一個(gè)簡(jiǎn)單的例子,需要根據(jù)你的具體情況選擇循環(huán)方式。
1、采用insert into values 語(yǔ)句插入一條,寫(xiě)很多條語(yǔ)句即可多條數(shù)據(jù),這種主要針對(duì)于離散值以及一些基礎(chǔ)信息的錄入,如:insert into test(xh,mc) values('123','測(cè)試');
如果插入的數(shù)據(jù)有規(guī)律,可利用for、loop循環(huán)插入,主要用于批量生成測(cè)試數(shù)據(jù)
begin
for i in 1 .. 100 loop
insert into test(xh,mc) values(i||'','測(cè)試');
end loop;
end ;。
2、采用insert into selct from 語(yǔ)句來(lái)一次性插入一個(gè)集合,這種主要依據(jù)于要插入的數(shù)據(jù)源已經(jīng)存儲(chǔ)于數(shù)據(jù)庫(kù)對(duì)象中,或者利用dual虛表來(lái)構(gòu)造數(shù)據(jù),經(jīng)過(guò)加工后寫(xiě)入一個(gè)集合。
insert into test (xh,mx) select '123','測(cè)試' from dual;
3、采用plsql等工具、或者oracle的imp、impdp命令來(lái)導(dǎo)入,這種主要用數(shù)據(jù)庫(kù)與數(shù)據(jù)庫(kù)之間的大批量數(shù)據(jù)導(dǎo)入,導(dǎo)入的數(shù)據(jù)格式為plsql的pde、oracle的dmp等。dmp文件可使用
table_exists_action參數(shù)控制導(dǎo)入動(dòng)作:replace替換原表,truncate清除原表數(shù)據(jù)再導(dǎo)入,append增量導(dǎo)入數(shù)據(jù),當(dāng)然impdp數(shù)據(jù)泵的導(dǎo)入要依賴(lài)于directory路徑。
impdp 用戶(hù)名/密碼 dumpfile=123.dmp logfile=123.log directory=imp_dir tables=test table_exists_action=append。
4、使用excel文件直接拷貝。這種主要用于要寫(xiě)入的數(shù)據(jù)已是excel文件或者行列分明的其它格式文件,每一列的值和表結(jié)構(gòu)相對(duì)應(yīng),可直接打開(kāi)表的行級(jí)鎖,把數(shù)據(jù)拷貝進(jìn)入。
Oracle中有三種循環(huán)(For、While、Loop):
1、loop循環(huán):
create?or?replace?procedure?pro_test_loop?is
i?number;
begin
i:=0;
loop
i:=i+1;
dbms_output.put_line(i);
if?i5?then
exit;
end?if;
end?loop;
end?pro_test_loop;
2、while循環(huán):
create?or?replace?procedure?pro_test_loop??is
i?number;
begin
i:=0;
while?i5?loop
i:=i+1;
dbms_output.put_line(i);
end?loop;
end?pro_test_loop;
3、for循環(huán)1:
create?or?replace?procedure?pro_test_for?is
i?number;
begin
i:=0;
for?i?in?1..5?loop
dbms_output.put_line(i);
end?loop;
end?pro_test_for;
4、for循環(huán)2:
create?or?replace?procedure?pro_test_cursor?is
userRow?t_user%rowtype;
cursor?userRows?is
select?*?from?t_user;
begin
for?userRow?in?userRows?loop
dbms_output.put_line(userRow.Id||','||userRow.Name||','||userRows%rowcount);
end?loop;
end?pro_test_cursor;
一、GOTO循環(huán)用法
DECLARE
x number;
BEGIN
x := 9;
repeat_loop --循環(huán)點(diǎn)
x := x - 1;
dbms_output.put_line(x);
IF x 0 THEN
? ? GOTO repeat_loop;? --當(dāng)x的值0時(shí),就goto到repeat_loop
END IF;
END;
/*以上語(yǔ)句翻譯如下:
declare 定義變量;
begin...end語(yǔ)句塊
x 變量賦值
repeat_loop 設(shè)置循環(huán)點(diǎn)
循環(huán)內(nèi)容
? ? x 變量遞減
? ? 按行打印 x
IF...END IF語(yǔ)句塊
? ? IF...(條件) THEN :滿(mǎn)足IF條件? 則
? ? GOTO語(yǔ)句 前往循環(huán)點(diǎn)
*/
二、FOR循環(huán)用法
DECLARE
x number;
BEGIN
FOR i in 2..10 LOOP
? ? dbms_output.put_line(i);
END LOOP;
END;
--最簡(jiǎn)單的循環(huán)?
/*
declare 定義變量
begin...end語(yǔ)句塊
for...loop...end loop; 語(yǔ)句
*/
三、WHILE循環(huán)用法
DECLARE
x number;
BEGIN
x :=5;
WHILE x 1 LOOP
? ? x := x - 1;
? ? dbms_output.put_line('循環(huán)內(nèi)'||x);
END LOOP;
dbms_output.put_line('循環(huán)外'||x);
END;
/*
declare 定義變量
begin...end 語(yǔ)句塊
while...loop...end loop; 語(yǔ)句
*/
四、LOOP循環(huán)用法
DECLARE
x number;
BEGIN
x :=0;
LOOP
? ? ? x := x + 1;
? ? ? EXIT WHEN x 9; --這里有個(gè)";"號(hào)
? ? ? dbms_output.put_line('內(nèi)'||x);
END LOOP;
dbms_output.put_line('外'||x);
END;
/*
declare 定義變量
begin...end
x 變量賦值
loop...end loop語(yǔ)句
? ? exit when ...(條件) ;
*/
文章標(biāo)題:oracle中怎么寫(xiě)循環(huán) oracle程序設(shè)計(jì)中的循環(huán)語(yǔ)句
當(dāng)前鏈接:http://vcdvsql.cn/article2/hejioc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、搜索引擎優(yōu)化、關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計(jì)公司、動(dòng)態(tài)網(wǎng)站、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)