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

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

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

1.建立函数
oracle

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;

调用函数函数

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

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

2.建立存储过程get

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;

调用存储过程io

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

3. 删除函数或存储过程
function

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

drop procedure 存储过程名;select

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

drop function 函数名;

相关文章
相关标签/搜索