Traversing Two XMLs in getting intersection of data in SQL - sql

I have two xmls like below. i want to return the first xml in which ids are matched with second xml.
DECLARE
#x xml,
#y xml
SET #x = '<roots><root><id>1</id><name>rp</name></root><root><id>2</id><name>pr</name></root><root><id>3</id><name>rrp</name></root></roots>'
SET #y = '<root><id>1</id><id>5</id><id>6</id></root>'
output: <roots><root><id>1</id><name>rp</name></root></roots>
i am able to acheive this with loop. But is there any simple way to do this.

Shred #x on roots/root and extract the XML and the value for id.
Check against #y in the where clause using exist() and sql:column().
select X.root
from (
select X.X.query('*') as root,
X.X.value('(id/text())[1]', 'int') as id
from #x.nodes('/roots/root') as X(X)
)
as X
where #y.exist('/root/id/text()[. = sql:column("X.id")]') = 1
for xml path(''), root('roots')
Result:
<roots>
<root>
<id>1</id>
<name>rp</name>
</root>
</roots>

Related

How to add additional XML node on top of my SQL generated XML

I have generated XML from a SQL Server FOR XML PATH statement as shown here:
USE MySQLDB
SELECT *
FROM BillTable
FOR XML PATH ('BillAdd'), ROOT ('BillAddRq')
And this is the result:
<BillAddRq>
<BillAdd>
<TxnID>2432-1071510295</TxnID>
<TimeCreated>2003-12-16T01:44:55</TimeCreated>
<TimeModified>2015-12-15T22:38:33</TimeModified>
<EditSequence>1450190313</EditSequence>
<TxnNumber>413</TxnNumber>
<VendorRef_ListID>E0000-933272656</VendorRef_ListID>
<VendorRef_FullName>Timberloft Lumber</VendorRef_FullName>
<APAccountRef_ListID>C0000-933270541</APAccountRef_ListID>
<APAccountRef_FullName>Accounts Payable</APAccountRef_FullName>
<TxnDate>2016-12-01T00:00:00</TxnDate>
<DueDate>2017-12-31T00:00:00</DueDate>
<AmountDue>80.50000</AmountDue>
<TermsRef_ListID>50000-933272659</TermsRef_ListID>
<TermsRef_FullName>1% 10 Net 30</TermsRef_FullName>
<IsPaid>0</IsPaid>
</BillAdd>
<BillAdd>
<TxnID>243A-1071510389</TxnID>
<TimeCreated>2003-12-16T01:46:29</TimeCreated>
<TimeModified>2015-12-15T22:38:33</TimeModified>
<EditSequence>1450190313</EditSequence>
<TxnNumber>414</TxnNumber>
<VendorRef_ListID>C0000-933272656</VendorRef_ListID>
<VendorRef_FullName>Perry Windows & Doors</VendorRef_FullName>
<APAccountRef_ListID>C0000-933270541</APAccountRef_ListID>
<APAccountRef_FullName>Accounts Payable</APAccountRef_FullName>
<TxnDate>2016-12-02T00:00:00</TxnDate>
<DueDate>2018-01-01T00:00:00</DueDate>
<AmountDue>50.00000</AmountDue>
<TermsRef_ListID>10000-933272658</TermsRef_ListID>
<TermsRef_FullName>Net 30</TermsRef_FullName>
<IsPaid>0</IsPaid>
</BillAdd>
</BillAddRq>
Now, I'd like to encapsulate the above with these nodes:
<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="15.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
//above generated xml//
</QBXMLMsgsRq>
</QBXML>
How will I achieve this in a SQL Query I created above?
I am new to SQL Server and XML. I am trying to generate this XML directly from my database and vice versa to make it more efficient and faster — let my SQL directly communicate with XML.
ATTEMPT 1:
USE MySQLDB;
GO
DECLARE #myDoc XML;
SET #myDoc = '<QBXML>
<QBXMLMsgsRq onError="stopOnError">
</QBXMLMsgsRq>
</QBXML>';
SET #myDoc.modify('
insert
-- instead of inserting string here.. I would like to insert here the query I made above
into (/QBXML/QBXMLMsgsRq)[1]');
SELECT #myDoc;
ATTEMPT 2:
USE MySQLDB;
GO
DECLARE #myDoc XML;
SET #myDoc = '<QBXML>
<QBXMLMsgsRq onError="stopOnError">
</QBXMLMsgsRq>
</QBXML>';
DECLARE #qry XML;
SET #qry = (SELECT * FROM BillTable FOR XML PATH ('BillAdd'), ROOT ('BillAddRq'));
-- SELECT #qry;
SET #myDoc.modify('insert #qry
into (/QBXML/QBXMLMsgsRq)[1]');
SELECT #myDoc;
There are many ways to construct your XML result, consider the following three alternatives...
Use XML.modify() to insert the BillTable XML into an XML scalar variable (which includes the ?qbxml XML processing instruction):
declare #BillTableXml xml = (
select *
from BillTable
for xml path('BillAdd'), root('BillAddRq')
);
declare #myDoc xml = '<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="15.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
</QBXMLMsgsRq>
</QBXML>';
set #myDoc.modify('
insert sql:variable("#BillTableXml")
into (/QBXML/QBXMLMsgsRq)[1]
');
select #myDoc as Result;
Use a nested query to construct the entire XML result (which does not, however, include the ?qbxml XML processing instruction):
select
'stopOnError' as [QBXML/QBXMLMsgsRq/#onError],
(
select *
from BillTable
for xml path('BillAdd'), root('BillAddRq'), type
) as [QBXML/QBXMLMsgsRq]
for xml path('');
Or use an XQuery to construct the entire XML result (which also includes the ?qbxml XML processing instruction):
select BillTableXml.query('
<?qbxml version="15.0"?>,
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
{ /BillAddRq }
</QBXMLMsgsRq>
</QBXML>
') as Result
from (
select *
from BillTable
for xml path('BillAdd'), root('BillAddRq'), type
) Data (BillTableXml);

How to delete an item inside an XML with a certain condition?

I have an XML variable value like this in SQL. (SQL SERVER 18)
DECLARE #result NVARCHAR(55) = '123'
DECLARE #XML XML = '<NewData>
<XML>
<PONo>123</SAPPONo>
<Brand>Goldilucks</Brand>
<Color>Amber Brown</Color>
<Size>L</Size>
<OrderQty>21</OrderQty>
<FinalOrderQty>21</FinalOrderQty>
<Unit>KG</Unit>
<DeliveryDate>2021-07-09T00:00:00+08:00</DeliveryDate>
<BatchNo>GC-L1</BatchNo>
<ExpiryDate>2021-08-23T00:00:00+08:00</ExpiryDate>
</XML>
</NewData>'
I want to remove the
<BatchNo>GC-L1</BatchNo> and <ExpiryDate>2021-08-23T00:00:00+08:00</ExpiryDate>
if the <PONo> = '123'.
How can I achieve that in SQL?
I am trying this solution
SET #XML.modify('delete /NewDataSet/XML/BatchNo'[PONo = sql:variable(#result)])
SET #XML.modify('delete /NewDataSet/XML/ExpiryDate'[PONo = sql:variable(#result)])
But the said items still exist in my XML.
Thank you

How to pass index value from a node in XML DML with SQL

Starting with XML DML in SQL Server, pretty fine at the moment, but I am facing this challenge. I need to iterate through some defined nodes in XML data stored in SQL Server.
Already check this as reference, it gives a clue but still I did not figure it out how to send a SQL variable as an index in XML DML Reference.
Suppose the following XML data:
<materials>
<est_mat>
<pos>20</pos>
<item>BOX</item>
<qty>0.004</qty>
</est_mat>
<est_mat>
<pos>30</pos>
<item>xxx-xxx-xxx01</item>
<qty>1</qty>
</est_mat>
<est_mat>
<pos>40</pos>
<item>xxx-xxx-xxx02</item>
<qty>1</qty>
</est_mat>
</materials>
So what I am looking is to iterate through all number of <est_mat> nodes and replace <pos> attribute starting from 10, then next node will be 20 and so on.
So far I have this :
--starting of code
declare #cnt int = 10
declare #totalchildren varchar(300)
declare #pos int = 1
--returns the number of nodes
SET #totalchildren = (SELECT (XMLData.value('count(/materials/est_mat)', 'int'))
FROM TABLE_XMLFiles
WHERE myref = 173)
WHILE #cnt < #totalchildren
BEGIN
--PRINT #cnt
UPDATE TABLE_XMLFiles
SET XMLData.modify('replace value of (/materials/est_mat/pos[sql:variable("#pos")])[[1]] with sql:variable("#cnt")')
WHERE myref = 173
SET #cnt = #cnt + 1
SET #pos = #pos + 10
END
--end of code
Error:
XQuery [BinControl_XMLFiles.XMLData.modify()]: The target of 'replace value of' must be a non-metadata attribute or an element with simple typed content, found 'element(pos,xdt:untyped) ?'
Question is: how I can send a SQL variable as an index position like this:
SET XMLData.modify('replace value of (/materials/est_mat/pos/text())[sql:variable("#pos")]
with sql:variable("#cnt")')
as the value which I am replacing it works by sending it this way with sql:variable("#cnt") - already tried it and works but I am still not figuring it out how to send a variable through the index context.
Thanks in advance for your attention.
Why not just ignore the exsting <pos>-element and re-build the XML?
DECLARE #xml XML=
N'<materials>
<est_mat>
<pos>20</pos>
<item>BOX</item>
<qty>0.004</qty>
</est_mat>
<est_mat>
<pos>30</pos>
<item>xxx-xxx-xxx01</item>
<qty>1</qty>
</est_mat>
<est_mat>
<pos>40</pos>
<item>xxx-xxx-xxx02</item>
<qty>1</qty>
</est_mat>
</materials>';
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) * 10 AS pos
,em.value(N'item[1]',N'nvarchar(max)') AS item
,em.value(N'qty[1]',N'decimal(16,4)') AS qty
FROM #xml.nodes(N'/materials/est_mat') AS A(em)
FOR XML PATH('est_mat'),ROOT('materials')
UPDATE Your follow-up question
(Please avoid chameleon questions!)
Your structure might be queried in two combined steps. One query picks out all existing nodes, which are not called <materials> and then adds the query I stated above as a sub-element.
Hint The appropriate date format within XML is ISO8601. Your value 02092017 is culture depending and therefore something you should avoid. Better 2017-02-09 or 2017-02-09T00:00:00 (If it's not the 2nd of September :-) )
DECLARE #xml XML=
N'<order>
<orderbook>
<date> 02092017 </date>
</orderbook>
<materials>
<est_mat>
<pos>20</pos>
<item>BOX</item>
<qty>0.004</qty>
</est_mat>
<est_mat>
<pos>30</pos>
<item>xxx-xxx-xxx01</item>
<qty>1</qty>
</est_mat>
<est_mat>
<pos>40</pos>
<item>xxx-xxx-xxx02</item>
<qty>1</qty>
</est_mat>
</materials>
</order>';
SELECT #xml.query(N'/order/*[local-name()!="materials"]') AS [*]
,(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) * 10 AS pos
,em.value(N'item[1]',N'nvarchar(max)') AS item
,em.value(N'qty[1]',N'decimal(16,4)') AS qty
FROM #xml.nodes(N'order/materials/est_mat') AS A(em)
FOR XML PATH('est_mat'),ROOT('materials'),TYPE
)
FOR XML PATH(N'order');
Attention: The XML's inner order of nodes might be changed...

Passing irregular xml file to the stored procedure

I have a sample xml as follows. I am trying to get all or specific data and then insert into the my sql table which has the same columns representing the values coming from xml. I looked through some solutions but the xml files are not formatted like I have in here. Can you help me?
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<VehicleStatusResponse xmlns:ns2=
"http://fms-standard.com/rfms/v1.0.0/xsd/common/position" xmlns="http://fms-standard.com/rfms/v1.0.0/xsd/status">
<VehicleStatus>
<VIN>VF254ANA735752628</VIN>
<TriggerType>TIMER</TriggerType>
<CreatedDateTime>2014-09-08T09:30:20</CreatedDateTime>
<ReceivedDateTime>2014-09-08T09:30:57</ReceivedDateTime>
<GNSSPosition>
<ns2:Latitude>49.18557</ns2:Latitude>
<ns2:Longitude>11.18557</ns2:Longitude>
<ns2:Heading>33</ns2:Heading>
<ns2:Altitude>500</ns2:Altitude>
<ns2:Speed>16.4</ns2:Speed>
<ns2:PositionDateTime>2014-09-08T09:30:20</ns2:PositionDateTime>
</GNSSPosition>
<WheelBasedSpeed>16.07</WheelBasedSpeed>
<TachographSpeed>15.83</TachographSpeed>
<HRTotalVehicleDistance>817.5</HRTotalVehicleDistance>
<EngineTotalFuelUsed>575</EngineTotalFuelUsed>
<FuelLevel1>83</FuelLevel1>
<CatalystFuelLevel>88.48</CatalystFuelLevel>
<GrossCombinationVehicleWeight>10000</GrossCombinationVehicleWeight>
</VehicleStatus>
</VehicleStatusResponse>
You can use an XML type and XML methods if you remove or modify the declaration. The SQL Server XML type only supports UCS-2 encoding and doesn't recognize "standalone". The example below uses string manipulation to tweak the declaration. You'll need to change the data types according to your actual column types and should specify an explicit column list on the INSERT statement. I omitted that in this example only because I didn't want to assume your actual table columns matched the element names in the XML.
DECLARE #xml xml;
DECLARE #xmlString nvarchar(MAX) = N'<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<VehicleStatusResponse xmlns:ns2=
"http://fms-standard.com/rfms/v1.0.0/xsd/common/position" xmlns="http://fms-standard.com/rfms/v1.0.0/xsd/status">
<VehicleStatus>
<VIN>VF254ANA735752628</VIN>
<TriggerType>TIMER</TriggerType>
<CreatedDateTime>2014-09-08T09:30:20</CreatedDateTime>
<ReceivedDateTime>2014-09-08T09:30:57</ReceivedDateTime>
<GNSSPosition>
<ns2:Latitude>49.18557</ns2:Latitude>
<ns2:Longitude>11.18557</ns2:Longitude>
<ns2:Heading>33</ns2:Heading>
<ns2:Altitude>500</ns2:Altitude>
<ns2:Speed>16.4</ns2:Speed>
<ns2:PositionDateTime>2014-09-08T09:30:20</ns2:PositionDateTime>
</GNSSPosition>
<WheelBasedSpeed>16.07</WheelBasedSpeed>
<TachographSpeed>15.83</TachographSpeed>
<HRTotalVehicleDistance>817.5</HRTotalVehicleDistance>
<EngineTotalFuelUsed>575</EngineTotalFuelUsed>
<FuelLevel1>83</FuelLevel1>
<CatalystFuelLevel>88.48</CatalystFuelLevel>
<GrossCombinationVehicleWeight>10000</GrossCombinationVehicleWeight>
</VehicleStatus>
</VehicleStatusResponse>';
SET #xmlString = REPLACE(#xmlString, 'encoding="UTF-8"', 'encoding="UCS-2"');
SET #xmlString = REPLACE(#xmlString, 'standalone="true"', '');
SET #xml = #xmlString;
WITH XMLNAMESPACES (
DEFAULT 'http://fms-standard.com/rfms/v1.0.0/xsd/status'
,'http://fms-standard.com/rfms/v1.0.0/xsd/common/position' AS ns2
)
INSERT INTO dbo.YourTable
SELECT
#xml.value('(/VehicleStatusResponse/VehicleStatus/VIN)[1]', 'varchar(50)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/TriggerType)[1]', 'varchar(50)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/CreatedDateTime)[1]', 'datetime2(3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/ReceivedDateTime)[1]', 'datetime2(3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GNSSPosition/ns2:Latitude)[1]', 'decimal(8,5)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GNSSPosition/ns2:Longitude)[1]', 'decimal(8,5)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GNSSPosition/ns2:Heading)[1]', 'int')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GNSSPosition/ns2:Altitude)[1]', 'int')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GNSSPosition/ns2:Speed)[1]', 'decimal(8,3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GNSSPosition/ns2:PositionDateTime)[1]', 'datetime2(3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/WheelBasedSpeed)[1]', 'decimal(8,3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/TachographSpeed)[1]', 'decimal(8,3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/HRTotalVehicleDistance)[1]', 'decimal(8,3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/EngineTotalFuelUsed)[1]', 'int')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/CatalystFuelLevel)[1]', 'decimal(8,3)')
, #xml.value('(/VehicleStatusResponse/VehicleStatus/GrossCombinationVehicleWeight)[1]', 'int');
First of all you need to get your value into a declare variable of type XML or into an XML-typed data table column. As your XML contains namespaces you have to declare them in a WITH XMLNAMESPACES first. You might use wildcard syntax (*:), but its better to be as specific as possible.
The .nodes() call navigates to the Level of <VehicleStatus>. All elements below are simply 1:1 and easy to read...
You can try it like this:
DECLARE #xml XML=
N'<VehicleStatusResponse xmlns:ns2=
"http://fms-standard.com/rfms/v1.0.0/xsd/common/position" xmlns="http://fms-standard.com/rfms/v1.0.0/xsd/status">
<VehicleStatus>
<VIN>VF254ANA735752628</VIN>
<TriggerType>TIMER</TriggerType>
<CreatedDateTime>2014-09-08T09:30:20</CreatedDateTime>
<ReceivedDateTime>2014-09-08T09:30:57</ReceivedDateTime>
<GNSSPosition>
<ns2:Latitude>49.18557</ns2:Latitude>
<ns2:Longitude>11.18557</ns2:Longitude>
<ns2:Heading>33</ns2:Heading>
<ns2:Altitude>500</ns2:Altitude>
<ns2:Speed>16.4</ns2:Speed>
<ns2:PositionDateTime>2014-09-08T09:30:20</ns2:PositionDateTime>
</GNSSPosition>
<WheelBasedSpeed>16.07</WheelBasedSpeed>
<TachographSpeed>15.83</TachographSpeed>
<HRTotalVehicleDistance>817.5</HRTotalVehicleDistance>
<EngineTotalFuelUsed>575</EngineTotalFuelUsed>
<FuelLevel1>83</FuelLevel1>
<CatalystFuelLevel>88.48</CatalystFuelLevel>
<GrossCombinationVehicleWeight>10000</GrossCombinationVehicleWeight>
</VehicleStatus>
</VehicleStatusResponse>';
--This is the query
WITH XMLNAMESPACES(DEFAULT 'http://fms-standard.com/rfms/v1.0.0/xsd/status'
,'http://fms-standard.com/rfms/v1.0.0/xsd/common/position' AS ns2)
SELECT vs.value('VIN[1]','nvarchar(max)') AS VehicleStatus_VIN
,vs.value('TriggerType[1]','nvarchar(max)') AS VehicleStatus_TriggerType
,vs.value('CreatedDateTime[1]','datetime') AS VehicleStatus_CreatedDateTime
,vs.value('ReceivedDateTime[1]','datetime') AS VehicleStatus_ReceivedDateTime
,vs.value('(GNSSPosition/ns2:Latitude)[1]','decimal(14,6)') AS VehicleStatus_GNSSPosition_Latitude
,vs.value('(GNSSPosition/ns2:Longitude)[1]','decimal(14,6)') AS VehicleStatus_GNSSPosition_Longitude
/*other columns follow the same pattern*/
FROM #xml.nodes('/VehicleStatusResponse/VehicleStatus') AS A(vs)
update: insert into a table
Easiest was to wrap this call as CTE like
WITH XMLNAMESPACES(...)
,DerivedTableCTE AS
(
The query here
)
INSERT INTO YourTable (col1, col2, col3, ...)
SELECT col1, col2, col3, ...
FROM DerivedTableCTE

SQL Server 2005 with XML Namespaces - Query Multiple Nodes and Properties

I have the following XML and code and can pull back the AccountNumber and Status. I also need to pull back...
The ID property of the MFRRequest
Several elements from the CaseInformation node
I am not sure how to get the ID property and I'm wondering... can I get all of this information (from multiple nodes) back in the same query?
Thank you!
DECLARE #doc xml
SET #doc =
'<?xml version="1.0" encoding="UTF-8"?>
<p:OrderRequest xmlns:p="http://XXX.Schemas.OrderRequest/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://XXX.Schemas.OrderRequest/2 OrderRequestV2.xsd ">
<p:Header>
<p:SCRARequestCount>0</p:SCRARequestCount>
<p:MFRRequestCount>1</p:MFRRequestCount>
<p:FileCreatedAt>2001-12-31T12:00:00</p:FileCreatedAt>
<p:RequestFileName>p:RequestFileName</p:RequestFileName>
</p:Header>
<p:RequestItems>
<p:MFRRequest id="1" priority="">
<p:AccountNumber>9999999</p:AccountNumber>
<Status>Initial</Status>
<p:CaseInformation>
<ReferralDate>2011-01-01</ReferralDate>
<LoanType>1A</LoanType>
<ARM>Yes</ARM>
<InvestorNumber>InvestorNumber</InvestorNumber>
<PropertyAddress>PropertyAddress</PropertyAddress>
<PrivateLabel>Yes</PrivateLabel>
<CaseNumber>01-11111/AK/</CaseNumber>
<SuspenseBalance>9999.00</SuspenseBalance>
<TitleOrderedDate>2011-09-01</TitleOrderedDate>
<TotalMonthlyPayment>876.99</TotalMonthlyPayment>
</p:CaseInformation>
</p:MFRRequest>
</p:RequestItems>
</p:OrderRequest>'
;WITH XMLNAMESPACES('http://XXX.Schemas.OrderRequest/2' AS p)
SELECT
Y.i.value('Status[1]', 'varchar(10)') AS Status ,
Y.i.value('p:AccountNumber[1]', 'varchar(10)') AS AccountNumber
FROM #doc.nodes('/p:OrderRequest/p:RequestItems/p:MFRRequest') AS Y(i)
Try something like this:
;WITH XMLNAMESPACES('http://XXX.Schemas.OrderRequest/2' AS p)
SELECT
MFRRequestID = Y.i.value('(#id)[1]', 'int'),
RequestStatus = Y.i.value('Status[1]', 'varchar(10)') ,
AccountNumber = Y.i.value('p:AccountNumber[1]', 'varchar(10)'),
ReferralDate = CIF.value('(ReferralDate)[1]', 'varchar(25)'),
CaseInfoType = CIF.value('(Type)[1]', 'varchar(25)')
FROM
#doc.nodes('/p:OrderRequest/p:RequestItems/p:MFRRequest') AS Y(i)
CROSS APPLY
i.nodes('p:CaseInformation') AS Tbl(CIF)
Basically, once you have the <p:MFRRequest> XML element in your Y(i) pseudo-table, you can then again use a second call to .nodes() to get the (possibly multiple) <p:CaseInfomration> nodes and use .value() calls on this new second pseudo-table to extract single bits of information from it.