Home  >  Article  >  Java  >  Oracle stored procedure and stored function creation example tutorial

Oracle stored procedure and stored function creation example tutorial

零下一度
零下一度Original
2017-06-25 10:56:471857browse
select * from emp;
-----------------存储过程------------------------
--定义
   create[or replace] procedure 存储过程名称(参数名 [in]/out 数据类型) 
   is/as
   begin
     --逻辑表达式 
   end [存储过程名称];
       
       
--定义存储过程计算年薪,并答应输出
  create or replace procedure proc_salyears(v_no in number)
   is
   sal_years number(9,2);
   begin
     --计算年薪
     select sal*12+nvl(comm,0) into sal_years from emp where empno=v_no;
     
     --输出
     dbms_output.put_line(sal_years);
   end;
 
--调用存储过程
   方式1:
     call proc_salyears(7788);
   方式2:
     begin
       proc_salyears(7369);
     end;
     
--out参数的存储过程
--计算年薪并返回   
  create or replace procedure proc_salyears(v_no in number,sal_years out number)
   is
   begin
     --计算年薪
     select sal*12+nvl(comm,0) into sal_years from emp where empno=v_no;
   end;
  --调用存储过程
  declare
   v_sal number(9,2);
  begin
     proc_salyears(7876,v_sal);
     dbms_output.put_line(v_sal);
  end;
-----------------存储函数------------
--定义
  create or replace function 存储函数名(参数名 in/out 数据类型)
    return 数据类型
    is|as
    begin
        return 具体的数据;
    end [存储函数名称];
   
--定义存储函数名计算年薪
  create or replace function fun_salyears(f_no number)
    return number
    is
  sal_years number(9,2);
    begin
    select sal*12+nvl(comm,0) into sal_years from emp where empno=f_no;
        return sal_years;
    end ;
--使用存储函数
  declare
  sal_yeats number(9,2);
  begin
    sal_yeats := fun_salyears(7876);
    dbms_output.put_line(sal_yeats);
  end;
  --可简写
  begin
    dbms_output.put_line(fun_salyears(7369));
  end;

--------存储过程和存储函数的区别--------
存储过程多用于项目之间的数据共享,存储函数多被存储过程调用.
存储函数可以再sql语句中调用,存储过程不能.

 

The above is the detailed content of Oracle stored procedure and stored function creation example tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn