存储过程中的日期使用报“ORA-01861”
程序员文章站
2022-03-22 10:05:33
...
在存储过程begin中用查询用到to_date(),一直报“ORA-01861: 文字与格式字符串不匹配”。
我的存储
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;
入参start_date 的值是2017-05-01,在1处使用正常,但运行到2出的时候就报“ORA-01861: 文字与格式字符串不匹配”。经过研究发现在存储过程begin体中需要将start_date和end_date再赋给新定义的变量,因为有可能是oracle内部把它当做了某些处理,正确使用方法:
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;