How to pass value in xmltable - sql

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;

Related

ORA-22992 / ORA-06502+ORA-06512 errors

I'm using this part of code in large one :
SELECT DISTINCT
P.SKU, SUBSTR(X.ATTRIBUTENAME, 14, 3) ATTRIBUTECODE, X.ATTRIBUTEVALUE
FROM
PRODUCT#ISPSTAG2 P,
XMLTABLE('/attrs/attr'
PASSING XMLTYPE(REGEXP_REPLACE(P.ATTRIBUTES_DE_AT, '<attr name="longDescription">.*?<\/attr>'))
COLUMNS ATTRIBUTENAME VARCHAR2(50) PATH '#name',
ATTRIBUTEVALUE VARCHAR2(4000) PATH '/string'
) X
WHERE X.ATTRIBUTENAME LIKE 'Z_CA%'
AND DN(DOMAINID) = 'AT'
AND SKU NOT LIKE 'OFF_%' AND SKU NOT LIKE 'PDT%'
AND ATTRIBUTES_DE_AT IS NOT NULL;
And it throws an ORA-22992 error.
I've made some research and a tip can to use the dual
SELECT DISTINCT P.SKU, SUBSTR(X.ATTRIBUTENAME, 14, 3) ATTRIBUTECODE, X.ATTRIBUTEVALUE
FROM PRODUCT#ISPSTAG2 P,
XMLTABLE('/attrs/attr'
PASSING XMLTYPE(REGEXP_REPLACE(P.ATTRIBUTES_DE_AT, '<attr name="longDescription">.*?<\/attr>'))
COLUMNS ATTRIBUTENAME VARCHAR2(50) PATH '#name',
ATTRIBUTEVALUE VARCHAR2(4000) PATH '/string'
) X
WHERE X.ATTRIBUTENAME LIKE 'Z_CA%'
AND DN(DOMAINID) = 'AT'
AND SKU NOT LIKE 'OFF_%' AND SKU NOT LIKE 'PDT%'
AND (SELECT ATTRIBUTES_DE_AT FROM DUAL) IS NOT NULL;
But now I get ORA-06502/ORA-06512 errors :
ORA-06502: PL/SQL : erreur numérique ou erreur sur une valeur
ORA-06512: à "SYS.XMLTYPE", ligne 272
ORA-06512: à ligne 1
06502. 00000 - "PL/SQL: numeric or value error%s"
*Cause: An arithmetic, numeric, string, conversion, or constraint error occurred. For example, this error occurs if an attempt is made to assign the value NULL to a variable declared NOT NULL, or if an attempt is made to assign an integer larger than 99 to a variable declared NUMBER(2).
*Action: Change the data, how it is manipulated, or how it is declared so that values do not violate constraints.
But, I executed the first one on ISPSTAG2 and it works, but the second one on ISPSTAG2 returns me the same ORA-06502/ORA-06512 errors so the issue is with the dual subquery.
I also tried to create a view on ISPSTAG2 using DBMS_LOB.SUBSTR with 4000 characters but same error.
Any ideas ? Thank you.
If forgot to use the (SELECT ATTRIBUTES_DE_AT FROM DUAL) subquery inside the XMLTYPE...
SELECT DISTINCT P.SKU, SUBSTR(X.ATTRIBUTENAME, 14, 3) ATTRIBUTECODE, X.ATTRIBUTEVALUE
FROM PRODUCT#ISPSTAG2 P,
XMLTABLE('/attrs/attr'
PASSING XMLTYPE(REGEXP_REPLACE(**(SELECT ATTRIBUTES_DE_AT FROM DUAL)**, '<attr name="longDescription">.*?<\/attr>'))
COLUMNS ATTRIBUTENAME VARCHAR2(50) PATH '#name',
ATTRIBUTEVALUE VARCHAR2(4000) PATH '/string'
) X
WHERE X.ATTRIBUTENAME LIKE 'Z_CA%'
AND DN(DOMAINID) = 'AT'
AND SKU NOT LIKE 'OFF_%' AND SKU NOT LIKE 'PDT%'
AND (SELECT ATTRIBUTES_DE_AT FROM DUAL) IS NOT NULL;
The thing I don't understand is that when don't use the subquery in the IS NOT NULL filter I have the the ORA-22992 error (using distant LOB), so why I have a different error not using the dual subquery, which is the same distant LOB ?
Anyway for you for your time/help :)

XMLQuery with Oracle

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.

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

Issue with dynamic execute immediate query

I have a code in my procedure that looks like this. But when i execute this code, i get the error as mentioned below.
The Error report that i got is:
Error report -
ORA-06553: PLS-306: wrong number or types of arguments in call to 'OGC_Y'
ORA-06512: at line 20
06553. 00000 - "PLS-%s: %s"
*Cause:
*Action:
The error has something to do with primary_flag = "Y" <-- this. How else can i write primary_flag = 'Y' inside a string?
The dynamic query is required in my case.
MY CODE IS:
DECLARE
p_assignee_id NUMBER := 10153;
time_stamp timestamp := '12-DEC-2011';
create_task_view_sql VARCHAR2(4000);
BEGIN
create_task_view_sql:=
'select unique cp.sub_last_name
from cs_sr_contact_points_v cp
where cp.incident_id = 55500
and cp.contact_phone is not null
and primary_flag = "Y"';
dbms_output.put_line(create_task_view_sql);
execute immediate create_task_view_sql;
END;
To embed quoted string in a quoted string, use two single quotes:
'...and primary_flag=''Y''';
Or you can use the newer q' syntax to avoid doubling up the embedded quotes:
q'[...and primary_flag='Y']';

How to Convert Output of listagg() function into XMLTYPE

I have one stored Procedure P_FP_GET_PATTERN.I expect output in following format
PATTERN_ID |PATTERN_NAME | SHIFT
1 Pattern 1 A,B,C,B,A
2 Pattern 2 C,B,A,C
I'm getting this output in SYS_REFCURSOR ALL_RESULT_SET.I need to pass it in XML format.But the moment i put that output in ALL_RESULT_SET_XML which is my out parameter of stored procedure of XMLTYPE using ALL_RESULT_SET_XML:= XMLTYPE(ALL_RESULT_SET);
Im getting an error as
Error encountered: ORA-31061: XDB error: special char to escaped char conversion failed.
I`m getting this error due to column shown using LISTAGG() function.Anybody can please tell me how to handle this ?
My stored Procedure
create or replace
PROCEDURE P_FP_GET_PATTERN
(
ALL_RESULT_SET_XML OUT XMLTYPE,
P_MESSAGE_ALL OUT VARCHAR2
)
AS
V_ERROR VARCHAR2(2000);
ALL_RESULT_SET SYS_REFCURSOR;
BEGIN
OPEN ALL_RESULT_SET FOR
SELECT PM.PATTERN_ID ,PM.PATTERN_NAME,
LISTAGG(SM.SHIFT_NUMBER) WITHIN GROUP (ORDER BY PD.INSTANCE_DAY) "SHIFT"
FROM T_FP_PATTERN_MASTER PM,
T_FP_PATTERN_DETAILS PD,
T_FP_SHIFT_MASTER SM
WHERE SM.SHIFT_ID= PD.SHIFT_ID
AND PM.PATTERN_ID = PD.PATTERN_ID
GROUP BY PM.PATTERN_NAME,PM.PATTERN_ID;
--Adding output in XML output parameter
ALL_RESULT_SET_XML:= XMLTYPE(ALL_RESULT_SET);
EXCEPTION
WHEN OTHERS
THEN
V_ERROR := SUBSTR(SQLERRM,1,1000);
P_MESSAGE_ALL := 'Error encountered: '||V_ERROR ;
END;