XML行类型(xml数据输入)
XML Row TYPE (xml data input)
第 1 步:
CREATE TABLE Customer(
idCustomer INT PRIMARY KEY,
XmlCustomer XMLTYPE not null);
第 2 步:
CREATE OR REPLACE DIRECTORY XMLDIR AS 'C:\xml_files';
第 3 步:
XmlCustomer.xml
<?xml version="1.0"?>
<XmlCustomer>
<Customer>
<name>Sally Black</name>
<type>Person</type>
<nip>1234567890</nip>
<city>London</city>
<street>Blue Street 2</street>
<signUpDate>21-12-2007</signUpDate>
</Customer>
<Customer>
<name>John Snow</name>
<type>Person</type>
<nip>2345678901</nip>
<city>Wroclaw</city>
<street>Uprised 23</street>
<signUpDate>12-02-1991</signUpDate>
</Customer>
<Customer>
<name>PC-Kom</name>
<type>Fleet</type>
<nip>3456789012</nip>
<city>Swidnica</city>
<street>Water Street 5</street>
<signUpDate>12-02-1991</signUpDate>
</Customer>
</XmlClient>
第 4 步:
现在我正在尝试将这些 XML 数据输入到 Table 客户
INSERT INTO Customer (idCustomer,XmlCustomer) VALUES (1, XMLType(bfilename('XMLDIR', 'XmlCustomer.xml'), nls_charset_id('AL32UTF8')));
但我正在将所有文件插入 1 行。
我的问题是如何插入这些数据以接收 3 行(xml 文件包含 3 行)
以及如何自动增加 idCustomer??
我还需要创建这个 xml 文件的 xsd:schema,这对我来说有点黑魔法 ^^。
我对 Oracle DB 和 XML 还很陌生,所以我在等待明确的答案。
您可以使用 XQuery
,更具体地说,在此处 XMLTable
,将 XML 文档转换为您可以将其视为行的多个值:
INSERT INTO Customer (idCustomer, xmlCustomer)
SELECT CustomerIdSeq.nextval, XmlCustomer
FROM XMLTable('/XmlCustomer/Customer'
PASSING XMLType(bfilename('XMLDIR', 'XmlCustomer.xml'), nls_charset_id('AL32UTF8'))
COLUMNS "XMLCUSTOMER" XMLType PATH '/'
);
我已经创建了 a sequence 并且正在调用 nextval
以生成 'auto-increment' 客户 ID; Oracle 12c 具有自动递增列,但在早期版本中,您需要一个序列来伪造它,并可选择使用触发器来隐藏它。无论如何,这给出了:
SELECT * FROM Customer ORDER BY idCustomer;
IDCUSTOMER XMLCUSTOMER
---------- ---------------------------------------------------
1 <Customer>
<name>Sally Black</name>
<type>Person</type>
<nip>1234567890</nip>
<city>London</city>
<street>Blue Street 2</street>
<signUpDate>21-12-2007</signUpDate>
</Customer>
2 <Customer>
<name>John Snow</name>
<type>Person</type>
<nip>2345678901</nip>
<city>Wroclaw</city>
<street>Uprised 23</street>
<signUpDate>12-02-1991</signUpDate>
</Customer>
3 <Customer>
<name>PC-Kom</name>
<type>Fleet</type>
<nip>3456789012</nip>
<city>Swidnica</city>
<street>Water Street 5</street>
<signUpDate>12-02-1991</signUpDate>
</Customer>
SQL Fiddle 很高兴创建填充,但很难像普通的那样查询它 XML...
除非您特别想存储 XML 结构,否则您最好创建单独的列并将数据加载到这些列中:
SELECT CustomerIdSeq.nextval, name, type, nip, city, street, signUpDate
FROM XMLTable('/XmlCustomer/Customer'
PASSING XMLType(bfilename('D42', 'XmlCustomer.xml'), nls_charset_id('AL32UTF8'))
COLUMNS "NAME" VARCHAR2(20) PATH 'name',
"TYPE" VARCHAR2(10) PATH 'type'
"NIP" NUMBER PATH 'nip'
"CITY" VARCHAR2(20) PATH 'city'
"STREET" VARCHAR2(20) PATH 'street'
"SIGNUPDATE" DATE PATH 'signUpDate'
);
然后:
INSERT INTO Customer (idCustomer, name, type, nip, city, street, signUpDate)
SELECT CustomerIdSeq.nextval, name, type, nip, city, street,
TO_DATE(signUpDate, 'DD-MM-YYYY')
FROM XMLTable('/XmlCustomer/Customer'
PASSING XMLType(bfilename('D42', 'XmlCustomer.xml'), nls_charset_id('AL32UTF8'))
COLUMNS "NAME" VARCHAR2(20) PATH 'name',
"TYPE" VARCHAR2(10) PATH 'type',
"NIP" NUMBER PATH 'nip',
"CITY" VARCHAR2(20) PATH 'city',
"STREET" VARCHAR2(20) PATH 'street',
"SIGNUPDATE" VARCHAR2(10) PATH 'signUpDate'
);
给出:
SELECT * FROM Customer ORDER BY idCustomer;
IDCUSTOMER NAME TYPE NIP CITY STREET SIGNUPDATE
---------- -------------------- ---------- ---------- -------------------- -------------------- ----------
4 Sally Black Person 1234567890 London Blue Street 2 21-DEC-07
5 John Snow Person 2345678901 Wroclaw Uprised 23 12-FEB-91
6 PC-Kom Fleet 3456789012 Swidnica Water Street 5 12-FEB-91
当然,您需要为字符串列设置合适的大小。请注意,我使用 to_date()
将日期字符串转换为实际日期;因为它不是标准的 YYYY-MM-DD ISO 格式 XML expects,所以不能直接提取为日期值。
第 1 步:
CREATE TABLE Customer(
idCustomer INT PRIMARY KEY,
XmlCustomer XMLTYPE not null);
第 2 步:
CREATE OR REPLACE DIRECTORY XMLDIR AS 'C:\xml_files';
第 3 步:
XmlCustomer.xml
<?xml version="1.0"?>
<XmlCustomer>
<Customer>
<name>Sally Black</name>
<type>Person</type>
<nip>1234567890</nip>
<city>London</city>
<street>Blue Street 2</street>
<signUpDate>21-12-2007</signUpDate>
</Customer>
<Customer>
<name>John Snow</name>
<type>Person</type>
<nip>2345678901</nip>
<city>Wroclaw</city>
<street>Uprised 23</street>
<signUpDate>12-02-1991</signUpDate>
</Customer>
<Customer>
<name>PC-Kom</name>
<type>Fleet</type>
<nip>3456789012</nip>
<city>Swidnica</city>
<street>Water Street 5</street>
<signUpDate>12-02-1991</signUpDate>
</Customer>
</XmlClient>
第 4 步:
现在我正在尝试将这些 XML 数据输入到 Table 客户
INSERT INTO Customer (idCustomer,XmlCustomer) VALUES (1, XMLType(bfilename('XMLDIR', 'XmlCustomer.xml'), nls_charset_id('AL32UTF8')));
但我正在将所有文件插入 1 行。 我的问题是如何插入这些数据以接收 3 行(xml 文件包含 3 行) 以及如何自动增加 idCustomer??
我还需要创建这个 xml 文件的 xsd:schema,这对我来说有点黑魔法 ^^。
我对 Oracle DB 和 XML 还很陌生,所以我在等待明确的答案。
您可以使用 XQuery
,更具体地说,在此处 XMLTable
,将 XML 文档转换为您可以将其视为行的多个值:
INSERT INTO Customer (idCustomer, xmlCustomer)
SELECT CustomerIdSeq.nextval, XmlCustomer
FROM XMLTable('/XmlCustomer/Customer'
PASSING XMLType(bfilename('XMLDIR', 'XmlCustomer.xml'), nls_charset_id('AL32UTF8'))
COLUMNS "XMLCUSTOMER" XMLType PATH '/'
);
我已经创建了 a sequence 并且正在调用 nextval
以生成 'auto-increment' 客户 ID; Oracle 12c 具有自动递增列,但在早期版本中,您需要一个序列来伪造它,并可选择使用触发器来隐藏它。无论如何,这给出了:
SELECT * FROM Customer ORDER BY idCustomer;
IDCUSTOMER XMLCUSTOMER
---------- ---------------------------------------------------
1 <Customer>
<name>Sally Black</name>
<type>Person</type>
<nip>1234567890</nip>
<city>London</city>
<street>Blue Street 2</street>
<signUpDate>21-12-2007</signUpDate>
</Customer>
2 <Customer>
<name>John Snow</name>
<type>Person</type>
<nip>2345678901</nip>
<city>Wroclaw</city>
<street>Uprised 23</street>
<signUpDate>12-02-1991</signUpDate>
</Customer>
3 <Customer>
<name>PC-Kom</name>
<type>Fleet</type>
<nip>3456789012</nip>
<city>Swidnica</city>
<street>Water Street 5</street>
<signUpDate>12-02-1991</signUpDate>
</Customer>
SQL Fiddle 很高兴创建填充,但很难像普通的那样查询它 XML...
除非您特别想存储 XML 结构,否则您最好创建单独的列并将数据加载到这些列中:
SELECT CustomerIdSeq.nextval, name, type, nip, city, street, signUpDate
FROM XMLTable('/XmlCustomer/Customer'
PASSING XMLType(bfilename('D42', 'XmlCustomer.xml'), nls_charset_id('AL32UTF8'))
COLUMNS "NAME" VARCHAR2(20) PATH 'name',
"TYPE" VARCHAR2(10) PATH 'type'
"NIP" NUMBER PATH 'nip'
"CITY" VARCHAR2(20) PATH 'city'
"STREET" VARCHAR2(20) PATH 'street'
"SIGNUPDATE" DATE PATH 'signUpDate'
);
然后:
INSERT INTO Customer (idCustomer, name, type, nip, city, street, signUpDate)
SELECT CustomerIdSeq.nextval, name, type, nip, city, street,
TO_DATE(signUpDate, 'DD-MM-YYYY')
FROM XMLTable('/XmlCustomer/Customer'
PASSING XMLType(bfilename('D42', 'XmlCustomer.xml'), nls_charset_id('AL32UTF8'))
COLUMNS "NAME" VARCHAR2(20) PATH 'name',
"TYPE" VARCHAR2(10) PATH 'type',
"NIP" NUMBER PATH 'nip',
"CITY" VARCHAR2(20) PATH 'city',
"STREET" VARCHAR2(20) PATH 'street',
"SIGNUPDATE" VARCHAR2(10) PATH 'signUpDate'
);
给出:
SELECT * FROM Customer ORDER BY idCustomer;
IDCUSTOMER NAME TYPE NIP CITY STREET SIGNUPDATE
---------- -------------------- ---------- ---------- -------------------- -------------------- ----------
4 Sally Black Person 1234567890 London Blue Street 2 21-DEC-07
5 John Snow Person 2345678901 Wroclaw Uprised 23 12-FEB-91
6 PC-Kom Fleet 3456789012 Swidnica Water Street 5 12-FEB-91
当然,您需要为字符串列设置合适的大小。请注意,我使用 to_date()
将日期字符串转换为实际日期;因为它不是标准的 YYYY-MM-DD ISO 格式 XML expects,所以不能直接提取为日期值。