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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

PLSQL示例

發(fā)布時間:2025/6/15 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PLSQL示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??

##1.最簡單的plsql

例子:

declare --開始 x varchar2(10); --聲明變量 begin --開始標識 x:='This is ..'; --給變量賦值 dbms_output.put_line('x的值為:'||x); --輸出x的值 end; --結(jié)束標識 /

在plsql中用||連接兩個字符 等于java中的+符號

設置服務器的plsql顯示結(jié)果

set serveroutput on size 1000; --或直接寫: set serveroutput on;

再執(zhí)行/ 可以看到這個結(jié)果 x的值為: This is ..

dbms_output.put_line('x的值為:'||x); 等同于 dbms_output.put('x的值為:'||x); dbms_output.new_Line;

##2.分支語句

###1.if分支

If… then Elsif … then Else … End if

例子1:

declare a number:=3; b number; begin if a<3 then b:=1; elsif a>3 then b:=5; else b:=3; end if; dbms_output.put_line(b); end; /

例子2:

declare a number; b varchar2(10); begin a:=4; if a=1 then b:='a'; elsif a=2 then b:='b'; else b:='c'; end if; dbms_output.put_line(b); --輸出結(jié)果為:c end; /

###2.case分支

Case When … then …Else End case

例子:

declare a number; b varchar2(10); begin a:=2; case when a=1 then b:='a'; when a=2 then b:='b'; when a=3 then b:='c'; else b:='others'; end case; dbms_output.put_line(b); end; /

##3.循環(huán)語句

###1.基本循環(huán)(loop)

Loop … end loop

示例1:

declare x number ; begin x:=0; loop x:=x+1; if x>5 then exit; end if; dbms_output.put_line('內(nèi)x='|| x); end loop; dbms_output.put_line('外x='|| x); end; / /*結(jié)果 內(nèi)x=1 內(nèi)x=2 內(nèi)x=3 內(nèi)x=4 內(nèi)x=5 外x=6 */

示例2:

declare x number; begin x:=0; loop x:=x+1; exit when x>=3; --當x大于等于3的時候跳出 dbms_output.put_line('內(nèi)x='|| x); end loop; dbms_output.put_line('外x='||x); end ; / /*結(jié)果 內(nèi)x=1 內(nèi)x=2 外x=3 */

###2.while循環(huán)

While expression loop… end loop;

示例1:

declare x number; begin x:=0; while x<=3 loop x:=x+1; dbms_output.put_line('內(nèi)x='||x); end loop; dbms_output.put_line('外x='||x); end; /

示例2:

declarex number;beginx:=0;while x<=6 loopx:=x+1;dbms_output.put_line('內(nèi)x='||x);exit when x=3; --滿足條件跳出循環(huán)end loop;dbms_output.put_line('外x='||x);end;/

###3.for循環(huán)

For counter in [reverse] start_value..end_value loop … End loop;

示例1:

beginfor i in 1..6 loopdbms_output.put_line('i='|| i);end loop;dbms_output.put_line('end of for loop');end;/ /*結(jié)果 i=1 i=2 i=3 i=4 i=5 i=6 end of for loop */

示例2:

begin for i in reverse 1..5 loop dbms_output.put_line(i); end loop; end; //*結(jié)果 5 4 3 2 1 */

###4.Goto的使用

declare x number; begin x:=0; <<repeat_loop>> x:=x+1; dbms_output.put_line(x); if x<3 then goto repeat_loop; end if; end; / /*結(jié)果 1 2 3 */

4.異常處理

Exception when .. then ..

示例1:

declare test varchar2(10); begin select ename into test from emp where empno=55; dbms_output.put_line(test);exception when No_DATA_FOUND then dbms_output.put_line('沒有找到數(shù)據(jù)'); end; /

示例2:

declaretest varchar2(10);beginselect ename into test from emp ;dbms_output.put_line(test);exceptionwhen no_data_found thendbms_output.put_line('沒有找到數(shù)據(jù)!');when too_many_rows thendbms_output.put_line('返回的數(shù)據(jù)行太多!');when others thendbms_output.put_line('其他問題!');end;/

示例3:

declare ename varchar2(10); begin select ename into ename from emp where empno=33; exception when others then dbms_output.put_line('出錯了!'); end; /

轉(zhuǎn)載于:https://my.oschina.net/csmw00/blog/678281

總結(jié)

以上是生活随笔為你收集整理的PLSQL示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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