Oracle --存储函数和存储过程

oracle提供可以将pl/sql程序块存储在数据库中,并可以在任何地方运行它,这种pl/sql程序块称为存储过程或函数。

存储过程和函数的区别:函数需要向调用者返回数据,而过程不需要返回数据。

1.创建函数

create or replace function getAllSalary(i_cstmId in t_consumption.csptn_id%type)
  return number is
  v_sum number;
begin
  select sum(amount)
    into v_sum
    from t_consumption tcm
   where tcm.cstm_id = i_cstmId;
  return v_sum;
end getAllSalary;

调用函数

在函数或存储过程里面均可调用函数

declare
        v_mount number;
begin
        --SQL语句
         v_mount := getAllSalary(100000001);        
end;

2.创建存储过程

create or replace procedure getAllSalary(i_cstmId in t_consumption.csptn_id%type,
                                                             o_amount number)
is
        v_sum number;
begin
  select sum(amount)
    into v_sum
    from t_consumption tcm
   where tcm.cstm_id = i_cstmId; 
   --给输出赋值
   o_amount = v_sum;   
end getAllSalary;

调用存储过程

declare
        v_mount number;
begin
        --执行SQL语句
        --这里是在存储过程里面调用存储过程
        getAllSalary(i_cstmId => '100000001', o_amount => v_mount);        
end;

3. 删除函数或存储过程

可以使用drop procedure命令对不需要的存储过程进行删除,语法如下:

drop procedure 存储过程名;

可以使用drop function命令对不需要的函数进行删除,语法如下:

drop function 函数名;




你可能感兴趣的:(Oracle --存储函数和存储过程)