简体   繁体   中英

Oracle APEX SQL Date Time Import Procedure

I have a field (v_date) that contains date and time value eg(30-07-2021 13:30) I want to import to my table two new dates and time values according to this field. The first will be seven days later (08-06-2021 13:30), and the second one will be 7 days and one hour later (08-06-2021 14:30).

This is my code:

create or replace PROCEDURE "PR_ADD_MASSIVE_SESSIONS"
(v_date date)
IS
v_counter_day number :=7;
v_session_temp_date date;
v_session_temp_end date;


BEGIN

v_session_temp_date := to_date(v_date, 'MM-DD-YYYY HH24:MI') + v_counter_day;
v_session_temp_end := to_date(v_date, 'MM-DD-YYYY HH24:MI') + (v_counter_day + (1/1440*60));
        
INSERT INTO SESSIONS (CLIENTS_ID, SESSION_AREA, SESSION_DATE, SESSION_STOP, PAYMENT_TYPE_ID, PRICE, STATUS, COMPLETED, PAYMENT_STATUS, SESSION_NO)
VALUES (5, 1, v_session_temp_date, v_session_temp_end, 2, 40, 1, 1, 1, 1);

END;

And I get this error: Ajax call returned server error ORA-01843: not a valid month. I try with to_char but the compiler only wants to_date in order to have a successful procedure.

The variables v_session_temp_date and v_session_temp_date are defined as DATE . The function TO_DATE converts a string to a date. It should not be used on variables of type DATE . Remove the TO_DATE and you're good to go. Example code below (using an anonymous block, not a procedure)

CREATE TABLE sessions
(
clients_id NUMBER,
session_area NUMBER,
session_date  DATE,
session_stop  DATE,
payment_type_id NUMBER,
price NUMBER,
status NUMBER,
completed NUMBER,
payment_status NUMBER,
session_no NUMBER
);

Table SESSIONS created.

--original code
DECLARE
  v_date date := SYSDATE;
  v_counter_day number := 7;
  v_session_temp_date date;
  v_session_temp_end date;
BEGIN
  v_session_temp_date := to_date(v_date, 'MM-DD-YYYY HH24:MI') + v_counter_day;
  v_session_temp_end := to_date(v_date, 'MM-DD-YYYY HH24:MI') + (v_counter_day + (1/1440*60));
  
  INSERT INTO SESSIONS (CLIENTS_ID, SESSION_AREA, SESSION_DATE, SESSION_STOP, PAYMENT_TYPE_ID, PRICE, STATUS, COMPLETED, PAYMENT_STATUS, SESSION_NO)
  VALUES (5, 1, v_session_temp_date, v_session_temp_end, 2, 40, 1, 1, 1, 1);
END;
/
ORA-01843: not a valid month
ORA-06512: at line 7
01843. 00000 -  "not a valid month"
*Cause:    
*Action:

--fixed code
DECLARE
  v_date date := SYSDATE;
  v_counter_day number := 7;
  v_session_temp_date date;
  v_session_temp_end date;
BEGIN
  v_session_temp_date := v_date + v_counter_day;
  v_session_temp_end := v_date + (v_counter_day + (1/1440*60));
  
  INSERT INTO SESSIONS (CLIENTS_ID, SESSION_AREA, SESSION_DATE, SESSION_STOP, PAYMENT_TYPE_ID, PRICE, STATUS, COMPLETED, PAYMENT_STATUS, SESSION_NO)
  VALUES (5, 1, v_session_temp_date, v_session_temp_end, 2, 40, 1, 1, 1, 1);
END;
/
PL/SQL procedure successfully completed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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