PL SQL流程控制語法

2021-10-21 20:54:52 字數 2909 閱讀 9346

set serveroutput on /ed 開啟text editer 並輸出結果

dbms_output.put_line(『helloword』);輸出語句

/執行輸出。v_sal varchar2(10) :=0;賦值語句

例子declare

v_sal sc.score%type;獲取相同資料型別

v_sale sc.cid%type;

begin

select score,cid into v_sal,v_sale from sc where rownum = 1;

dbms_output.put_line(v_sal||』,』||v_sale);

記錄型別:邏輯相關的資料

例子declare

–宣告記錄型別

type sc_record is record(

v_sal sc.score%type,

v_sale sc.cid%type);

–定義記錄型別成員變數

v_sc_record sc_record;

begin

select score,cid into v_sc_record from sc where rownum = 1;

dbms_output.put_line(『s:』||v_sc_record.v_sal||』,』||v_sc_record.v_sale);

–表v_sc_record sc%rowtype;

v_num number(10);

begin

v_num :=1;

select * into v_sc_record from sc where rownum = v_num;

dbms_output.put_line(『s:』||v_sc_record.sid||』,』||v_sc_record.score);

流程控制語句

declare

v_sc_record sc%rowtype;

v_num number(10);

v_sal varchar2(20);

begin

v_num :=1;

select * into v_sc_record from sc where rownum = v_num;

if v_sc_record.score <=30 then v_sal := 『score <=30』;

elsif v_sc_record.score <=30 then v_sal := 『score <=50』;

else v_sal := 『score >50』;

end if;

dbms_output.put_line(『s:』||v_sc_record.score||』,』||v_sal);

–流程控制語句:case,數字2,3也可用字串代替,如果v_sc_record.score是字串;

declare

v_sc_record sc%rowtype;

v_num number(10);

v_sal varchar2(20);

begin

v_num :=1;

select * into v_sc_record from sc where rownum = v_num;

v_sal :=

case trunc(v_sc_record.score/80)

when 3 then 『score <=30』

when 2 then 『score <=50』

else 』 score >50』

end ;

dbms_output.put_line(『s:』||v_sc_record.score||』,』||v_sal);

–迴圈loop

declare

v_num number(10);

begin

v_num :=1;

loop

dbms_output.put_line(v_num);

exit when v_num =100;

v_num :=v_num +1;

end loop;

–迴圈while

declare

v_num number(10) :=1;

begin

while v_num <=100 loop

dbms_output.put_line(v_num);

v_num :=v_num +1;

end loop;

–查詢質數

declare

v1 number(10) :=2;

v2 number(10) :=2;

v_flag number(1) :=1;

begin

while v1 <=100 loop

while v2 <=sqrt(v1) loop

if mod(v1,v2) = 0 then v_flag :=0;

goto label;(直接跳轉)

end if;

v2 := v2 + 1;

end loop;

<>

if v_flag = 1 then dbms_output.put_line(v1);

end if;

v2 := 2;

v1 := v1 + 1;

v_flag := 1;

end loop;

–迴圈 for: for v_num in reverse 1…100 loop(反序)

begin

for v_num in 1..100 loop

dbms_output.put_line(v_num);

end loop;

end;

PLSQL程式設計 流程控制

1.條件分支 語法 created on 2018 8 23 by mengmeng.chen declare local variables here i integer begin test statements here if 條件1 then 執行1 elsif 條件2 then 執行2 e...

PL SQL之 流程控制語句

一 簡介 像程式語言一樣,oracle pl sql也有自己的流程控制語句。通過流程控制語句,我們可以在pl sql中實現一下比較複雜的業務邏輯操作。而無需到程式中去控制,在一定程度上提高了效率,這也是pl sql的強大之處。pl sql流程控制語句有如下幾種 二 語句種類 1 控制語句 a if語...

流程控制語法2

1.for語句 for variable in argument list do command list done 2.while語句 while expression do command list done 3.until 語句 until expression do command list...