Domanda

CREATE OR REPLACE PROCEDURE PROC1(
   V_STARTTIME    IN DATE,
   V_ENDTIME      IN DATE)
BEGIN
INSERT INTO TAB1
SELECT COINS FROM TAB2
WHERE DATE BETWEEN TO_DATE(V_STARTTIME,'MM/DD/YYYY') AND TO_DATE(V_ENDTIME,'MM/DD/YYYY');
COMMIT;
END;

SAMPLE DATE in Tab2 IS TIMESTAMP DATATYPE 5/5/2014 9:46:38.000000 AM

When I try to execute

Execute PROC1(TO_DATE('5/5/2014','MM/DD/YYYY'),TO_DATE('5/6/2014','MM/DD/YYYY'));

the procedure is successfully completed but my Insert into was ignored.

I tried printing the input date through dbms_output.put_line and the date did not return.

È stato utile?

Soluzione

This is very, very similar to the question you asked yesterday.

If v_starttime and v_endtime are of type date, it makes no sense to call to_date on them. to_date does not take an argument of type date. It takes a parameter of type varchar2. If you try to pass a date to to_date, Oracle has to implicitly cast the date to a varchar2 using the session's NLS_DATE_FORMAT. If that doesn't match the format mask you're passing to to_date, you may get an error or you may get an incorrect result. As in yesterday's question, you want to avoid implicit conversions.

A date in Oracle has both a day and a time component (to the second). If you are doing the to_date in order to ensure that the time component is midnight, use the trunc function instead.

INSERT INTO TAB1( column_name )
  SELECT COINS 
    FROM TAB2
   WHERE <<timestamp column>> BETWEEN trunc( v_starttime ) AND trunc( v_endtime );

You say that your "insert was ignored". That seems highly unlikely. It's much more likely that your SELECT statement returned 0 rows so your INSERT inserted 0 rows. That's not an error. If you want to treat it as an error, you'd need to check SQL%ROWCOUNT after the INSERT and throw an error if the INSERT statement inserts 0 rows.

If the SELECT was not selecting any rows because of an implicit conversion error, then getting rid of the to_date and potentially adding the trunc would fix the problem.

Altri suggerimenti

The function TO_DATE requires string as first parameter.

CREATE OR REPLACE PROCEDURE PROC1(
   V_STARTTIME    IN DATE,
   V_ENDTIME      IN DATE)
BEGIN
 INSERT INTO TAB1
   SELECT COINS FROM TAB2 WHERE DATE BETWEEN V_STARTTIME AND V_ENDTIME;
 COMMIT; --You should not use commit in procedure.
END;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top