PL/SQL是過程化的語言,面向過程,關注細節,可以實現復雜的業務邏輯。
1.PL/SQL優勢
①提高程序的運行性能
②程序模塊化
③可邏輯控制語句的執行
④使用exception可以獲得並處理語句異常
⑤移植性強,成功移植到不同版本的ORACLE中。
2.PL/SQL結構
declare --非必須
begin
exception --非必須
end;
其中begin ... end;是語句的執行部分,必須有內容。
注:如果在SQL PLUSE中執行PL/SQL需要在前輸入set serverputout on回車數據語句即可,這樣保證輸出會在控制天顯示。
注:select ...into...是oracle中的賦值語句,但每次只返回一條記錄,多條記錄會出現異常。x into y:將查詢出來的x賦值給y。
注釋分為兩種,
第一種:--單行注釋
第二種:/*
XXXXXXXXXXXXXXX注釋/*...*/整個內容。
*/
3.PL/SQL變量
變量:
n_count number;
常量:
n_count constant number;
基礎變量分類:
數值類型:
number:整數浮點數
pls_integer:溢出發生異常,整數
binary_integer:溢出不發生異常,證書
simple_integer:不發生異常使用此類型,性能較pls_integer高。
字符類型:char、varchar2,nchar、nchar2、long:變長,最大存2GB。
日期類型:date、timestamp
%type:與列名相同的類型,
符合類型變量
4.控制結構
4.1 if條件
if...\if ...else...\if...elsif...else...
具體語法
declare n_count number := 2; begin if n_count > 1 then dbms_output.put_line(n_count); end if; end;
4.2 case條件
簡單case、搜索式case
declare--簡單case n_count number := 2; begin case n_count when 1 then dbms_output.put_line(n_count); when 2 then dbms_output.put_line('OK'); else dbms_output.put_line('ERROR'); end case; end;
declare--搜索式case
n_count number := 2;
begin
case
when n_count=1 then
dbms_output.put_line(n_count);
when n_count=2 then
dbms_output.put_line('OK');
else
dbms_output.put_line('ERROR');
end case;
end;
4.3 基本loop
declare --簡單loop n_count number := 0; begin <<basic_loop>>--標簽:可不寫 loop dbms_output.put_line('當前n_count的值:' || n_count); n_count := n_count + 1; if n_count = 3 then dbms_output.put_line('退出->當前n_count的值:' || n_count); exit basic_loop;--如果定義標簽,在這可以跳出 end if; end loop; end; ------- declare --exit when類型loop n_count number := 0; begin <<basic_loop>> loop dbms_output.put_line('當前n_count的值:' || n_count); n_count := n_count + 1; exit basic_loop when n_count = 3;--先執行exit - when 語句 end loop; dbms_output.put_line('退出->當前n_count的值:' || n_count); end;
4.4 while loop語句
declare --while類型loop,先判斷后執行 n_count number := 0; begin while n_count < 3 loop dbms_output.put_line('當前n_count的值:' || n_count); n_count := n_count + 1; end loop; dbms_output.put_line('退出->當前n_count的值:' || n_count); end; --------- declare --while類型loop,先執行后判斷 n_count number := 0; flag boolean := true; begin while flag loop dbms_output.put_line('當前n_count的值:' || n_count); n_count := n_count + 1; if (n_count = 3) then flag := false; end if; end loop; dbms_output.put_line('退出->當前n_count的值:' || n_count); end;
4.5 for loop語句
declare --for類型loop n_count number := 0; begin for inx in 1 .. 3 loop --1:代表下界 3:代表上界 dbms_output.put_line('當前n_count的值:' || n_count); n_count := n_count + 1; end loop; dbms_output.put_line('退出->當前n_count的值:' || n_count); end;
4.6 ddl/dml
declare --ddl v_sql varchar2(200); begin v_sql := 'create table test_1238 (id number)'; execute immediate v_sql; end; ------- declare --dml v_sql varchar2(200); begin insert into test_1238 values (32); commit; v_sql := 'insert into test_1238 values (34)'; execute immediate v_sql; commit; end;
注意:在PL/SQL中select語句的使用為 select 1 into vals from dual;
執行DDL語句中,需要 execute immediate ‘語句’,關於 execute immediate的使用將在后續推出。