我有一个将一些输入参数作为日期的程序。当我尝试执行我的 SP 时,这里是分配给每个参数的值
但是,当我执行时,我收到一条关于 p_schedule_value 参数的日期格式的错误消息。事实上,我们可以看到它没有以指定格式传递日期;相反,它会截断时间并将其传递为 yyyy-MMM-dd 您可以在下面的屏幕截图中看到输出中的 dynamic_sql 以及错误消息和 p_schedule_value 的值
为什么我的时间被截断了?似乎它忽略了 TO_DATE 转换谢谢
在“insert_or_upd.....”过程中,参数 p_schedule_value 已经是 DATE,因此请删除合并语句中的 TO_DATE。
更新答案
似乎立即执行会将日期作为 varchar2 传递,因此以日期格式明确可能会更好。
plsql_block :=
'merge into MOVEMENTS m
using (select :id as movement_id, '||q'"to_date(:dt,'YYYYMMDDHH24MI')"'||' as movement_date from dual) s
on (m.MOVEMENT_ID = s.movement_id and m.MOVEMENT_DATE = s.movement_date)
when matched then update set ' ||'colname'||q'" = to_date('"'||to_char(sysdate,'YYYYMMDDHH24MI')||q'"','YYYYMMDDHH24MI')
when not matched then insert (MOVEMENT_ID, MOVEMENT_DATE,MOVEMENT_ETD)
(:id,:dt,to_date(:value1,'YYYYMMDDHH24MI'))"' from dual;
execute immediate plsql_block USING (...... ,to_char(p_mvt_date,'YYYYMMDDHH24MI'),to_char(p_schedule_value,'YYYYMMDDHH24MI'));
然后,您执行的语句将如下所示..
"merge into MOVEMENTS m
using (select :id as movement_id, to_date(:dt,'YYYYMMDDHH24MI') as movement_date from dual) s
on (m.MOVEMENT_ID = s.movement_id and m.MOVEMENT_DATE = s.movement_date)
when matched then update set colname = to_date('201903151837','YYYYMMDDHH24MI')
when not matched then insert (MOVEMENT_ID, MOVEMENT_DATE,MOVEMENT_ETD)
(:id,:dt,to_date(:value1,'YYYYMMDDHH24MI'))"