XMLQuery with Oracle - sql

I'm doing examples from workbook. I created table and insert couple of records. Below is my code:
Create table:
CREATE TABLE test_Pracownicy
(IDPracownika NUMBER(3),
Dane XMLTYPE);
Insert record to the table:
INSERT INTO test_Pracownicy (IDPracownika, Dane)
VALUES (1,
XMLTYPE('
<PRecord>
<Nazwisko>Kowalski</Nazwisko>
<Imie>Jan</Imie>
<RokUrodz>1980</RokUrodz>
<Wzrost>1.77</Wzrost>
<DataZatr>2001/02/10</DataZatr>
</PRecord>')
);
Now I want to run XMLQuery:
SELECT IDPracownika,
XMLQuery(
'FOR $i IN /PRecord
WHERE $i /Nazwisko = "Kowalski"
ORDER BY $i/Imie
RETURN $i'
PASSING by VALUE Dane
RETURNING CONTENT) NazwiskoXML
FROM test_Pracownicy;
and I'm getting error:
ORA-19114: XPST0003 - error during parsing the XQuery expression:
LPX-00801: XQuery syntax error at 'i'
1 FOR $i IN /PRecord
- ^
19114. 00000 - "error during parsing the XQuery expression: %s"
*Cause: An error occurred during the parsing of the XQuery expression.
*Action: Check the detailed error message for the possible causes.
Error at Line: 117 Column: 6

I changed query to small (for, where, return...) letters and it's workging:
SELECT IDPracownika,
XMLQuery(
'for $i in /PRecord
where$i /Nazwisko = "Kowalski"
order by $i /Imie
retrun $i'
PASSING by VALUE Dane
RETURNING CONTENT) NazwiskoXML
FROM test_Pracownicy;
It's case sensitive.

Related

Using variable in Oracle function

I have a variable and want to use in a query inside fuzzy function but it is giving me some syntax error or wrong result considering the var.
ORA-20000: Oracle Text error:
DRG-50901: text query parser syntax error on line 1, column 21 29902.
00000 - "error in executing ODCIIndexStart() routine"
When I replace the my_var variable in the fuzzy function with some static string it works fine but with variable it is giving me this error.
My query is as follows:
DEFINE my_var = 'Bhularam';
SELECT a.EXTERNALID_ENC,
a.EXTERNALID,
a.TELNUMBER,
a.TELAREACODE,
a.DQ_ENGLISH_NAME,
a.DQ_ARABIC_NAME,
a.NAMEFIELD_1,
a.USAGETYPE,
a.MANUAL_UPDATE_FLAG,
a.RULE_UPDATE_FLAG,
a.BUSINESS_UPDATE_FLAG,
a.EXCEL_UPDATE_FLAG
FROM (
SELECT * FROM (
SELECT dqlist.*,
score(1) AS rank
FROM dq_list_hash_full dqlist
WHERE contains(dqlist.dq_english_name
,'definescore(fuzzy(my_var, 1, 6, weight),relevance)',1) > 0
UNION
SELECT
dqlist.*,
score(1) AS rank
FROM
dq_list_hash_full dqlist
WHERE
contains(dqlist.dq_english_name,'!Bhularam',1) > 0
)
ORDER BY
rank DESC
) a
I know it is something really stupid but I am unable to get my head around it probably I am new to oracle. Please help me out.
If using sqlplus, verify what prefix character is used to identify substitution variables. Default is set to '&'.
sqlplus > show define
define "&" (hex 26)
Try using your substitution variable within your query, for example
sqlplus > define my_var='hello world!'
sqlplus > select '&my_var' from dual;
old 1: select '&my_var' from dual
new 1: select 'hello world!' from dual
'HELLOWORLD!'
--------------------------------
hello world!
For your query try (assuming define is set to '&'):
'definescore(fuzzy(&my_var, 1, 6, weight),relevance)',1)

weird query behavior oracle sql - convert text to number

oracle query This works
select *
from (
select to_number(substr(app_cluster,6,2), '99') as b
from xtern_app_info
WHERE app_cluster IS NOT NULL
AND APP_CLUSTER <> 'CLUSTER'
);
but when adding 'where b > 2' makes an error, why?
select *
from (
select to_number(substr(app_cluster,6,2), '99') as b
from xtern_app_info
WHERE app_cluster IS NOT NULL
AND APP_CLUSTER <> 'CLUSTER'
) where b > 2;
ORA-29913: error in executing ODCIEXTTABLEFETCH callout
ORA-01722: invalid number
29913. 00000 - "error in executing %s callout"
*Cause: The execution of the specified callout caused an error.
*Action: Examine the error messages take appropriate action.

How to pass value in xmltable

How can I pass a variable value inside the xpath of xmltable?
DECLARE
v NUMBER := 0;
BEGIN
SELECT *
FROM xml_billrun_files t ,
xmltable('/invoice/AR_ITEMS[#elem='||v||']/ITEMS/USAGE_RECORDS/SESSION_INFO'
passing t.update_xmldoc
columns chrg_duration VARCHAR2(20) path 'DURATION',
amount NUMBER path 'AMOUNT') x;
END;
I've tried this one.
DECLARE
v NUMBER := 0;
BEGIN
SELECT *
FROM xml_billrun_files t ,
xmltable('/invoice/AR_ITEMS[#elem=$i]/ITEMS/USAGE_RECORDS/SESSION_INFO'
passing t.update_xmldoc, xmltype(v) as "i"
columns chrg_duration VARCHAR2(20) path 'DURATION',
amount NUMBER path 'AMOUNT') x;
END;
But it returns an error:
ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00210: expected '<' instead of '0'
Error at line 1
ORA-06512: at "SYS.XMLTYPE", line 310
ORA-06512: at line 1
ORA-06512: at line 5
31011. 00000 - "XML parsing failed"
*Cause: XML parser returned an error while trying to parse the document.
*Action: Check if the document to be parsed is valid.
thanks for the help.
In my answer to your previous question, I incorrectly used xmltype(lp) as "lp" for your XMLQuery call. Not sure why it didn't complain there, but it didn't actually restrict the match anyway...
For this XMLTable call you can pass the number directly, without conversion/cast, as it's already a number:
SELECT chrg_duration, amount
into ...
FROM xml_billrun_files t ,
xmltable('/invoice/AR_ITEMS[#elem=$i]/ITEMS/USAGE_RECORDS/SESSION_INFO'
passing t.update_xmldoc, v as "i"
columns chrg_duration VARCHAR2(20) path 'DURATION',
amount NUMBER path 'AMOUNT') x;
If you're doing it in a for loop, then the index is the wrong data type, and you need to cast it to number:
SELECT chrg_duration, amount
into ...
FROM xml_billrun_files t ,
xmltable('/invoice/AR_ITEMS[#elem=$i]/ITEMS/USAGE_RECORDS/SESSION_INFO'
passing t.update_xmldoc, cast(v as number) as "i"
columns chrg_duration VARCHAR2(20) path 'DURATION',
amount NUMBER path 'AMOUNT') x;

How can I insert multiple xml nodes with XMLQuery (XQuery) in Oracle SQL?

I have created the following test case to demonstrate my issue:
create table test_table (idx number, a varchar2(20), b varchar2(20));
insert into test_table values (1, 'item1', 'value1');
insert into test_table values (2, 'item2', 'value2');
select appendChildXML(
xmltype('<inventory></inventory>'),
'/inventory',
xmlagg(
xmlelement("id", xmlattributes(tt.idx as "val"),
xmlelement("listing",
xmlelement("item",tt.a),
xmlelement("value",tt.b)
)))) as xml
from test_table tt
;
This gives the desired output of:
<inventory>
<id val="1">
<listing>
<item>item1</item>
<value>value1</value>
</listing>
</id>
<id val="2">
<listing>
<item>item2</item>
<value>value2</value>
</listing>
</id>
</inventory>
However, if I try to use XMLQuery I get an error.
select XMLQuery(
(
'copy $tmp := . modify insert node '
|| xmlagg(
xmlelement("id", xmlattributes(tt.idx as "val"),
xmlelement("listing",
xmlelement("item",tt.a),
xmlelement("value",tt.b)
)))
|| ' as last into $tmp/inventory return $tmp'
)
PASSING xmltype('<inventory></inventory>') RETURNING CONTENT
) as xml
from test_table tt
;
Error:
ORA-19112: error raised during evaluation:
XVM-01003: [XPST0003] Syntax error at 'id'
1 copy $tmp := . modify insert node <id val="1"><listing><item>item1</item><v
- ^
19112. 00000 - "error raised during evaluation: %s"
*Cause: The error function was called during evaluation of the XQuery expression.
*Action: Check the detailed error message for the possible causes.
I believe the problem has to do with the fact that I'm inserting multiple id nodes as it will work if I only have one in the table, but I don't understand why appendChildXML will work and XMLQuery will not.
I'm guessing maybe I need to use a FLWOR expression, but I haven't been able to create one that works.
I am currently using Oracle 11g and will be moving to 12c (trying to move to XMLQuery since appendChildXML is deprecated in 12c). I have little experience with XML in Oracle and no previous XMLQuery experience.
Can anyone offer advice on how to get the XMLQuery to work? Thanks!
You can use the ora:view function to query the table and then generate the XML with the FLWOR expression, something like this:
10/12/2015 19:53:25:SQL> SELECT XMLQuery('<inventory>
2 {for $i in ora:view("TEST_TABLE")
3 let $idval := $i/ROW/IDX,
4 $item := $i/ROW/A/text(),
5 $value := $i/ROW/B/text()
6 return <id val="{$idval}">
7 <listing>
8 <item>{$item}</item>
9 <value>{$value}</value>
10 </listing>
11 </id>}
12 </inventory>'
13 RETURNING CONTENT) AS test_xml
14 FROM DUAL;
TEST_XML
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<inventory><id val="1"><listing><item>item1</item><value>value1</value></listing></id><id val="2"><listing><item>item2</item><value>value2</value></listing></id></inventory>
I was able to get the results I needed via the query below. Thanks to Francisco Sitja for his answer and FLWOR expression that guided me toward the complete answer.
select XMLQuery(
(
'copy $tmp := . modify insert node '
|| 'for $i in ora:view("TEST_TABLE")
let $idval := $i/ROW/IDX,
$item := $i/ROW/A/text(),
$value := $i/ROW/B/text()
return <id val="{$idval}">
<listing>
<item>{$item}</item>
<value>{$value}</value>
</listing>
</id>'
|| ' as last into $tmp/inventory return $tmp'
)
PASSING xmltype('<inventory></inventory>') RETURNING CONTENT
) as xml
from dual;

How to insert XML attributes in oracle tables using XMLTABLE Type

Please help to resolve the issue.
I verified previous questions in the same blog but could not able to resolve the issue.
SELECT x.* --,--XXAR_CUST_ACCT_INT_TBL_S.nextval
FROM XMLTABLE ('for $i in $wh//NewCustomerToEBS,
$j in $i//SiteLine
$j in $i//ContactLine
return <deNormalizedWH>{$i}{$j}</deNormalizedWH>'
PASSING xmltype (y)
AS "wh"
COLUMNS ATTRIBUTE2 VARCHAR2(20) path NewCustomerToEBS/#AccessoryFulfilmentOrg',
CUSTOMER_TYPE_SITE VARCHAR2(80) path 'SiteLine/#AccountType',
CUSTOMER_NUMBER_CONTACT VARCHAR2(40) path 'ContactLine/#AccountNumber') x.
when I try to execute the select statement in my custom procedure, I am getting below error:
This is my complete xml file, but for reference I gave 3 columns (each from header, line and contact).
DECLARE
P_XML_DATA CLOB;
BEGIN
P_XML_DATA := '<NewCustomersToEBS> <NewCustomerToEBS AccessoryFulfilmentOrg="TEST" AccountType="TEST1" AlternateEmail="syad#tek.com" BlindShippments="NHGSV" Classification="HXHB" ComplianceCustomer="SKBCS" CustomerCategory="IUYT" CustomerNumber="FGGD12" CustomerProspectCode="LKOU" CustomerType="LOUTR" EDISDQSegmentCustomer="LOUY" Email="HDEG" Fax="HHBGG" FreeOnBoardPoint="KJHTR" FreightTerms="LKJ" OrderType="HBHJBJ" OrganizationName="VEN" Phone="LKJU87" PriceList="knjj" ProfileClass="MJRFS" RequestDataType="JNCJA" RoutingWindow="OIJH" SalesChannel="MNJH" ShipSet="RDSA" ShippingWindow="LKOP" StoreLocations="EDCG" UPCLabletype="LKJUO" URLs="JXABHCBH"> <SiteLine AccountType="JHFJS" AlternateEmailAddress="SV" BillToAddressLine1="SVSF" BillToAddressLine2="HGG" BillToAddressLine3="DBH" BillToCity="BJKD" BillToCountry="BB" BillToPostalCode="2323" BillToProvinice="BD" BillToState="GSDG" CensusRegion="DH" Country="HH" CustomerProspectCode="43T" CustomerType="HFHF" ECSRetailStoreLocation="FHF" EDILocation="HFHF" Email="FHF" Fax="5656" FreeOnBoardPoint="NFN" FreightTerms="BC" OrderType="KKMCK" Phone="4756" ProfileClass="KBD" Region="LS" ResidentialAddress="SS" SalesPerson="SW" ShipMethod="BDGD" ShipToAddressLine1="RF" ShipToAddressLine2="ES" ShipToAddressLine3="SS" ShipToCity="FF" ShipToCountry="GG" ShipToPostalCode="76" ShipToProvince="MM" ShipToState="GG" SiteLocation="GGG" SiteNumber="977" StoreLocations="GKV" StoresDistributionCentre="RT" URLs="RRR"/> <SiteLine AccountType="LOKI" AlternateEmailAddress="JJS" BillToAddressLine1="KMKK" BillToAddressLine2="POKJ" BillToAddressLine3="KIJO" BillToCity="PL" BillToCountry="OKJ" BillToPostalCode="OU" BillToProvinice="PL" BillToState="MN" CensusRegion="NB" Country="JH" CustomerProspectCode="KH" CustomerType="LK" ECSRetailStoreLocation="MJ" EDILocation="CF" Email="GF" Fax="987" FreeOnBoardPoint="MN" FreightTerms="LG" OrderType="UY" Phone="23" ProfileClass="LE" Region="DE" ResidentialAddress="ER" SalesPerson="RT" ShipMethod="TY" ShipToAddressLine1="YU" ShipToAddressLine2="IU" ShipToAddressLine3="SD" ShipToCity="DF" ShipToCountry="FG" ShipToPostalCode="34" ShipToProvince="NH" ShipToState="HG" SiteLocation="JK" SiteNumber="98" StoreLocations="YT" StoresDistributionCentre="RE" URLs="EW"/> <ContactLine AccountNumber="123" Acknowledgement="GGG" Email="NB" FaxAreaCode="54" FaxNumber="456" FaxType="BB" FirstName="KJ" Invoices="HG" JobTitle="FD" LastName="DS" OrganizationName="VG" PhoneAreaCode="66" PhoneNumber="55" PhoneType="XX" ShipTo="XX"/><ContactLine AccountNumber="345" Acknowledgement="RT" Email="TT" FaxAreaCode="33" FaxNumber="22" FaxType="BC" FirstName="SS" Invoices="JH" JobTitle="HH" LastName="JJ" OrganizationName="JHG" PhoneAreaCode="57" PhoneNumber="99" PhoneType="90" ShipTo="MM"/> </NewCustomerToEBS></NewCustomersToEBS>';
XXAR_CUSTOMER_INBOUND_PROC(P_XML_DATA);
commit;
END;
ORA-19114: XPST0003 - error during parsing the XQuery expression:
LPX-00801: XQuery syntax error at 'j'
3 $j in $i//ContactLine
- ^
ORA-06512: at "APPS.XXAR_CUSTOMER_INBOUND_PROC", line 8
ORA-06512: at line 13
Please review the error message and let me know where i am doing mistake.
Thanks in advance!!
You need a , after $j in $i//SiteLine - this is the cause for the error.
But you also used $j twice (which might work but give wrong results).
Try:
SELECT x.*
FROM XMLTABLE ('for $i in $wh//NewCustomerToEBS,
$j in $i//SiteLine,
$z in $i//ContactLine
return <deNormalizedWH>{$i}{$j}{$z}</deNormalizedWH>'
PASSING xmltype (y)
AS "wh"
COLUMNS ATTRIBUTE2 VARCHAR2(20) path 'NewCustomerToEBS/#AccessoryFulfilmentOrg',
CUSTOMER_TYPE_SITE VARCHAR2(80) path 'SiteLine/#AccountType',
CUSTOMER_NUMBER_CONTACT VARCHAR2(40) path 'ContactLine/#AccountNumber') x
Here is a sqlfiddle demo