zoukankan      html  css  js  c++  java
  • orace如何创建函数并调用

    我们来定义一个oracle的函数

    create or replace function 方法名(参数名1 参数类型,参数名2 参数类型,参数名3 参数类型)return 返回类型
    is
     num_C number; --定义变量
    begin
         --处理函数的过程--
         --返回结果
         return num_C;
    end;

    如:我们来创建一个处理加、减、乘、除的计算函数

    /*** ****
    *** 说明:创建一个加法、减法、乘法、除法的计算函数
    **  参数:num_A 数字型参数A,num_B 数字型参数B,numType 计算类型
    **  返回:数字类型
    ****/
    create or replace function fun_Test(num_A number,num_B number,numType number)return number
    is
     num_C number; --定义变量
    begin
         --计算类型为1 时,表示进行加法运算---
         if numType = 1 then
              num_C := num_A + num_B;
         end if;
         --计算类型为2 时,表示进行减法运算---
         if numType=2 then
              num_C := num_A - num_B;
         end if;
          --计算类型为3 时,表示进行乘法运算---
          if numType=3 then
              num_C := num_A * num_B;
           end if;
          --计算类型为4 时,表示进行除法运算---
         if numType=4 then
              num_C := num_A/num_B;
         end if;        
         --输出结果
         dbms_output.put_line('输出值:'|| num_C);
         return num_C;
    end;

    上面的处理函数用的if end if,也可用if elsif else end if进行处理(注意 不是else if ,是elsif)

         if numType=1 then
            num_C := num_A + num_B;
         elsif numType=2 then
            num_C := num_A - num_B;
         elsif numType=3 then
            num_C := num_A * num_B; 
         elsif numType=4 then
            num_C := num_A / num_B; 
         else
            --其它处理
         end if;

    执行创建后,可在数据的函数文件下看到

    那么怎么调用我们创建的计算函数呢?

    --执行加法运算--
    select fun_Test(1,3,1) 结果 from dual;
    --执行减法运算--
    select fun_Test(8,3,2) 结果 from dual;
    --执行乘法运算--
    select fun_Test(4,3,3) 结果 from dual;
    --执行除法运算--
    select fun_Test(6,3,4) 结果 from dual;

    也可以一起调用

    --执行加法、减法、乘法、除反运算--
    select fun_Test(1,3,1) 结果1,fun_Test(8,3,2) 结果2,fun_Test(4,3,3) 结果3,fun_Test(6,3,4) 结果4 from dual;

    结果如下

    附录一个生成单号函数方法

    create or replace function fun_DxcWorkNo(prefix varchar2,singStr varchar2,billType integer)return varchar2
    is
     billNo varchar(20);
     nowDate varchar(20);
    begin   
         --获取单号的当前年月日时分秒 190412090428 ---
         select to_char(Sysdate,'yyMMddHHmmss') into nowDate from dual;
         --组成单号的字符串 Rw190412090428_1---
         billNo:= prefix || nowDate || singStr|| billType;
         return billNo;
    end;

    注意事项:

    1) 如果函数的参数是字符串,那边它的数据类型是varchar2,而不是varchar2(20)

    2) 函数定义的返回类型是什么类型,就得return 什么类型

    3)如果包含if 判断,记得是if-elsif  不是 if-else if

  • 相关阅读:
    有关tensorflow一些问题
    一个完整openlayer的例子,包括marker,popup等
    OpenLayers中的Layer概念和实践--Openlayers调用WMS服务
    MapInfo格式转arggis格式
    arcgis for server 登陆manager失败解决办法
    1752:鸡兔同笼(2.1)
    1749:数字方格(2.1)
    08:石头剪刀布(1.6)
    c++中的243、251、250错误原因
    05:年龄与疾病(1.6)
  • 原文地址:https://www.cnblogs.com/xielong/p/10715486.html
Copyright © 2011-2022 走看看