1. 程式人生 > >Oracle資料庫之PL/SQL

Oracle資料庫之PL/SQL

Oracle資料庫總結:

一、語法結構:

declare 
--說明部分
begin
--sql語句
exception
--例外處理語句
end;
/

二、變數說明

變數型別:char、varhchar2、number、longdateboolean
普通變數:
    var1 varchar2(20);
記錄型變數:
    emp1 emp%rowtype;
引用型變數:
    emp2 emp.empno%type;
--查詢並列印5478的姓名和薪水
set serveroutput on

declare
  --定義變數儲存姓名和薪水
  --pename varchar
(20);
--psal number; pename emp.ename%type; psal emp.sal%type; begin --得到姓名和薪水 select ename,sal into pename,psal from emp where empno=7839; dbms_output.put_line(pename||'的薪水是'||psal); end; /
--查詢並列印5478的姓名和薪水
set serveroutput on
declare
  --定義記錄型變數:代表一行
  emp_rec emp%rowtype;
begin select * into emp_rec from emp where empno=7839; dbms_output.put_line(emp_rec.ename||'的薪水是'||emp_rec.sal); end; /

三、迴圈體
1)if判斷語句
語法結構:

if 條件 then 語句1
語句2
end if;

if 條件 then 語句1
語句2
elsif 條件 then 語句3
語句4
end if;

if 條件 then 語句1
語句2
elsif 條件 then 語句3
語句4
else
語句5
end if;
--判斷使用者從鍵盤上輸入的數字
set serveroutput on --接收鍵盤輸入 --num: 地址值,在地址上儲存了輸入的數字 accept num prompt '請輸入一個數字'; declare --定義變數,儲存輸入的數字 pnum number := # begin if pnum = 0 then dbms_output.put_line('您輸入的是0'); elsif pnum = 1 then dbms_output.put_line('您輸入的是1'); elsif pnum = 2 then dbms_output.put_line('您輸入的是2'); else dbms_output.put_line('其他數字'); end if; end; /

2)迴圈結構

while 條件
loop
語句
end looploop
exit when 條件
語句
end loopfor i in 1..3
loop
語句
end loop;
--列印1~10
set serveroutput on
declare
  pnum number := 1;
begin
  loop
    --退出條件
    exit when pnum > 10;

    dbms_output.put_line(pnum);

    --加一
    pnum := pnum + 1;
  end loop;
end;
/

四、游標(cursor)
1)語法規則

cursor cursorname[(引數名 資料型別...)]
is select語句

2)使用步驟

開啟游標:open cursorname;

抓取資料:fetch cursorname into 變數;

關閉游標:close cursorname;
--查詢並列印員工的姓名和薪水
/*
1.  游標的屬性
      %isopen      %rowcount (影響的行數)
      %found       %notfound

*/
set serveroutput on
declare
  -- 定義游標
  cursor cemp is select ename,sal from emp;
  pename emp.ename%type;
  psal       emp.sal%type;
begin
  --開啟游標
  open cemp;

  loop
    --取一條記錄
    fetch cemp into pename,psal;
    --exit when 沒有取到記錄;
    exit when cemp%notfound;

    dbms_output.put_line(pename||'的薪水是'||psal);

  end loop;

  --關閉游標
  close cemp;
end;
/
set serveroutput on

declare
   --說明部分
begin
   --程式
   dbms_output.put_line('Hello World');
end;
/

五、異常處理

1)系統異常

no_data_found:沒有資料
to_many_rows:太多行
zero_divide:0value_error:算數或者轉換錯誤
timeout_on_resource:等待資源時超時
--被0set serveroutput on

declare 
  pnum number;
begin
  pnum := 1/0;

exception
  when zero_divide then dbms_output.put_line('1:0不能做被除數');
                                         dbms_output.put_line('2:0不能做被除數');
  when value_error then dbms_output.put_line('算術或者轉換錯誤');
  when others then dbms_output.put_line('其他例外');
end;
/

例項一:

--漲工資,總裁10000 經理5000 其他4000
set serveroutput on

declare
  --員工的集合
  --alter table "SCOTT"."EMP" rename column "JOB" to empjob
  cursor cemp is select empno,empjob from emp;
  pempno emp.empno%type;
  pjob   emp.empjob%type;
begin
  rollback;

  open cemp;

  loop
    --取一個員工
    fetch cemp into pempno,pjob;
    exit when cemp%notfound;

    --判斷職位
    if pjob = 'PRESIDENT' then update emp set sal=sal+10000 where empno=pempno;
      elsif pjob = 'MANAGER' then update emp set sal=sal+5000 where empno=pempno;
      else update emp set sal=sal+4000 where empno=pempno;
    end if;
  end loop;

  close cemp;


  commit;

  dbms_output.put_line('完成');
end;
/

例項二:

--查詢某個部門的員工姓名
set serveroutput on
declare
  cursor cemp(dno number) is select ename from emp where deptno=dno;
  pename emp.ename%type;
begin
  open cemp(10);
  loop
    fetch cemp into pename;
    exit when cemp%notfound;

    dbms_output.put_line(pename);

  end loop;
  close cemp;
end;
/

例項三:

--查詢並列印100號部門的員工姓名
set serveroutput on

declare
  cursor cemp is select ename from emp where deptno=100;
  pename emp.ename%type;

  --自定義例外
  no_emp_found exception;
begin
  open cemp;

  --取一條記錄
  fetch cemp into pename;

  if cemp%notfound then 
    --丟擲例外
    raise no_emp_found;
  end if;  

  close cemp;

exception
  when no_emp_found then dbms_output.put_line('沒有找到員工');
  when others then dbms_output.put_line('其他例外');
end;
/