zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

ORACLE 存储过程

Oracle存储 过程
2023-09-11 14:14:42 时间
前奏:
1:必要的概念:
    ORACLE  提供能够把 PL/SQL  程序存储在数据库中。并能够在不论什么地方来执行它。这样就叫存储过
    程或函数。

过程和函数统称为 PL/SQL 子程序,他们是被命名的 PL/SQL 块,均存储在数据库中,并 通过输入、输出參数或输入/输出參数与其调用者交换信息。

过程和函数的唯一差别是函数总向调 用者返回数据,而过程则不返回数据。

2:建立存储过程 在 ORACLE SERVER 上建立存储过程,能够被多个应用程序调用,能够向存储过程传递參数,也能够向存储 过程传回參数 创建过程语法: CREATE [OR REPLACE] PROCEDURE Procedure_name [ (argment [ { IN | IN OUT }] Type, argment [ { IN | OUT | IN OUT } ] Type ] [ AUTHID DEFINER | CURRENT_USER ] { IS | AS } <类型.变量的说明> BEGIN <执行部分> EXCEPTION <可选的异常错误处理程序> END; 3:例题: --定义一个存储过程,获取给定部门的工资总和(通过out參数)。要求部门号和工资总和定义为參数 create or replace procedure get_sal3(dept_id number , sum_salary out number) is cursor salary_cursor is select salary from employees where department_id = dept_id; begin sum_salary := 0; for c in salary_cursor loop sum_salary := sum_salary + c.salary; end loop; dbms_output.put_line(sum_salary); end; select get_sal3(60,) --对给定部门的员工进行加薪操作。若其到公司的时间是在(?-95)期间,为其加薪5% --(95,98)加薪3%,(98,如今)加薪1%,得到下面返回结果: --此次加薪公司每一个月额外付出多少成本(定义一个out型的输出參数) create or replace procedure get_money(dept_id number , temp_sal out number) is v_i number(4,2):=0; cursor salary_cursor is select employee_id , salary ,hire_date from employees where department_id = dept_id; begin temp_sal := 0; for c in salary_cursor loop if to_char(c.hire_date,'yyyy')<'1995' then v_i := 0.05; elsif to_char(c.hire_date,'yyyy')<'1998' then v_i := 0.03; else v_i := 0.01 ; end if; --更新工资 update employees set salary = salary * (1+v_i) where employee_id = c.employee_id; --计算付出的成本 temp_sal := temp_sal + c.salary*v_i; end loop; dbms_output.put_line(temp_sal); end; 4:调用存储过程: declare v_temp number(10):=0; begin get_money(80,v_temp); end;