进程 触发器

1.进程是相当于一个后台运行,赋予一个值,他能给出相关的结果。

  --创建一个过程 "给你一个员工号 你给我返回他的年薪"

    create or replace procedure  nianxin(eno in number, psal out number)        --'nianxin'是进程的名字 eno输入数据  in 代表输入值  number 类型 psal输出数据

    as                                               ---进程之中不用   declare

     csal emp.sal%type;                  

     ccomm emp.comm%type;

    begin

      select sal ,comm into csal,ccomm from emp where empno=eno;

      psal :=sal *12 + nvl( ccomm,0);                --注意 nvl 的作用

    end;

    /

    --------------运用进程

   declare

      psal number;

      begin

      nianxin(7566,psal);               ------psal是返回值, 7566是赋予值

      dbms_output.put_line(psal);

      end;

      /

------------- ---------------触发器: 建立一个触发器,当发现定义的操作时,给出 回应

     建立:周四 周天 9:00-18:00 时间段不能 insert 操作

create or replace trigger empinert   ----------------trigger 关键字

 before                      -----------------before  关键字 (before  ,  after )之前 之后

insert                         -------------------触发操作

on emp                         ------------------------基于哪个表

begin

if to_char(sysdate,'day') = '星期四' or to_char(sysdate,'day') ='星期天' or to_char(sysdate,'hh24') not between 9 and 18 then raise_application_error(-20003,'不能在非上班时间做插入操作');          -----to_char 将格式转换 只取 一部分

end if;

end;

/

-------------------监控每个部门不能超过 5 个人

create or replace trigger empinsert

before

insert/delete/update (of) 列名

on emp

for each row (when)条件

declare

      pnumber number;

begin

     select count(*) into pnumber from emp where deptno = :new.deptno;    -------------:new.deptno   来源于更新后数据下的部门号

     if pnumber >= 5 then raise_application_error(-20002,'每个部门只能有5各员工');

     end if;

end;

/

     

原文地址:https://www.cnblogs.com/savepoint/p/5316323.html