Date usage in stored procedure reports "ORA-01861"

When using to_date() in the stored procedure begin, the query keeps reporting "ORA-01861: the text does not match the format string".

my storage

create or replace procedure p_bill_statics(start_date in varchar2,end_date in varchar2) is

  --1

  cursor cur is select distinct(bill_id) as bill_id from t_print_log where print_time>=to_date(start_date,'yyyy-mm-dd') and print_time<=to_date(end_date,'yyyy-mm-dd');

begin

    select count(1) into v_cnt from t_receipt_data rd inner join t_account_info ac on rd.account_code=ac.account_code
            inner join t_org_info oi on ac.org_code = oi.org_code
            where rd.type_code=v_type_cur.type_code and ac.org_code=v_cur_init.org_code
            and rd.import_time>=to_date(start_date,'yyyy-mm-dd')
            and rd.import_time<=to_date(end_date,'yyyy-mm-dd') ;--2

end;
 

 

The value of the input parameter start_date is 2017-05-01, which works normally at 1, but when it runs to 2, it reports "ORA-01861: The text does not match the format string". After research, it is found that start_date and end_date need to be assigned to the newly defined variables in the begin body of the stored procedure, because it is possible that oracle internally treats it as some processing, and the correct use method:

create or replace procedure p_bill_statics(start_date in varchar2,end_date in varchar2) is

  --1

  cursor cur is select distinct(bill_id) as bill_id from t_print_log where print_time>=to_date(start_date,'yyyy-mm-dd') and print_time<=to_date(end_date,'yyyy-mm-dd');

v_start varchar2 (30); - 3
   v_end varchar2 (30);

begin

    select count(1) into v_cnt from t_receipt_data rd inner join t_account_info ac on rd.account_code=ac.account_code
            inner join t_org_info oi on ac.org_code = oi.org_code
            where rd.type_code=v_type_cur.type_code and ac.org_code=v_cur_init.org_code
            and rd.import_time>=to_date(v_start,'yyyy-mm-dd')
            and rd.import_time<=to_date(v_end,'yyyy-mm-dd');--2

end;
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326167771&siteId=291194637