【问题标题】:'Different results for the same function called from SQL Developer UI and in SQL query'从 SQL Developer UI 和 SQL 查询中调用的相同函数的不同结果
【发布时间】:2019-08-06 09:51:10
【问题描述】:

F 是一个计算两个日期之间的工作日数的函数(使用格式 to_date(a, 'DD/MM/YYYY') [NLS_DATE_LANGUAGE = "FRENCH"]。 它存储在包P中。

a 是期间的开始日期,b 是结束日期。

当我使用 SQL developer 运行函数时,即右键单击包p,执行,选择f 函数并填写参数:

if a = to_date('02/08/2019', 'DD/MM/YYYY') and b = to_date('12/08/2019', 'DD/MM/YYYY'))

我得到的天数 = 7,这是正确的结果。

但是当我在 SQL 中运行它时:

select p.f('02/08/2019','12/08/2019') from DUAL

我得到的天数 = 8,这个结果显然是错误的。

我无法理解这两个进程如何返回两个不同的值,因为调用了相同的F 函数,并且参数完全相同。

我一直注意日期格式,仍然两种调用f的方式不会返回相同的结果

仅供参考,这是这个函数的内容(对不起,cariable是用法语表示的)

FUNCTION getjoursouvres
(
    i_debut IN DATE,
    i_fin IN DATE   
)
RETURN NUMBER IS o_result NUMBER;
v_jour date;
v_nbjours NUMBER;
v_testferie number;
v_testweekend number;
v_testglobal number;
-- This cursor browses all dates between i_debut and i_fin (included)
cursor cx is 
            select to_date(i_debut, 'DD/MM/YYYY') + rownum -1 dt 
            from dual 
            connect by level <= to_date(i_fin, 'DD/MM/YYYY') - to_date(i_debut, 'DD/MM/YYYY') + 1;
BEGIN
open cx;
v_nbjours := 0;
    loop
-- Browses all the days in the interval (begining and end included)
        fetch cx into v_jour;
        exit when cx%NOTFOUND;
-- testferie return 1 if the day is NOT a holiday, 0 if it is (so not be be added)
            v_testferie := testferie(v_jour);
-- testweekend return 1 if the day is a weekday, 0 if it is on weekend (so not be be 
            v_testweekend := testweekend(v_jour);
-- 
            v_testglobal := v_testferie + v_testweekend;
-- If v_testglobal = 2 then the day is neither weekend nor holiday. Therefore it is aded to the sum of days
            if v_test = 2 then
                v_nbjours := v_nbjours + 1;
            end if;
    end loop;
o_result := v_nbjours;
close cx;
return o_result;
END;

【问题讨论】:

  • 请您编辑您的问题以包含您的函数f的内容。
  • select f.p(a,b) from DUAL - 在这种情况下,ab 是什么?

标签: oracle plsql oracle-sqldeveloper


【解决方案1】:

经过多次调用两种方式都可以使用该功能。得出的结论是,引起异常的因素来自于

to_date(v_date, 'DD/MM/YYYY')

其中 v_date 已被声明为日期格式,其行为方式无法预测。 我决定保留 date 变量,只使用 to_date 用于不同的 cariable 格式。

我无法准确解释原因,但它解决了我关于日期行为的问题。现在,我可以从 DUAL 或 SQL 开发人员界面调用带有数据参数的函数,并获得相同且连贯的结果。

【讨论】:

  • “其中 v_date 已被声明为日期格式” 您的意思是声明为 日期 吗?日期没有任何格式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
  • 2021-05-17
  • 2019-06-25
  • 1970-01-01
相关资源
最近更新 更多