查看当前日期的数据库中的函数符合此条件

时间:2017-04-25 08:48:23

标签: sql oracle function date plsql

在这种情况下,用户可以按计划访问。 我如何在oracle数据库中创建具有逻辑的函数:

  • 一个月一次访问(f1)
  • 两个月一次访问(f2)
  • 一周一次访问(f4)
  • 一周两次访问(f8)。

因此,我们可以查看当前日期符合该条件的数据。

包含表名的数据库中的示例是mst_callplan:

cust_no : 201, sales_no:001, frequent: f1, day: monday    
cust_no : 202, sales_no:001, frequent: f8, day: friday
cust_no : 203, sales_no:001, frequent: f2, day: wednesday
cust_no : 204, sales_no:001, frequent: f1, day: monday

cust_no,sales_no,frequency和day是列的名称。

从数据库中查看当前日期符合该条件的表格行。当前日期更改时,数据会自动显示。

示例结果为:null(当前日期:2017年4月25日)

帮我查看数据库中的功能。如何创建适合该条件的函数。

1 个答案:

答案 0 :(得分:0)

在这种情况下的功能是:

     FUNCTION get_data_sales(pi_trxn_dt IN DATE)
        RETURN slm_type_mst_callplan_tab IS

    l_slm_type_mst_callplan_rec slm_type_mst_callplan_rec;
    l_slm_type_mst_callplan_tab slm_type_mst_callplan_tab := slm_type_mst_callplan_tab();

    l_return PLS_INTEGER;

    ex_set_date_lang EXCEPTION;

    CURSOR cur_data IS
      SELECT cust_id
            ,sales_id
            ,mst_freq
            ,mst_day
            ,start_date
            ,end_date
            ,week_no
      FROM mst_callplan
      WHERE (upper(mst_freq) = 'F1' and week_no=4 AND
            MOD(slm_pkg_utl.cnt_week(pi_start_dt => g_start_dt
                                     ,pi_end_dt   => pi_trxn_dt)
                ,4) = 0 AND upper(TRIM(mst_day)) = 
            upper(TRIM(to_char(pi_trxn_dt,'Day'))))

           OR
           (upper(mst_freq) = 'F1'and week_no=3 AND
            MOD(slm_pkg_utl.cnt_week(pi_start_dt => g_start_dt
                                     ,pi_end_dt   => pi_trxn_dt)
                ,4) = 3 AND upper(TRIM(mst_day)) = 
            upper(TRIM(to_char(pi_trxn_dt,'Day'))))

           OR
           (upper(mst_freq) = 'F1'and week_no=2 AND
            MOD(slm_pkg_utl.cnt_week(pi_start_dt => g_start_dt
                                     ,pi_end_dt   => pi_trxn_dt)
                ,4) = 2 AND upper(TRIM(mst_day)) = 
            upper(TRIM(to_char(pi_trxn_dt,'Day'))))

           OR
           (upper(mst_freq) = 'F1'and week_no=1 AND
            MOD(slm_pkg_utl.cnt_week(pi_start_dt => g_start_dt
                                     ,pi_end_dt   => pi_trxn_dt)
                ,4) = 1 AND upper(TRIM(mst_day)) = 
            upper(TRIM(to_char(pi_trxn_dt,'Day'))))

            OR
            (upper(mst_freq) = 'F2' and week_no=2 AND
            MOD(slm_pkg_utl.cnt_week(pi_start_dt => g_start_dt
                                     ,pi_end_dt   => pi_trxn_dt)
                ,2) = 0 AND upper(TRIM(mst_day)) =
            upper(TRIM(to_char(pi_trxn_dt
                                               ,'Day'))))
            OR
            (upper(mst_freq) = 'F2' and week_no=1 AND
            MOD(slm_pkg_utl.cnt_week(pi_start_dt => g_start_dt
                                     ,pi_end_dt   => pi_trxn_dt)
                ,2) = 1 AND upper(TRIM(mst_day)) =
            upper(TRIM(to_char(pi_trxn_dt,'Day'))))

            OR (upper(mst_freq) = 'F4' AND
            upper(TRIM(mst_day)) =
            upper(TRIM(to_char(pi_trxn_dt
                                  ,'Day'))))
            OR (upper(mst_freq) = 'F8' AND
            upper(TRIM(mst_day)) =
            upper(TRIM(to_char(pi_trxn_dt
                                  ,'Day'))));

  BEGIN
    set_date_language(po_return => l_return);
    IF l_return <> 0 THEN
      RAISE ex_set_date_lang;
    END IF;

    FOR rec_dt IN cur_data
    LOOP
      l_slm_type_mst_callplan_tab.extend;
      l_slm_type_mst_callplan_tab(l_slm_type_mst_callplan_tab.last) := slm_type_mst_callplan_rec(rec_dt.cust_id
                                                                                                ,rec_dt.sales_id
                                                                                                ,rec_dt.mst_freq
                                                                                                ,rec_dt.mst_day
                                                                                                ,rec_dt.start_date
                                                                                                ,rec_dt.end_date
                                                                                                ,rec_dt.week_no);

    END LOOP;
    RETURN l_slm_type_mst_callplan_tab;
  EXCEPTION
    WHEN ex_set_date_lang THEN
      dbms_output.put_line('Error set date languange');
    WHEN OTHERS THEN
      l_slm_type_mst_callplan_tab.delete;
  END get_data_sales;