Query xml using extract over CLOB column in Oracle - sql

We are storing the xml in oracle database. The column type is CLOB.
I could fetch data from the xml tag with below query. When i execute the query i am getting null.
XML in the column:
<input xmlns="http://google.com/testsystem"
xmlns:testsystem="http://test.com/testSystem"
xmlns:tns="http://google.com/testService/">
<ProcessData>
<reviewYear>2014-2015</reviewYear>
</ProcessData>
</input>
Table name : test_table
Column name : input_xml
Column type : CLOB
Query:
select extract(xmltype(input_xml),'//reviewYear/text()').getStringVal() as data
from test_table
where id = 1;
Result:
DATA
------------------------------------

you need to specify namespace, please, try next query
select extract(xmltype('<input xmlns="http://google.com/testsystem"
xmlns:testsystem="http://test.com/testSystem"
xmlns:tns="http://google.com/testService/">
<ProcessData>
<reviewYear>2014-2015</reviewYear>
</ProcessData>
</input>'),'/input/ProcessData/reviewYear/text()', 'xmlns="http://google.com/testsystem" xmlns:testsystem="http://test.com/testSystem" xmlns:tns="http://google.com/testService/"').getStringVal() as data
from dual
UPD.
for update try
select updatexml(xmltype('<input xmlns="http://google.com/testsystem"
xmlns:testsystem="http://test.com/testSystem"
xmlns:tns="http://google.com/testService/">
<ProcessData>
<reviewYear>2014-2015</reviewYear>
</ProcessData>
</input>'), '/input/ProcessData/reviewYear/text()', '2013-2014',
'xmlns="http://google.com/testsystem" xmlns:testsystem="http://test.com/testSystem" xmlns:tns="http://google.com/testService/"').getclobval() as data
from dual

Related

Oracle Regex Replace on String Query

Query to fetches the sub xml from an xml saved in the CLOB column of my table is given as :
select REGEXP_REPLACE(xmltype(t.prov_request).extract('//SOAP_Domain_Msg/Body').getStringVal(),'<Body>|</Body>','') xml
from tbl_prov_comptel
There is "SO1_USERNAME" value="xxx" in the string returned by the above query.
What i want to achieve is to form a consolidated query and append something in the start of the expression above.
ie."SO1_USERNAME" value="qwexxx"
Oracle Setup:
CREATE TABLE tbl_prov_comptl ( prov_request CLOB );
INSERT INTO tbl_prov_comptl VALUES (
'<SOAP_Domain_Msg><Body><NS4:ModifyRequest xmlns:NS4="http://soa.comptel.com/2011/02/instantlink"><NS4:RequestParameters> <NS4:Parameter name="SO1_USERNAME" value="222671150"/></NS4:RequestParameters> </NS4:ModifyRequest></Body></SOAP_Domain_Msg>'
);
Query:
SELECT EXTRACTVALUE(
xml,
'//NS4:ModifyRequest/NS4:RequestParameters/NS4:Parameter[name="SO1_USERNAME"]/#value',
'xmlns:NS4="http://soa.comptel.com/2011/02/instantlink"'
) AS SO1_USERNAME,
x.xml.getStringVal() AS xml
FROM (
SELECT XMLType( prov_request ).extract( '//SOAP_Domain_Msg/Body/*' ) AS xml
FROM tbl_prov_comptl
) x;
Output:
SO1_USERNAME XML
------------ ------------------------------------------------------------------------------
222671150 <NS4:ModifyRequest xmlns:NS4="http://soa.comptel.com/2011/02/instantlink"><NS4
:RequestParameters> <NS4:Parameter name="SO1_USERNAME" value="222671150"/></NS
4:RequestParameters> </NS4:ModifyRequest>

Fetch Xml Content from a table in oracle

This is my XML Content stored same as it is in a Column name "Xml_Column" in the table "TEST_TABLE" and the data type of the column is clob.
<ns0:TEST_EVENTS xmlns:ns0="http://TEST.APPLICATION.ABC.Schemas.XML_Target_TESTEvents">
<compname>Sherlock</compname>
<Add>Homes</Add>
<Employee>
<firstname>Jim</firstname>
<lastname>Moriarty</lastname>
<age>52</age>
<email>jim#sh.com</email>
</Employee>
</ns0:TEST_EVENTS>
My requirement is to fetch the firstname and last name from this column where my xml content is stored and display. Any help
Use EXTRACTVALUE, it's a simpliest way: https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions052.htm
For optimization, you should make inner query for constructing XMLType instance based on your clob; in the outer query use EXTRACTVALUE to select every value you wish.
For example:
SELECT extractValue(OBJECT_VALUE, '/firstname'),
extractValue(OBJECT_VALUE, '/lastname')
FROM TEST_TABLE;
All firstname and lastname:
SELECT XMLQuery('for $d in /Employee
return ($d/firstname/text(),$d/lastname/text())
PASSING OBJECT_VALUE
RETURNING CONTENT) FROM TEST_TABLE;

query for extracting values from a column containing data in XML format

suppose I have a table, mydata, that has a column xmldata whose values are text in XML format such as
<maindataheading>
<firstleveldata>
<pricedata pricetype="normal">123</pricedata>
</firstleveldata>
</maindataheading>
how do I extract <pricedata pricetype="normal">123</pricedata> from the xmldata column?
Below is sample SQL for retrieving a varchar value from XML:
DECLARE #MyXmlData XML
-- Read XML data into variable
SELECT #MyXmlData = xmldata
FROM MyData
-- Check if the XML node exists before attempting to retrieve it
IF #MyXmlData.exist('/maindataheading/firstleveldata/pricedata') = 1
BEGIN
DECLARE #MyDataValue VARCHAR(255)
-- Get specific XML data
SELECT #MyDataValue = ParamValues.ID.Value('.', 'varchar(255)')
FROM #MyXmlData.nodes('/maindataheading/firstleveldata/pricedata') AS ParamValues(ID)
IF #MyDataValue IS NOT NULL
BEGIN
-- Do something with data
END
END
select xmldata.query('/maindataheading/firstleveldata/pricedata')
from mydata
Result:
<pricedata pricetype="normal">123</pricedata>

Oracle SQL - Extracting clob value from XML with repeating nodes

I am attempting to run SQL on a table (called test_xml with a column xml_data [data type xmltype]). The column contains xml with repeating nodes (test_3). The following statement runs successfully when the node contains data of a non clob size:
SELECT
extractvalue (Value (wl), '*/test_3')
FROM test_xml
, TABLE (xmlsequence (extract (xml_data, '*/record'))) wl
but fails when test_3 node contains a lot of data:
ORA-01706: user function result value was too large
I amended my query:
SELECT
extractvalue(Value (wl), '*/test_3').getClobVal()
FROM test_xml
, TABLE (xmlsequence (extract (xml_data, '*/record'))) wl
but this fails with:
ORA-22806: not an object or REF
This was resolved via a response received on Oracle Forums:
See Forum Post
From Oracle release 11.2.0.2:
SELECT x.*
FROM test_xml t
, XMLTable(
'/*/record'
passing t.xml_data
columns
test_3 clob path 'test_3'
) x
;
My database version is 10.2.0.4 hence the following 'trick' is required:
SELECT dbms_xmlgen.convert(x.test_3.getClobVal(), 1) as test_3
FROM test_xml t
, XMLTable(
'/*/record'
passing t.xml_data
columns
test_3 xmltype path 'test_3/text()'
) x
;
Thanks go to odie_63 for this

SQL- Collect all data into a variable

i need to collect all return data into a variable using comma separated.
let say i have a select command like: select * from #temptable.
it's return:
Field1|Field2
-------------
Value1|Value2
Expected Result: #testvariable hold the value: 'Value1','Value2'
On this table their may have 2 columns and i need to store all the return result into a single variable. We can easily collect a single value like: select #var=column1 from #temptable. But i need to store all.Here the problem is, the number of column can be vary. Mean, number of column and name of column generate from another query.So, i can't mention the field name.I need a dynamic way to do it. on this table only one row will be return. Thanks in advance.
You can do this without dynamic SQL using XML
DECLARE #xml XML = (SELECT * FROM #temptable FOR XML PATH(''))
SELECT stuff((SELECT ',' + node.value('.', 'varchar(100)')
FROM #xml.nodes('/*') AS T(node)
FOR XML PATH(''), type).value('.','varchar(max)')
, 1, 1, '');
This can probably be simplified by someone more adept at XML querying than me.
Since your column names are dynamic, so first you have to take the column names as comma separated in a variable and then can use EXEC()
for example :-
//making comma seperated column names from table B
DECLARE #var varchar(1000)=SELECT SUBSTRING(
(SELECT ',' + Colnames
FROM TABLEB
ORDER BY Colnames
FOR XML PATH('')),2,200000)
//Execute the sql statement
EXEC('select '+#var+' from tableA')
if you want to get the value returned after execution of sql statement then you can use
sp_executesql (Transact-SQL)