【问题标题】:Comparing Date type in Oracle PACKAGE does not work比较 Oracle PACKAGE 中的日期类型不起作用
【发布时间】:2015-01-01 03:19:09
【问题描述】:

我制作了一个如下所示的 Oracle 包。

我将传递参数字符串,如“2014-11-05”。

--SEARCH 2014 11 04
FUNCTION SEARCHMYPAGE(v_created_after IN DATE, v_created_before IN DATE)
return CURSORTYPE is rtn_cursor CURSORTYPE; 
BEGIN
OPEN
rtn_cursor FOR
  select 
    news_id  
  from
    (
    select 
      news_id,
      news_title, news_desc,
      created, news_cd
    from
      news
    )
  where
  1=1
  AND (created BETWEEN decode(v_created_after, '', to_date('2000-01-01', 'YYYY-MM-DD'), to_date(v_created_after, 'YYYY-MM-DD'))
  AND (decode(v_created_before, '', sysdate, to_date(v_created_before, 'YYYY-MM-DD')) + 0.999999));
return rtn_cursor ;
END SEARCHMYPAGE;

我在 Eclipse 控制台消息中确认了我的参数,因为我正在使用 Eclipse IDE。 我得到了2014-10-29~2014-10-31制作的内容。

当我将 '2014-11-01' 作为 created_after 传递时,它返回 0 条记录。(但我期望所有内容,因为每个内容都是在 10-29 和 10-31 之间制作的)

你会发现我的函数有什么问题吗?

谢谢:D

【问题讨论】:

    标签: java sql oracle date package


    【解决方案1】:
    create function search_my_page(p_created_after in date, p_created_before in date)
    return cursortype
        is rtn_cursor cursortype; 
    begin
        open rtn_cursor for
        select news_id  
        from news
        where created between
            nvl(v_created_after, date '1234-01-01')
            and
            nvl(v_created_before, sysdate) + interval '1' day - interval '1' second;
    
        return rtn_cursor;
    end search_my_page;
    /
    

    变化:

    1. 重新编写谓词 - 括号放错位置改变了含义。
    2. 将 to_date 替换为日期文字和变量。由于您已经在使用 ANSI 日期格式,因此不妨使用文字。并且日期变量不需要强制转换为日期。
    3. 将 DECODE 替换为更简单的 NVL。
    4. 删除了多余的括号。
    5. 将 v_ 重命名为 p_。通常使用 p_ 表示“参数”,使用 v 表示“(局部)变量”。
    6. 删除了额外的内联视图。通常内联视图没有得到充分利用,在这种情况下,它似乎没有多大帮助。
    7. 删除了不必要的 1=1。
    8. 将 0.99999 替换为日期间隔,以使数学更清晰。
    9. 更改为小写(这不是 COBOL),在函数名称中添加了下划线。
    10. 将 2000-01-01 更改为 1234-01-01。如果您使用魔法值,它应该看起来不寻常 - 不要试图隐藏它。

    【讨论】:

    • 感谢有意义的回答。我的问题解决了,我得到了额外的提示:D b
    猜你喜欢
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    相关资源
    最近更新 更多