• 自定义函数、存储过程


     一:函数

    create or replace function 函数名(参数1 模式 数据类型,......) return 数据类型
    as
      -- 定义局部变量。
      变量1 数据类型;
      ......
    begin
      -- 实现函数功能的PL/SQL代码。
      ......
      exception
      -- 异常处理的PL/SQL代码。
      ......
    end;

    1)参数的模式有三种:

      in:只读模式,在函数中,参数只能被引用/读取,不能改变它的值。

      out:只写模式,参数只能被赋值,不能被引用/读取。

      in out:可读可写。

      参数的模式可以不写,缺省为in,out和in out两种模式极少使用。

    2)as/is二选一,在这里没有区别。

    3)可以不定义局部变量。

    4)可以没有异常(exception)处理代码段。

    示例,创建自定义函数maxvalue,用于比较两个数字的大小,返回较大值:

    --自定义函数:maxvalue(),比较两个数的大小.
    create or replace function maxvalue(val1 number,val2 number) return number is
      FunctionResult number;
    begin
      if (val1>val2) then
          FunctionResult:=val1;
      else
          FunctionResult:=val2;
      end if;
      return(FunctionResult);
    end;

    调用函数:

      自定义函数是数据库对象,Oracle对它权限管理方式与其它数据库对象相同。如果maxvalue函数是用scott用户创建的,其它用户调用时需要加scott用户名前缀,否则报错:

    --调用函数
    select maxvalue(50,20) from dual;

    删除函数:

    --删除函数
    drop function maxvalue;

    二:存储过程:

    与函数大致一致,把 function 修改为:procedure

    调用、删除存储过程:

    --调用存储过程
    call addvalue(10,20);
    --删除存储过程
    drop procedure addvalue; 
  • 相关阅读:
    时间类型:datetime,timestamp,date,time,year
    字符串类型:char,varchar,text,enum,set
    RHEL7安装ZABBIX 3.2
    Go-06-数据类型、常量、运算符
    GO-05-数据类型
    GO-04-变量
    GO-03-基础
    GO-02-helloworld
    Ambassador-09-prefix正则表达式
    Ambassador-08-跨域
  • 原文地址:https://www.cnblogs.com/mxggx/p/14807888.html
Copyright © 2020-2023  润新知