Oracle預存程序與觸發器基本操作

來源:互聯網
上載者:User

標籤:

(1) 在JXGL 資料庫中建立一預存程序並運行,從S 表中查詢所有女生的資訊。

1、建立預存程序

create or replace procedure prFemale(pSno out S_RZ0119.Sno%type,pSname out S_RZ0119.Sname%type,pSage out S_RZ0119.Sage%type,pSsex out S_RZ0119.Ssex%type,pSdept out S_RZ0119.Sdept%type,out_var out sys_refcursor)isbeginopen out_var for select Sno,Sname,Sage,Ssex,Sdept into pSno,pSname,pSage,pSsex,pSdeptfrom S_RZ0119where Ssex=‘女‘;end;/

2、執行預存程序

var abc refcursordeclarepSno varchar2(11);pSname varchar2(20);pSage number(2);pSsex varchar2(9);pSdept varchar2(20);beginprFemale(pSno,pSname,pSage,pSsex,pSdept,:abc);end;/

//列印

print :abc;

(2) 在JXGL 資料庫中建立一預存程序並運行,從S 表中根據學號查詢並返回該學
生的姓名和年齡。

1、建立根據學號查詢學生姓名和年齡的過程

create or replace procedure searchS(pSno in S_RZ0119.Sno%type,pSname out S_RZ0119.Sname%type,pSage out S_RZ0119.Sage%type)isbeginselect Sname,Sage into pSname,pSage from S_RZ0119where Sno=pSno;end;/

2、執行預存程序

declarepSname varchar2(20);pSage number(20);beginsearchS(‘131102‘,pSname,pSage);dbms_output.put_line(pSname);dbms_output.put_line(pSage);end;/

(3) 在JXGL 資料庫中建立一觸發器,保證S 表中學生的年齡在8-45 歲之間。

1、建立年齡觸發器

create or replace trigger tri_SageWrongbefore inserton S_RZ0119for each rowbeginif((:new.Sage<8) or (:new.Sage>45)) thenRAISE_APPLICATION_ERROR(-20600,‘學生表中的學生年齡必須在8-45之間!‘);end if;end;/

2、測試

insert into S_RZ0119 values (‘130105‘,‘李得勝‘,‘男‘,6,‘軟體工程系‘);

(4) 在JXGL 資料庫中建立一觸發器,當有學生選了某門課時,不能從C 表中刪除
該課程。

1、建立課程觸發器

create or replace trigger tri_CourseDeletbefore deleteon C_RZ0119for each rowdeclare CCount number;beginselect count(*) into CCount from SC_RZ0119 where Cno=:old.Cno;if CCount>0 thenRAISE_APPLICATION_ERROR(-20601,‘該課程已被學生選定,不能刪除!‘);end if;end;/

2、測試

delete from C_RZ0119 where Cno=‘C001‘;

 

Oracle預存程序與觸發器基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.