通过添加嵌入到PL-SQL(Oracle函数)中的select子句来处理异常

问题描述 投票:0回答:1

我必须在oracle中设计一个函数(日期输入,数字输出),在给定条件下执行以下三种情况:

情况1:如果input_date与monday_date相匹配,则以monday_date作为输入参数执行query1(从哪里选择..和..)。

case2:如果来自case(1)的查询未返回任何行,则创建新变量max_input_date1 = input_date + 1,然后执行query2(将table.1中的..select从左连接table2 ..中选择),并将monday_date和max_input_date1作为输入参数。] >

case3:如果来自case(2)的查询未返回任何行,则创建新变量max_input_date2 = input_date + 4,然后执行query2(将monday_date和max_input_date2作为输入参数,并从table1左连接table2 ..select中进行选择。) >

我正在尝试使用异常-NO_DATA_FOUND移至每种情况,但看起来oracle函数中的异常子句不会执行任何“选择”语句-我不确定,但它对我不起作用,或者我做错了。函数进入异常子句后,不会返回任何内容。

我正在寻找有关如何使之有效的建议,无论是否使用例外。请帮忙。到目前为止,这就是我的功能。

CREATE OR
    REPLACE FUNCTION get_dept_no(input_date string)
    RETURN NUMBER
    IS
    dept_no NUMBER;

    max_input_date1 date ;
    max_input_date2 date ;

    delta1 integer := 1;
    delta2 integer := 4;

BEGIN
    SELECT num
    INTO dept_no
    FROM table1
    WHERE aperiod = 201910
      AND trunc(monday_date) = to_date(input_date, 'yyyy-mm-dd');
    RETURN dept_no;
EXCEPTION
     WHEN NO_DATA_FOUND THEN
            max_input_date1 := to_date(input_date, 'yyyy-mm-dd') + delta1;

            WITH max as (
                SELECT *
                FROM table2 adm
                GROUP BY aperiod, num
                )

                SELECT
                       ma.num INTO dept_no
                FROM max ma
                LEFT JOIN table1 yw
                  ON ma.aperiod = yw.aperiod
                  AND ma.num = yw.num
                WHERE trunc(ma.DOJ_date) >= to_date(input_date, 'yyyy-mm-dd')
                  AND trunc(ma.DOJ_date) <= to_date(max_input_date1, 'yyyy-mm-dd');
            raise;
            WHEN NO_DATA_FOUND THEN
                max_input_date2 := to_date(input_date, 'yyyy-mm-dd') + delta2;
                WITH max as (
                SELECT *
                FROM table2 adm
                GROUP BY aperiod, num
                )

                SELECT
                       ma.num INTO dept_no
                FROM max ma
                LEFT JOIN table1 yw
                  ON ma.aperiod = yw.aperiod
                  AND ma.num = yw.num
                WHERE trunc(ma.DOJ_date) >= to_date(input_date, 'yyyy-mm-dd')
                  AND trunc(ma.DOJ_date) <= to_date(max_input_date2, 'yyyy-mm-dd');

return dept_no;
END get_dept_no;

我必须在oracle中设计一个函数(日期输入,数字输出),在给定条件下执行以下三种情况:case1:如果input_date与monday_date匹配,则执行query1(select from ...

oracle plsql stored-functions
1个回答
0
投票

我倾向于编写很小的函数,它们只完成一件事;有时令人惊讶地涉及更多的代码,但是却消除了复杂性-似乎是一个矛盾,但并非如此。在这种情况下,我将破坏它的3个内部功能和一个简单的main。 (是的,您可以在函数内部编写函数)。

create or
    replace function get_dept_no(input_date string)
    return number
    is
    dept_no number;

    max_input_date1 date ;
    max_input_date2 date ;

    delta1 integer := 1;
    delta2 integer := 4;

    function get_dept_q1
      return number
        dept_q1_result number;
    begin 
        select num
          into dept_q1_result
          from table1
         where aperiod = 201910
           and trunc(monday_date) = to_date(input_date, 'yyyy-mm-dd');
        return dept_q1_result;
    exception 
       when no_data_found then 
            return null; 
    end get_dept_q1;  


    function get_dept_q2
      return number
        dept_q2_result number;
    begin 
      max_input_date1 := to_date(input_date, 'yyyy-mm-dd') + delta1;

      with max as (
           select *
             from table2 adm
            group by aperiod, num
            )
       select ma.num  
        into dept_no
        from max ma
        left join table1 yw
             on  ma.aperiod = yw.aperiod
             and ma.num = yw.num
         where trunc(ma.doj_date) >= to_date(input_date, 'yyyy-mm-dd')
           and trunc(ma.doj_date) <= to_date(max_input_date1, 'yyyy-mm-dd');
        return dept_q2_result;         
    exception 
       when no_data_found then 
            return null; 
    end get_dept_q2;  

    function get_dept_q3
      return number
        dept_q3_result number;
    begin 
      max_input_date1 := to_date(input_date, 'yyyy-mm-dd') + delta1;

      with max as (
           select *
             from table2 adm
            group by aperiod, num
            )
       select ma.num  
        into dept_q3_result
        from max ma
        left join table1 yw
              on  ma.aperiod = yw.aperiod
             and ma.num = yw.num
         where trunc(ma.doj_date) >= to_date(input_date, 'yyyy-mm-dd')
           and trunc(ma.doj_date) <= to_date(max_input_date1, 'yyyy-mm-dd');
         return dept_q1_result;
    exception 
       when no_data_found then 
            return null; 
    end get_dept_q3;       

-- MAIN    
begin
   dept_no = get_dept_q1;

   if dept_no is null 
   then 
      dept_no = get_dept_q2;
   end if;

   if dept_nun is null 
   then 
      dept_no = get_dept_q3;
   end if;                                                     

   if dept_num is null 
   then 
      raise no_data_found; 
   end if;

   return dept_no;
end get_dept_no; 
© www.soinside.com 2019 - 2024. All rights reserved.