我在解析 XML 文件时收到 ORA-01830:日期格式图片在转换整个输入字符串之前结束
Posted
技术标签:
【中文标题】我在解析 XML 文件时收到 ORA-01830:日期格式图片在转换整个输入字符串之前结束【英文标题】:I receive ORA-01830: date format picture ends before converting entire input string when I am parsing an XML file 【发布时间】:2016-07-01 11:07:22 【问题描述】:我有一个包含 XMLTYPE 列的表。 进入 XMLTYPE 列,我有一个 XML 文档。
这是 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<DeliveryNotificationFile Sender="1234567891234" Receiver="1234567891234" FileID="EAI_2e105455-0251-77aa-e053-0eba1a0a9787" CreateDate="1900-01-01T01:01:01+01:00" SendDate="1900-01-01T01:01:01+01:00" MessageType="DELIVERY_STATUS">
<DeliveryNotificationRecord>
<TackHeaderMessage>
<AckNackReceivedFromCMSDateTime>1900-01-01T01:01:01+01:00</AckNackReceivedFromCMSDateTime>
</TackHeaderMessage>
<TackPayload>
<TackHeaderPayload>
<TransactionId>2e105455-024b-77aa-e053-0eba1a0a9787</TransactionId>
<LinkedContextId>EAN_ST002_2e105455-024d-77aa-e053-0eba1a0a9787</LinkedContextId>
<LinkedCorrelationId>IMD_COR_00000000012240002150</LinkedCorrelationId>
<LinkedTransactionId>2e105455-024f-77aa-e053-0eba1a0a9787</LinkedTransactionId>
</TackHeaderPayload>
<Result>
<AckOrNack>ACK</AckOrNack>
<HTTPCode>520</HTTPCode>
<FaultCode>520.1</FaultCode>
<FaultString>TEST</FaultString>
</Result>
</TackPayload>
</DeliveryNotificationRecord>
</DeliveryNotificationFile>
我想根据 XML 文档中的一些属性值创建一条记录。 代码如下:
declare
TYPE r_actreq_file IS RECORD ( unzip_file_name VARCHAR2(250)
, sender NUMBER(13)
, receiver NUMBER(13)
, File_ID VARCHAR2(40)
, Create_Date DATE
, Send_Date DATE
, Type VARCHAR2(40)
);
gr_actreq_file r_actreq_file;
lx_xml_file xmltype;
as_xpath_expression varchar2(25) := '/DeliveryNotificationFile';
begin
SELECT B_XML_FILE_CONTENT
INTO lx_xml_file
FROM INTERFACE_ENTRIES
WHERE seq = 1316685715;
-- dbms_output.put_line('lx_xml_file = ' || lx_xml_file.getCLOBVal());
--retrieve the xml header data
SELECT header_data.sender
, header_data.receiver
, header_data.file_id
, CAST(to_timestamp_tz(header_data.create_date, 'yyyy-mm-dd"T"hh24:mi:ss TZH:TZM') at time zone 'Europe/Brussels' AS DATE)
, CAST(to_timestamp_tz(header_data.send_date, 'yyyy-mm-dd"T"hh24:mi:ss TZH:TZM') at time zone 'Europe/Brussels' AS DATE)
, header_data.type
INTO gr_actreq_file.sender
, gr_actreq_file.receiver
, gr_actreq_file.file_id
, gr_actreq_file.create_date
, gr_actreq_file.send_date
, gr_actreq_file.type
FROM XMLTABLE (as_xpath_expression PASSING lx_xml_file
COLUMNS sender NUMBER(13) PATH '@Sender'
, receiver NUMBER(13) PATH '@Receiver'
, file_id VARCHAR2(40) PATH '@FileID'
, create_date DATE PATH '@CreateDate'
, send_date DATE PATH '@SendDate'
, type VARCHAR2(40) PATH '@MessageType'
) header_data;
dbms_output.put_line('sender = ' || gr_actreq_file.sender || ' | receiver = ' || gr_actreq_file.receiver || ' | file_id = ' || gr_actreq_file.file_id || ' | create_date = ' || gr_actreq_file.create_date || ' | send_date = ' || gr_actreq_file.send_date || ' | type = ' || gr_actreq_file.type);
end;
但由于未知原因,我收到错误消息:
ORA-01830: 日期格式图片在转换整个输入之前结束 字符串
有人可以帮我吗?
【问题讨论】:
【参考方案1】:当您在结构和 COLUMNS 中将 Create_Date 和 Send_Date 定义为日期时,Oracle 会隐式转换为日期。而且因为这不是时间戳的标准格式,所以在转换时会出错。您正在转换为 TO_TIMESTAMP_TZ。比使用字符串。 在此处更改:
, Create_Date VARCHAR2(50)
, Send_Date VARCHAR2(50)
这里:
XMLTABLE (as_xpath_expression PASSING lx_xml_file
COLUMNS sender NUMBER(13) PATH '@Sender'
, receiver NUMBER(13) PATH '@Receiver'
, file_id VARCHAR2(40) PATH '@FileID'
, create_date VARCHAR2(50) PATH '@CreateDate'
, send_date VARCHAR2(50) PATH '@SendDate'
, type VARCHAR2(40) PATH '@MessageType'
) header_data;
编辑:
实际上也可以使用TIMESTAMP(6) WITH TIME ZONE。比您不需要将其转换为日期(to_timezone_tz):
COLUMNS sender NUMBER(13) PATH '@Sender'
, receiver NUMBER(13) PATH '@Receiver'
, file_id VARCHAR2(40) PATH '@FileID'
, create_date TIMESTAMP(6) WITH TIME ZONE PATH '@CreateDate'
, send_date TIMESTAMP(6) WITH TIME ZONE PATH '@SendDate'
, type VARCHAR2(40) PATH '@MessageType'
)
【讨论】:
以上是关于我在解析 XML 文件时收到 ORA-01830:日期格式图片在转换整个输入字符串之前结束的主要内容,如果未能解决你的问题,请参考以下文章