【原】Oracle-pl/sql-函数和存储过程

(注:主要摘自尚硅谷oracle教程笔记)

oracle 提供可以把 PL/SQL 程序存储在数据库中,并可以在任何地方来运行它。这样就叫储存过程和函数。过程和函数统称为 PL/SQL 子程序,他们是被命名的 PL/SQL块,均存储在数据库中,并通过输入、输出参数或输入/输出参数与其调用者交换信息。过程和函数的唯一区别是函数总向调用者返回数据,而过程则不返回数据。

函数

格式

--函数的声明(有参数的写在小括号里)
create or replace function func_name(v_param varchar2)
	--返回值类型
	return varchar2
is 
	--PL/SQL块变量、记录类型、游标的声明(类似于前面的declare的部分)
begin
	--函数体(可以实现增删改查等操作,返回值需要return)
       return 'helloworld'|| v_logo;
end;

说明

  1. OR REPLACE 为可选,有了它,可以或者创建一个新函数或者替换相同名字的函数,而不会出现冲突。
  2. 函数名后面是一个可选的参数列表,其中包含 IN,OUT 或 IN OUT 标记。参数之间用逗号隔开。 IN 参数标记表示传递给函数的值在该函数执行中不改变; OUT 标记表示一个值在函数中进行计算并通过该参数传递给调用语句,IN OUT 标记表示传递给函数的值可以变化并传递给调用语句。若省略标记,则参数隐含为 IN。
  3. 因为函数需要返回一个值,所以 RETURN 包含返回结果的数据类型。

示例
函数的 helloworld: 返回一个 “helloworld” 的字符串

create or replace function hello_func
return varchar2
is
begin
       return 'helloworld';
end;

执行函数

begin
    dbms_output.put_line(hello_func());
end;
或者:
select hello_func() from dual;

返回一个"helloworld: atguigu"的字符串,其中atguigu 由执行函数时输入。

--函数的声明(有参数的写在小括号里)
create or replace function hello_func(v_logo varchar2)
--返回值类型
return varchar2
is 
--PL/SQL块变量的声明
begin
--函数体
       return 'helloworld'|| v_logo;
end;

创建一个存储函数,返回当前的系统时间

create or replace function func1
return date
is
--定义变量
v_date date;
begin
	--函数体
	--v_date := sysdate;
       select sysdate into v_date from dual;
       dbms_output.put_line('我是函数哦');
       
       return v_date;
end;

执行

执行法1:
select func1 from dual;
执行法2:
declare
  v_date date;
begin
  v_date := func1;
  dbms_output.put_line(v_date);
end;

定义带参数的函数: 两个数相加

create or replace function add_func(a number, b number)
return number
is
begin
       return (a + b);
end;

执行

begin
    dbms_output.put_line(add_func(12, 13));
end;
或者
    select add_func(12,13) from dual;

定义一个函数: 获取给定部门的工资总和, 要求:部门号定义为参数, 工资总额定义为返回值.

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;

执行

begin
    dbms_output.put_line(sum_sal(80));
end;

关于 OUT 型的参数: 因为函数只能有一个返回值, PL/SQL 程序可以通过 OUT 型的参数实现有多个返回值

要求: 定义一个函数: 获取给定部门的工资总和 和 该部门的员工总数(定义为 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; 

执行

delare 
  v_total number(3) := 0;

begin
    dbms_output.put_line(sum_sal(80, v_total));
    dbms_output.put_line(v_total);
end;

存储过程

在 ORCALE SERVER 上建立存储过程,可以被多个应用程序调用,乐意向存储过程传递参数,也可以向存储过程传回参数。

示例
删除指定员工记录

create or repalce procedure del_emp(
	v_empid in employees.employee_id%type)
is
 no_result exception;
 begin
 	delete from employees
 	where employee_id = v_empid;
 	if sql%notfound then
 		raise no_result;
 	end if;

	dbms_output.put_line('编号为:'
		|| v_empid || '的员工已被除名');
exception
	when no_result then
		dbms_output.put_line('您要删除的数据不存在');
	when others then
		dbms_output.put_line(sqlcode || '--' || sqlerrm);
end;

定义一个存储过程: 获取给定部门的工资总和(通过 out 参数), 要求:部门号和工资总额定义为参数

create or replace procedure sum_sal_procedure(
	dept_id number, v_sum_sal out number)
       is
       cursor sal_cursor is select salary 
       		from employees where department_id = dept_id;
begin
       v_sum_sal := 0;
       
       for c in sal_cursor loop
           --dbms_output.put_line(c.salary);
           v_sum_sal := v_sum_sal + c.salary;
       end loop;       

       dbms_output.put_line('sum salary: ' || v_sum_sal);
end;

执行

declare 
     v_sum_sal number(10) := 0;
begin
     sum_sal_procedure(80,v_sum_sal);
end;

对给定部门(作为输入参数)的员工进行加薪操作, 若其到公司的时间在 (? , 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;

调用存储过程

 ORACLE 使用 EXECUTE 语句来实现对存储过程的调用
 EXEC[UTE] Proceduce_name(parameter1,parameter2...)

删除存储过程

DROP FUNCTION [user.]Function_name;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值