Oracle-14:PLSQL


 

 

------------吾亦無他,唯手熟爾,謙卑若愚,好學若飢-------------

 

 

PL/SQL

PL/SQL(Procedural Language):過程化sql語言!

在原本的sql語句之上,再增加一些邏輯或者循環等操作。

1:基本語法

01.declare     可選部分    聲明:變量,異常,游標......

02.begin      必選部分 

03.exception   可選部分     針對於異常的處理

04.end        必選部分 

2:小例子

-- 直接在輸出台中顯示內容

begin
dbms_output.put_line('真的很神奇。');
end;

 

-- 查詢1002老師編號 ,我們得到對應的老師姓名和薪水

declare  --聲明變量

  v_tname   teacher.tname%type;
  v_sal     teacher.sal%type;
  v_result   varchar2(30);
begin  -- 開始pl/sql編程

   select tname,sal into v_tname,v_sal   from teacher where tno=1002;
dbms_output.put_line('姓名是==》'||v_tname);  --  ||是拼接字符串
dbms_output.put_line('薪水是==》'||v_sal);

  if v_sal<=30000  then 
    v_result:='薪水是一般化'||v_sal;   --   :=是賦值操作

  elsif   v_sal>30000 and  v_sal<=40000 then
    v_result:='薪水是一般以上'||v_sal;

  else 
     v_result:='薪水可以了'||v_sal;   

  end  if;
   dbms_output.put_line(v_result); 

end;

  3:case

-- 直接在輸出台中顯示內容

begin
dbms_output.put_line('真的很神奇。');
end;

 

-- 查詢1002老師編號 ,我們得到對應的老師姓名和薪水

declare  --聲明變量
  v_tname   teacher.tname%type;
  v_sal     teacher.sal%type;
  v_result   varchar2(30);

begin  -- 開始pl/sql編程
   select tname,sal into v_tname,v_sal   from teacher where tno=1002;
dbms_output.put_line('姓名是==》'||v_tname);  --  ||是拼接字符串
dbms_output.put_line('薪水是==》'||v_sal);

  if v_sal<=30000  then 
    v_result:='薪水是一般化'||v_sal;   --   :=是賦值操作

  elsif   v_sal>30000 and  v_sal<=40000 then
    v_result:='薪水是一般以上'||v_sal;

  else 
     v_result:='薪水可以了'||v_sal;   

  end  if;
   dbms_output.put_line(v_result); 

end;

 

--  case 塊

declare 
  num1 varchar2(20);
  v_result  varchar2(20);   --聲明了2個變量

begin
  num1:='大家辛苦了20';   --  給num1賦值

  case num1               -- case開始
    when '大家辛苦了1' then
      v_result:='1'; 

    when '大家辛苦了2' then
      v_result:='2'; 

    when '大家辛苦了3' then
     v_result:='3'; 

     else
       v_result:='默認的'; 

  end  case;              -- case結束
   dbms_output.put_line(v_result);   -- 輸出結果

end;

 

4:循環結構

-- 循環結構 do_while

declare
i number;

begin
i:=1; --初始化變量

loop --開始循環

dbms_output.put_line(i); --循環體

i:=i+1;

exit when i>20; -- 循環條件

end loop; --結束循環

end;

-- while循環

declare
  i number;

begin
  i:=1;   --初始化變量

  while i<20  loop
    dbms_output.put_line(i);   --循環體
    i:=i+1;
  end loop;  

end;  

-- for循環

declare
  i number;

begin
 for i in 1..20
   loop
        dbms_output.put_line(i);   --循環體
   end loop;
end;

 

5:函數

-- 函數

create or replace function fn_teacher_tid
(f_tid varchar2)
return  varchar2
is  
f_result teacher.tid%type;

begin
    --判斷身份證格式是否正確
    if length(f_tid)!=18 then
      dbms_output.put_line('身份證格式不正確!');  

    else
      dbms_output.put_line('身份證格式正確!');

       --給返回值賦值
      f_result:=substr(f_tid,1,6)||'********'||substr(f_tid,15); 

    end if;  
    return f_result;

end   fn_teacher_tid; -- 函數結束

    

-- 調用函數

select fn_teacher_tid(11010119910815477) from dual;

 

6:游標

游標概念

01.是一個數據的緩存區,存放的是sql語句執行的結果集;

02.是一個能從多條數據結果集中每次獲取一條記錄的機制!

作用

 01.游標可以反復使用,減少訪問數據庫的次數

 02.大大提高我們的檢索效率

分類

 01.隱式游標: 我們執行dml操作時,數據庫自動創建  ==?名字 sql

 02.顯示游標: 可以返回多行數據的查詢結果

03.REF游標: 用於處理運行時才能確定的動態sql

 

游標常用的屬性

01. %found        是否還有數據   boolean類型的值

02. %notfound     是否沒有數據   boolean類型的值

03.%rowcount     sql語句影響的行數

04.%isopen        判斷游標是否打開

 

 --游標(顯示游標)

declare
 c_tname  teacher.tname%type;
 cursor  tname_cursor is select tname from teacher; -- 聲明游標

begin
  open  tname_cursor; --打開游標
   fetch tname_cursor into c_tname;  -- 給變量賦值

  --循環輸出教師名稱
  while tname_cursor%found loop
     dbms_output.put_line('老師的姓名'||c_tname);
     fetch tname_cursor into c_tname;  -- 給變量賦值
  end  loop;

  close tname_cursor; --關閉游標
end;

7:存儲過程

定義

01.就是一組用於完成特定數據庫功能的sql語句的集合;

02.經常被使用;

03.sql語句的集合必須編譯成功后才能存儲在數據庫系統中。

 

組成部分:

   01.聲明  變量:輸入的變量,輸出的變量

   02.執行

   03.異常處理

-- 創建一個新增部門信息的  存儲過程

create or replace procedure  pro_dept_add
(
p_deptno dept.deptno%type,
p_dname dept.dname%type,
p_loc dept.loc%type
)

is e_deptno exception;  -- 聲明存儲過程中可能出現的異常
begin
    if p_deptno<10 then
      raise e_deptno;   --拋出異常

    end if;
   --新增部門信息
   insert into dept(deptno,dname,loc)
   values (p_deptno,p_dname,p_loc);

   --手動提交事務
   commit;

exception     -- 對異常進行處理
    when e_deptno then
       dbms_output.put_line('部門編號不合法');

    when others then
       dbms_output.put_line('其他的異常');

end pro_dept_add;

-- 調用存儲過程

call pro_dept_add(10,'新增部門','一樓');

8:觸發器

定義

   01.在創建觸發器的時候,規定了觸發的時機

   02.人為的不能去調用

組成部分

  01.觸發器名

  02.觸發時間

  03.before:在數據庫動作執行之前

  04.after:在數據庫動作執行之后

  05.insert

  06.update

  07.delete

  08.表名: 觸發器所在的表

  09.for each row

屬性

  :old  代表修改之前的值

  :new 代表修改之后的值

只能在.for each row中使用

Insert   ====>:new

Delete  ====>:old

Update  ====>:old  :new

 

-- 創建一個序列

create sequence sq_teacherlog_logid
minvalue 1
maxvalue 9999999
start with 1
increment by 1
cache 50;

-- 創建一個表 用來 監聽 teacher表的變化

create  table  teacher_log(
logid  number not null primary key,    
log_type varchar2(10) not null,
log_time date not null,
log_data  varchar2(50)
)

-- 創建一個觸發器

create  or replace trigger tr_teacher
after insert or update or delete    -- 觸發時機
on  teacher for each row

declare
t_type teacher_log.log_type%type;
t_time teacher_log.log_time%type;
t_data teacher_log.log_data%type;

begin
  if inserting then  --新增
    t_type:='insert';
    t_data:=:new.tno||'===='||:new.sal;

  elsif deleting then  -- 刪除  
    t_type:='delete'; 
    t_data:=:old.tno||'===='||:old.sal;

  else
    t_type:='update'; 
    t_data:=:old.tno||'===='||:old.sal||'===='||:new.sal;

  end if;

  -- 將用戶操作的數據保存到 teacher_log

 

insert into teacher_log
  values(sq_teacherlog_logid.nextval,t_type,sysdate,t_data);
end tr_teacher;

-- 新增數據到teacher

insert into teacher(tno,sal) values(1200,20000);

-- 修改上面的新增數據

update teacher  set sal=40000 where tno=1200;

-- 刪除上面的數據

delete from teacher where tno=1200;

 

 

 

原作者:晨曦Dawn

博客地址:https://www.cnblogs.com/DawnCHENXI/p/9073382.html

轉載請注明出處!!!!!!!如果有錯誤請指出,會更改,感激不盡


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM