码迷,mamicode.com
首页 > 数据库 > 详细

Oracle笔记 十一、PL/SQL函数和触发器

时间:2015-10-13 00:07:49      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

--创建函数
create or replace function add_sal(sSal number)
       return number
is
begin
  if (sSal > 5000) then
    return sSal + 51;
  elsif (sSal > 3000) then
    return sSal + 111;
  else
    return sSal + 222;
  end if;
end;
 
select sal, add_sal(sal) from emp;
 
 
--触发器
create table deptLog(
       uName varchar2(20),
       action varchar2(20),
       dTime date
);
 
--创建触发器
--for each row 可以触发多条,当你的语言影响多少条记录就会触发多少次
create or replace trigger trig_dept2
  after insert or delete or update on dept2/* for each row*/
begin
  if inserting then
    insert into deptLog values(user, ‘insert‘, sysdate);
  elsif updating then
    insert into deptLog values(user, ‘update‘, sysdate);
  elsif deleting then
    insert into deptLog values(user, ‘delete‘, sysdate);
  end if; 
end;
 
select * from dept2;
select * from deptLog;
insert into dept2 values(55, ‘SOFTWARE‘, ‘cic‘);
update dept2 set loc = ‘go‘ where deptno in(30);
delete dept2 where deptno = 55;
 
--触发器
create or replace trigger trig_emp
  after update on dept for each row
begin
  update emp set emp.deptno =: new.deptno where emp.deptno = :old.deptno;
end;
 
update dept set deptno = 11     where deptno = 10;
select * from emp;
 
---语句级触发器
create or replace trigger trg_ins_dept2
before insert
on dept2
begin
     if user not in(‘SCOTT‘) then      
       raise_application_error(-20001, ‘只有SCOTT才能修改该表!‘);
     end if;
end;
 
禁用、启用触发器
alter trigger tgr_Name disable | enable;
alter table tableName disable all trigger | enable all trigger;

Oracle笔记 十一、PL/SQL函数和触发器

标签:

原文地址:http://www.cnblogs.com/bokxy/p/4873038.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!