简体   繁体   English

Oracle - 比较日期

[英]Oracle - Comparing dates

I have this Oracle procedure: 我有这个Oracle程序:

CREATE OR REPLACE PROCEDURE xmas
IS
    CURSOR curUser IS SELECT userID, coins FROM Users;
    thisUser curUser%ROWTYPE;
    d VARCHAR(7);
    CURSOR curDate IS SELECT TO_CHAR(sysdate, 'DD-MON') FROM DUAL;
BEGIN
    OPEN curDate;
    FETCH curDate INTO d;
    CLOSE curDate;
    IF ((TO_DATE(d)) = (TO_DATE('25-DEC'))) THEN
        OPEN curUser;
        LOOP
            FETCH curUser INTO thisUser;
            EXIT WHEN (curUser%NOTFOUND);
            thisUser.coins := thisUser.coins + 5.00;
            UPDATE Users SET coins = thisUser.coins WHERE userID = thisUser.userID;
        END LOOP;
        CLOSE curUser;
    END IF;
END xmas;

and when I call it, I get this error: 当我调用它时,我收到此错误:

ORA-01840: input value not long enough for date format. ORA-01840:日期格式的输入值不够长。

Tried different comparison methods for hours and nothing else has worked. 尝试了几个小时的不同比较方法,没有其他工作。 What's the problem?? 有什么问题??

You need to specify a date format for Oracle to know what actually '25-DEC' means. 您需要为Oracle指定日期格式,以了解实际上'25 -DEC'的含义。

select TO_DATE('25-DEC', 'DD-MON') from dual;

The problem should be on this line: 问题应该放在这一行:

    IF ((TO_DATE(d)) = (TO_DATE('25-DEC'))) THEN

As a note, it seems to me that you are also making a lot of unnecessary conversions. 作为一个注释,在我看来,你也做了很多不必要的转换。 I don't know if this is an educational attempt, but why not just run the update below: 我不知道这是否属于教育尝试,但为什么不运行以下更新:

update USERS 
set
  coins = coins + 5
where
  to_char(sysdate, 'DD-MON') = '25-DEC';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM