Oracle学习笔记7

一. 存储过程

1) -- 创建存储过程

Create or replace procedure p

Is

         Cursor c is

                Select * from emp for update;

Begin

         For v_emp in c loop

                If (v_emp.deptno = 10) then

                       Update emp set sal = sal +10 where current of c;

                Elsif (v_emp.deptno = 20) then

                       Update emp set sal = sal +20 where current of c;

                Else

Update emp set sal = sal +50 where current of c;

                End if;

         End loop;

         Commit;

End;

-- 调用存储过程

Exec p;            -- 方法一

Begin        -- 方法二

         P;          

End;

2) -- 带参数的存储过程

Create or replace procedure p

--in 调用存储过程前先赋值, out 调用存储过程时赋值,没有 in/out 默认为 in

         (v_a in number, v_b number, v_ret out number, v_temp in out number)

Is

Begin

         If (v_a > v_b) then

                V_ret := v_a;

         Else

                V_ret := v_b;

         End if;

         V_temp := v_temp + 1;

End;

-- 调用带参数存储过程

Declare

         V_a number := 3;

         V_b number := 4;

         V_ret number;

         V_temp number := 5;

Begin

         P(v_a, v_b, v_ret, v_temp);

         Dbms_output.put_line(v_ret);

         Dbms_output.put_line(v_temp);

End;

3) 创建存储过程出错,用 show error 显示错误

4) -- 删除存储过程

Drop procedure p;

 

二. 过程

1) -- 创建过程

Create or replace function f

         (v_sal number)

         Return number

Is

Begin

         If (v_sal < 2000) then

                Return 0.10;

         Elsif (v_sal <2750) then

                Return 0.15;

                     Else

                            Return 0.20;

                     End if;

End;

-- 调用过程

Select f(sal) from emp;

 

三. 触发器

1) -- 创建触发器

Create or replace trigger t

         After insert or delete or update on emp for each row                   -- 没操作一行则触发一次

         --before insert or delete or update on emp for each row

Begin

         If inserting then

                Insert into emp_log values (USER, ‘insert’, sysdate);            --USER 当前用户

         Elsif updating then

                Insert into emp_log values (USER, ‘update’, sysdate);

         Elsif deleting then

                Insert into emp_log values (USER, ‘delete’, sysdate);

         End if;

         Commit;

End;

2) -- 利用触发器级联更新(很少使用)

Create or replace trigger t

         After update on dept for each row

Begin

         -- :NEW 新记录, :OLD 旧记录

         Update emp set deptno = :NEW.deptno where deptno = :OLD.deptno;

End;

3) 先触发触发器,再检验约束条件

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(oracle,user,delete,存储,insert,each)