ORACLE 提供可以把 PL/SQL 程序存储在数据库中,并可以在任何地方来运行它。这样就叫存储过程或函数。
存储函数:有返回值,创建完成后,通过select function() from dual;执行
存储过程:由于没有返回值,创建完成后,不能使用select语句,只能使用pl/sql块执行
一.存储函数
1.存储函数语法格式
CREATE [OR REPLACE] FUNCTION function_name
[ (argment [ { IN | IN OUT } ] Type,
argment [ { IN | OUT | IN OUT } ] Type )]
--返回值类型
RETURN return_type
IS
--PL/SQL块变量、记录类型、游标的声明(类似于前面的declare的部分)
BEGIN
--函数体(可以实现增删改查等操作,返回值需要return)
return 返回值;
EXCEPTION
--异常捕获
END;
注:. IN : 表示传递给函数的值在该函数执行中不改变;
OUT : 表示一个值在函数中进行计算并通过该参数传递给调用语句;
IN OUT: 表示传递给函数的值可以变化并传递给调用语句.
若省略标记, 则参数隐含为 IN
【例1】不带参函数
要求:函数的 helloworld: 返回一个 "helloworld" 的字符串


create or replace function hello_func
return varchar2
is
begin
return 'helloworld';
end;
View Code
执行函数


begin
dbms_output.put_line(hello_func());
end;
View Code
【例2】带参函数
要求:返回一个"helloworld: atguigu"的字符串,其中atguigu 由执行函数时输入。


--函数的声明(有参数的写在小括号里)
create or replace function hello_func(v_logo varchar2)
--返回值类型
return varchar2
is
--PL/SQL块变量的声明
begin
--函数体
return 'helloworld'|| v_logo;
end;
View Code
【例3】
要求:获取给定部门的工资总和, 要求:部门号定义为参数, 工资总额定义为返回值.


create or replace function sum_sal(dept_id number)
return number
is
cursor sal_cursor is select salary from employees where department_id = dept_id;
v_sum_sal number(8) := 0;
begin
for c in sal_cursor loop
v_sum_sal := v_sum_sal + c.salary;
end loop;
--dbms_output.put_line('sum salary: ' || v_sum_sal);
return v_sum_sal;
end;
View Code
执行函数


begin
dbms_output.put_line(sum_sal(80));
end;
View Code
2. OUT 型参数
因为函数只能有一个返回值, PL/SQL 程序可以通过 OUT 型的参数实现有多个返回值
【例4】
要求: 定义一个函数: 获取给定部门的工资总和 和 该部门的员工总数(定义为 OUT 类型的参数). 部门号定义为参数, 工资总额定义为返回值.


create or replace function sum_sal(dept_id number, total_count out number)
return number
is
cursor sal_cursor is select salary from employees where department_id = dept_id;
v_sum_sal number(8) := 0;
begin
total_count := 0;
for c in sal_cursor loop
v_sum_sal := v_sum_sal + c.salary;
total_count := total_count + 1;
end loop;
--dbms_output.put_line('sum salary: ' || v_sum_sal);
return v_sum_sal;
end;
View Code
执行函数


delare
v_total number(3) := 0;
begin
dbms_output.put_line(sum_sal(80, v_total));
dbms_output.put_line(v_total);
end;
View Code
3.删除函数
DROP FUNCTION [user.]Function_name;
二.存储过程
1.存储过程语法格式
CREATE [OR REPLACE] PROCEDURE Procedure_name
[ (argment [ { IN | IN OUT } ] Type,
argment [ { IN | OUT | IN OUT } ] Type )]
IS
--PL/SQL块变量、记录类型、游标的声明(类似于前面的declare的部分)
BEGIN
--函数体(可以实现增删改查等操作,返回值需要return)
EXCEPTION
--异常捕获
END;
【例5】
要求:对给定部门(作为输入参数)的员工进行加薪操作, 若其到公司的时间在 (? , 95) 期间, 为其加薪 %5
[95 , 98) %3
[98, ?) %1
得到以下返回结果: 为此次加薪公司每月需要额外付出多少成本(定义一个 OUT 型的输出参数).


create or replace procedure add_sal_procedure(dept_id number, temp out number)
is
cursor sal_cursor is select employee_id id, hire_date hd, salary sal from employees where department_id = dept_id;
a number(4, 2) := 0;
begin
temp := 0;
for c in sal_cursor loop
a := 0;
if c.hd < to_date('1995-1-1', 'yyyy-mm-dd') then
a := 0.05;
elsif c.hd < to_date('1998-1-1', 'yyyy-mm-dd') then
a := 0.03;
else
a := 0.01;
end if;
temp := temp + c.sal * a;
update employees set salary = salary * (1 + a) where employee_id = c.id;
end loop;
end;
View Code
2.删除存储过程
DROP PROCEDURE [user.]Procudure_name;