Some of the columns that I am trying to load into Oracle via SQL Loader from an XML file are null. For example may have several filled but ocassionally it has some nulls.
How can I tell the SQL*Loader that some of the data will be null, or how can I deal with nulls?
Like jonearles suggested. I use XMLTABLE to insert XML data into relational tables. First put the XML data into a Oracle tables XMLTYPE column:
DROP TABLE XMLTEST;
CREATE TABLE XMLTEST
( XML_COL XMLTYPE);
DECLARE
poXML CLOB;
BEGIN
-- Store the Purchase Order XML in the CLOB variable
poXML := '<?xml version="1.0"?>
<zalen>
<zaal zaal_id="1">
<alt_id>88</alt_id>
<display_naam>01 West 430</display_naam>
<alt_db>exp_BB</alt_db>
</zaal>
<zaal zaal_id="2">
<alt_id>170</alt_id>
<display_naam>02 Midden 010</display_naam>
<alt_db>exp_BB</alt_db>
</zaal>
<zaal zaal_id="3">
<alt_id>173</alt_id>
<display_naam>02 Midden 110</display_naam>
<alt_db>exp_BB</alt_db>
</zaal>
<zaal zaal_id="4">
<syl_id>F491B0A119DABE76B2F6B2C0A3E902F6</syl_id>
<alt_id>183</alt_id>
<display_naam>02 Oost 010</display_naam>
<alt_db>exp_BB</alt_db>
</zaal>
<zaal zaal_id="5">
<alt_id>172</alt_id>
<display_naam>02 Oost 300</display_naam>
<alt_db>exp_BB</alt_db>
</zaal>
.
.
.
<zaal zaal_id="126">
<syl_id>F491B0A119DABE76B2F6B2C0A3E901E3</syl_id>
<alt_id>129</alt_id>
<display_naam>HB.02.140</display_naam>
<alt_db>exp_EE</alt_db>
</zaal>
</zalen>';
INSERT INTO xmltest (xml_col) VALUES (XMLTYPE(poXML));
END;
/
Use XMLTable function to create (or insert into) the table:
drop table zalen;
create table zalen as
select xt.zaal_id
, xt.alt_id
, xt.syl_id
, xt.alt_db
, xt.display_naam
from xmltest xts
, XMLTable('zalen/zaal' PASSING xts.xml_col
columns zaal_id INTEGER PATH '#zaal_id'
,alt_id INTEGER PATH 'alt_id'
,syl_id VARCHAR2(100) PATH 'syl_id'
,display_naam VARCHAR2(100)PATH 'display_naam'
,alt_db VARCHAR2(100)PATH 'alt_db') xt;
You may want to rethink the way you're loading and transforming files. I assume this is related to your other question, in which case it looks like you're building your own XML parser. Oracle provides some tools to help you do this.
First, just to load the file, you can either use SQL*Loader to load the data as an XMLType, or you can use something like DBMS_XSLPROCESSOR.READ2CLOB to read the file as a CLOB and then convert it to an XMLType.
After it's loaded as an XMLType, you can use a PL/SQL procedure that uses XPath to iterate through the values and insert them into the table.
And there are probably some other ways to do it.
I'm sure none of these ideas are nearly as easy as what you were hoping for. Processing XML correctly is much more complicated than what SQL*Loader is typically used for - loading delimited files.
Related
I Have an XML which have more than 4000 Characters . The Datatype in Oracle in XMLTYPE . The Insert or Execute Script Components are not allowing me to insert this XML into Oracle . I Cannot Change the Datatype in Oracle . Is there any way i can insert the XML into XMLTYPE Column. In Java i am able to achieve it by creating and SQLXML object from Connection .
SQLXML xml = conn.createSQLXML(); //This allowing to save the xml at any size
Error i am getting
ORA-01461: can bind a LONG value only for insert into a LONG column
insert Statement
insert into ABC(ID,RESPONSE_XML) values(123, :RESPONSE_XML))
Transformation
'RESPONSE_XML' : write(payload, 'application/xml')
If i reduce the number characters in the payload xml its inserting successfully . What we can do here to get it inserted.
JDBC Driver i am using is ojdbc14-10g.jar
it could be an Oracle limitation. It has a limit on the size of XML identifiers:
XML Identifier Length Limit – Oracle XML DB supports only XML identifiers that are 4000 characters long or shorter.
https://docs.oracle.com/cd/E18283_01/appdev.112/e16659/appjspec.htm
Having an XML like that would be just insane.
create table JSON_TAB (JSON_VAL CLOB);
-- tried to add the constraint like this CONSTRAINT JTE_CK check (JSON_VAL is json) butt says it expects null so I didn't create it for now.
I am trying to execute the statement
SELECT JT.Ime, JT.Broj, JT.Pozicija
FROM JSON_TAB JTE,
JSON_TABLE (JTE.JSON_VAL, '$.players[*]'
COLUMNS (Ime VARCHAR2(20) PATH '$.name',
Broj NUMBER PATH '$.number',
Pozicija VARCHAR2 PATH '$.position')) JT ;
and receiving an error SQL command not properly ended.
JSON_VAL is a CLOB!
Is it a problem with syntax, data type or something else?
I took the example from youtube tutorial and entered it manually.
ORACLE version is 12.1.0.2.0.
I have a SQL Server procedure written that exports data in XML format. In the database, I also have PDFs that have been stored as BLOB files that I need to export with the data. Is it possible to convert these to PDF as I export?
This is really easy...
I assume, that the BLOBs live in a table column of type VARBINARY(MAX). Including such a column into a SELECT ... FOR XML PATH will implicitly do the conversion for you.
In this example I use three tiny binary BLOBs, put them into a XML-variable and re-read them. There should be not difference with your PDF BLOBs:
DECLARE #tbl TABLE(ID INT,Content VARBINARY(MAX));
INSERT INTO #tbl VALUES
(1,0x101010101010101010101)
,(2,0x110011001100110011001100)
,(3,0x111000111000111000111000);
DECLARE #xml XML=
(
SELECT ID AS [#ID]
,Content
FROM #tbl
FOR XML PATH('myData'),ROOT('root')
);
SELECT #xml;
The result as XML (implicit conversion to base64)
<root>
<myData ID="1">
<Content>AQEBAQEBAQEBAQE=</Content>
</myData>
<myData ID="2">
<Content>EQARABEAEQARABEA</Content>
</myData>
<myData ID="3">
<Content>ERAAERAAERAAERAA</Content>
</myData>
</root>
Now I read the data from the XML
SELECT B.value('Content[1]','varbinary(max)') AS BackToVarbinary
FROM #xml.nodes('/root/myData') AS A(B)
The result
BackToVarbinary
0x0101010101010101010101
0x110011001100110011001100
0x111000111000111000111000
I have this table in Oracle 11g:
create table tmp_test_xml (
name_xml varchar2(4000),
file_xml xmltype
);
At this link oracle binding xmltype, I have read that for a correct insert into a field XMLType I must to use the "Xmltype binding" as this insert:
insert into tmp_test_xml values (
'file.xml',
xmltype(
'<?xml version="1.0" encoding="UTF-8"?>
<list_book>
<book>1</book>
<book>2</book>
<book>3</book>
</list_book>'
)
);
But, if I try to lunch this insert without the binding in XMLType, work very fine:
insert into tmp_test_xml values (
'file.xml',
'<?xml version="1.0" encoding="UTF-8"?>
<list_book>
<book>1</book>
<book>2</book>
<book>3</book>
</list_book>'
);
Now, is facoltative use binding or not? What is the correct insert?
Why the second insert works?
Oracle tries to do job for you, so if your datatype doesn't match column datatype, it attempts to convert data into correct data type.
Since you have correct xml string in your insert statement, database did conversion to xmltype for you.
And I'm not sure, but I doubt there were a word must use XMLType binding.
But you might want use it, if you need to have more control when creating XML from string.
Check XMLType constructors to get an idea.
I have a CLOB column which contains a large amount of XML. I want to add a new attribute in that XML, like this attribute :
<name>me</name>
I tried using UpdateXML but I'm not getting it right.
CLOB is converted to XMLType using XMLType() and XMLType is converted to CLOB using to_clob. The following is an example.
create table table_with_clob (myclob clob);
insert into table_with_clob values ('<mytag><subtag>hello world</subtag></mytag>');
UPDATE table_with_clob SET myclob =
to_clob(INSERTCHILDXML(xmltype(myclob),
'/mytag', 'subtag',
XMLType('<subtag>another</subtag>')));
select * from table_with_clob;
Output
myclob
------
<mytag><subtag>hello world</subtag><subtag>another</subtag></mytag>
Though I think this is not very efficient and you might better convert the column to XMLType and the operate with it.