PLSQL示例
為什么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é)
- 上一篇: U盘为什么还有剩余空间,但却提示说空间不
- 下一篇: PL/SQL异常处理(原创)