Oracle入門基礎(十二)一一儲存過程及觸發器

2021-09-27 06:18:33 字數 2923 閱讀 5216

列印hello world

呼叫儲存過程:

1.exec sayhelloworld();

2.begin

sayhelloworld();

sayhelloworld();

end;

create or replace procedure sayhelloworld

as --說明部分

begin

dbms_output.put_line('hello world');

end;

給指定的員工漲100,並且列印漲前和漲後的薪水

create or replace procedure raisesalary(eno in number)

is --定義變數儲存漲前的薪水

psal emp.sal%type;

begin

--得到漲前的薪水

select sal into psal from emp where empno=eno;

--漲100

update emp set sal=sal+100 where empno=eno;

--要不要commit?

dbms_output.put_line('漲前:'||psal||' 漲後:'||(psal+100));

end raisesalary;

查詢某個員工的年收入

create or replace function queryempincome(eno in number) 

return number

is --定義變數儲存月薪和獎金

psal emp.sal%type;

pcomm emp.comm%type;

begin

--得到月薪和獎金

select sal,comm into psal,pcomm from emp where empno=eno;

--返回年收入

return psal*12+nvl(pcomm,0);

end queryempincome;

–查詢某個員工的姓名 薪水和職位

/*1、查詢某個員工的所有資訊 —> out引數太多

2、查詢某個部門中的所有員工資訊 ----> 返回的是集合

*/

create or replace procedure queryempinformation(eno in number,

pename out varchar2,

psal out number,

pjob out varchar2)

isbegin

select ename,sal,job into pename,psal,pjob from emp where empno=eno;

end queryempinformation;

查詢某個部門中的所有員工資訊 ----> 返回的是集合

create or replace package mypackage is

type empcursor is ref cursor;

procedure queryemplist(dno in number,emplist out empcursor);

end mypackage;

create or replace package body mypackage is

procedure queryemplist(dno in number,emplist out empcursor)

asbegin

open emplist for select * from emp where deptno=dno;

end;

end mypackage;

每當成功插入新員工後,自動列印「成功插入新員工」

create trigger firsttrigger

after insert

on emp

declare

begin

dbms_output.put_line('成功插入新員工');

end;

實施複雜的安全性檢查

禁止在非工作時間 插入新員工

1、週末: to_char(sysdate,『day』) in (『星期六』,『星期日』)

2、上班前 下班後:to_number(to_char(sysdate,『hh24』)) not between 9 and 17

create or replace trigger securityemp

before insert

on emp

begin

if to_char(sysdate,'day') in ('星期六','星期日','星期五') or

to_number(to_char(sysdate,'hh24')) not between 9 and 17 then

--禁止insert

end if;

end securityemp;

/*

資料的確認

漲後的薪水不能少於漲前的薪水

create or replace trigger checksalary

before update

on emp

for each row

begin

--if 漲後的薪水 < 漲前的薪水 then

if :new.sal < :old.sal then

end if;

end checksalary;

一 基礎入門

1.輸出 語法 print value1,value2,sep end n print 123,abc 結果 123 abc print 1,a 3,sep 結果 1 a 3 print hello end 輸出結果時不再換行2.編碼初識 二進位制與文字的對應關係 密碼本 編碼英文 中文容量 說明a...

oracle 入門學習(一)

一直想學oracle但都沒有下定決心。這次借了書,一定要學好oracle。目前學習 oracle從入門到精通 明日科技 的oracle 11g 版本 關係型資料庫的基本理論 資料模型 層次模型 網狀模型 關係模型 最普及 關係 由行和列交叉組成的二維 一行為乙個元組,代表乙個實體 一列為乙個屬性 關...

python基礎入門一

python的變數是不用生宣告的,資料型別是系統自動判斷.print函式可以連續輸出中間逗號隔開.a 10 a 1.3 print a,type a sequence 序列 是一組有順序的元素的集合 序列有兩種 tuple 定值表 也有翻譯為元組 list 列表 s 1 2.1 print s可以對...