Oracle将数据插入XML类型表

时间:2016-07-05 15:07:52

标签: php xml oracle codeigniter

我有一个带有XMLTYPE列的表,我想以XML格式插入数据,但信息不是来自服务器的XML格式。我正在考虑实现一个存储过程,它从服务器获取值并将XML格式的数据插入该列,但该过程给了我正确的括号错误,我不明白为什么。

以下是代码:

CREATE TABLE xml_tab (
id       NUMBER,
xml_data XMLTYPE
);
ALTER TABLE XML_TAB
MODIFY (ID DEFAULT XML_TAB_SEQ.NEXTVAL);

我尝试实施的存储过程。

CREATE OR REPLACE PROCEDURE insert_xml_proc (
dish_name IN VARCHAR,
dish_type IN VARCHAR,
dish_image IN VARCHAR ) AS
BEGIN
INSERT INTO XML_TAB (XML_DATA) values(
xmltype(
'<xml>
<item>
    <DISH_ID>'XML_TAB_SEQ.CURRVAL'</DISH_ID>
    <DISH_NAME>'dish_name'</DISH_NAME>
    <DISH_TYPE>'dish_type'</DISH_TYPE>
    <DISH_IMAGE>'dish_image'</DISH_IMAGE>
</item></xml>' ));
END;

我的想法是我在PHP中使用我的表单将一些变量发送到存储过程以插入此数据。插入常规表然后从中获取数据以作为XML插入XML表也可以,但问题仍然存在。

谢谢!

编辑:由于别名,程序v2不起作用。

CREATE OR REPLACE PROCEDURE insert_xml_v2_proc (
dish_name IN VARCHAR2,
dish_type IN VARCHAR2,
dish_image IN VARCHAR2 ) AS
BEGIN
insert into xml_tab (xml_data) select xmlelement("xml", xmlelement("item",
                                xmlforest(XML_TAB_SEQ.nextval as dish_id,
                                          dish_name,
                                          dish_type,
                                          dish_image))) xmldata from dual;
END;
 /

2 个答案:

答案 0 :(得分:2)

这是因为你没有正确连接。它应该是:

CREATE OR REPLACE PROCEDURE insert_xml_proc (
dish_name IN VARCHAR2,
dish_type IN VARCHAR2,
dish_image IN VARCHAR2 ) AS
BEGIN
INSERT INTO XML_TAB (XML_DATA) values(
xmltype(
'<xml>
<item>
    <DISH_ID>'||XML_TAB_SEQ.CURRVAL||'</DISH_ID>
    <DISH_NAME>'||dish_name||'</DISH_NAME>
    <DISH_TYPE>'||dish_type||'</DISH_TYPE>
    <DISH_IMAGE>'||dish_image||'</DISH_IMAGE>
</item></xml>' ));
END;
/

N.B。另请注意,我已将参数从VARCHAR转换为VARCHAR2

有更好的方法来生成xml,例如:

with sample_data as (select 'a' dish_name, 'b' dish_type, 'c' dish_image from dual)
select xmlelement("xml", xmlelement("item",
                                    xmlforest(XML_TAB_SEQ.nextval as dish_id,
                                              dish_name,
                                              dish_type,
                                              dish_image))) xmldata
from  sample_data;

XMLDATA
------------------------------------------------------------------------------------------------------------------------
<xml><item><DISH_ID>262</DISH_ID><DISH_NAME>a</DISH_NAME><DISH_TYPE>b</DISH_TYPE><DISH_IMAGE>c</DISH_IMAGE></item></xml>

答案 1 :(得分:1)

我认为应该是这样(未经测试)

CREATE OR REPLACE PROCEDURE insert_xml_proc (
dish_name IN VARCHAR,
dish_type IN VARCHAR,
dish_image IN VARCHAR ) AS
BEGIN
INSERT INTO XML_TAB (XML_DATA) values (
xmlelement("xml", 
   xmlelement("item", 
      xmlelement("DISH_ID", XML_TAB_SEQ.CURRVAL),
      xmlelement("DISH_NAME", dish_name),
      xmlelement("DISH_TYPE", dish_type),
      xmlelement("DISH_IMAGE", dish_image)
   )
)
);

另一种方式就是这样:

create or replace type "item" as object (
"DISH_ID" number,
"DISH_NAME" varchar2(100),
"DISH_TYPE" varchar2(100),
"DISH_IMAGE" varchar2(100));

INSERT INTO XML_TAB (XML_DATA) values (
xmlelement("xml", 
   xmltype("item"(XML_TAB_SEQ.CURRVAL, dish_name, dish_type, dish_image))
   )
);

或(但在这里我不确定它是否正确):

create or replace type "item" as object (
"DISH_ID" number,
"DISH_NAME" varchar2(100),
"DISH_TYPE" varchar2(100),
"DISH_IMAGE" varchar2(100));

create or replace type "xml" as object (
"item" "item");

INSERT INTO XML_TAB (XML_DATA) values (
xmltype("xml"(
    "item"(XML_TAB_SEQ.CURRVAL, dish_name, dish_type, dish_image))
   )
);