PL/SQL函数和触发器

--创建函数
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;
posted @ 2012-05-09 15:58  hibernate3例子  阅读(272)  评论(0编辑  收藏  举报