what is wrong with my simple sqlxml query? - sql-server

Sorry,
Been struggle with this query for a while
declare #a xml
select #a='<GrsAutoCompleteCodes>
<GrsAutoCompleteCode CitiCode="00DX" IsMatched="0" HasCustomName="0" />
<GrsAutoCompleteCode CitiCode="00G0" IsMatched="0" HasCustomName="0" />
</GrsAutoCompleteCodes>'
SELECT
p.s.value('(CitiCode/text())[1]','VARCHAR(100)') AS CitiCode
FROM #a.nodes('/GrsAutoCompleteCode') p(s)
This query somehow returns no records, what am I doing wrongly?

Because it's completely wrong, I'm afraid. Your root is GrsAutoCompleteCodes not GrsAutoCompleteCode, and CitiCode isn't a node it's a property of GrsAutoCompleteCode.
Perhaps you want this, which returns the value of CitiCode for the first GrsAutoCompleteCode node:
DECLARE #a xml;
SET #a = '<GrsAutoCompleteCodes>
<GrsAutoCompleteCode CitiCode="00DX" IsMatched="0" HasCustomName="0" />
<GrsAutoCompleteCode CitiCode="00G0" IsMatched="0" HasCustomName="0" />
</GrsAutoCompleteCodes>'
SELECT a.GACC.value('(GrsAutoCompleteCode/#CitiCode)[1]','varchar(4)') AS CitiCode
FROM #a.nodes('/GrsAutoCompleteCodes')a(GACC);
If you want the value of every CitiCode, that would be this:
SELECT a.GACC.value('#CitiCode','varchar(4)') AS CitiCode
FROM #a.nodes('/GrsAutoCompleteCodes/GrsAutoCompleteCode')a(GACC);

Related

Retrieve associated value from next node for each tag

I have the following XML:
<Envelope format="ProceedoOrderTransaction2.1">
<Sender>SENDER</Sender>
<Receiver>RECEIVER</Receiver>
<EnvelopeID>xxxxx</EnvelopeID>
<Date>2021-05-06</Date>
<Time>11:59:46</Time>
<NumberOfOrder>1</NumberOfOrder>
<Order>
<Header>
<OrderNumber>POXXXXX</OrderNumber>
</Header>
<Lines>
<Line>
<LineNumber>1</LineNumber>
<ItemName>Ipsum Lorum</ItemName>
<SupplierArticleNumber>999999</SupplierArticleNumber>
<UnitPrice vatRate="25.0">50</UnitPrice>
<UnitPriceBasis>1</UnitPriceBasis>
<OrderedQuantity unit="Styck">200</OrderedQuantity>
<AdditionalItemProperty Key="ARTIKELNUMMER" Description="Unik ordermärkning (artikelnummer):" />
<Value>999999</Value>
<AdditionalItemProperty Key="BESKRIVNING" Description="Kort beskrivning:" />
<Value>Ipsum Lorum</Value>
<AdditionalItemProperty Key="BSKRIVNING" Description="Beskrivning:" />
<Value>Ipsum Lorum</Value>
<AdditionalItemProperty Key="ENHET" Description="Enhet:" />
<Value>Styck</Value>
<AdditionalItemProperty Key="KVANTITET" Description="Kvantitet:" />
<Value>200</Value>
<AdditionalItemProperty Key="PRIS" Description="Pris/Enhet (ex. moms):" />
<Value>50</Value>
<AdditionalItemProperty Key="VALUTA" Description="Valuta:" />
<Value>SEK</Value>
<Accounting>
<AccountingLine amount="10000">
<AccountingValue dimensionPosition="001" dimensionExternalID="ACCOUNT">xxx</AccountingValue>
<AccountingValue dimensionPosition="002" dimensionExternalID="F1">Ipsum Lorum</AccountingValue>
<AccountingValue dimensionPosition="005" dimensionExternalID="F3">1</AccountingValue>
<AccountingValue dimensionPosition="010" dimensionExternalID="F2">9999</AccountingValue>
</AccountingLine>
</Accounting>
</Line>
</Lines>
</Order>
</Envelope>
I am able to parse out all values correctly to table structure except for 1 value in a way that ensures its it associated with its tag. So where I stumble is that I am correctly getting 1 row per AdditionalItemProperty and I am able to get the Key and Description tag values, for example BESKRIVNING and Kort beskrivning:, but I can't (in a reasonable way) get the value between <Value> </Value> brackets that is also associated with each tag value. So for tag key value BESKRIVNING the associated value is 99999 which seem to be on same hierarchy level (insane I know) as the AdditionalItemProperty it is associated with. Seems like they use logic that value for a AdditionalItemProperty will be following the AdditionalItemProperty tag.
I am using SQL Server 2019. I have gotten this far:
-- Purchaseorderrowattributes
select top(10)
i.value(N'(./Header/OrderNumber/text())[1]', 'nvarchar(30)') as OrderNumber,
ap.value(N'(../LineNumber/text())[1]', 'nvarchar(30)') as LineNumber,
ap.value(N'(#Description)', 'nvarchar(50)') property_description
from
load.proceedo_orders t
outer apply
t.xml_data.nodes('Envelope/Order') AS i(i)
outer apply
i.nodes('Lines/Line/AdditionalItemProperty') as ap(ap)
where
file_name = #filename
Which produces the following output:
OrderNumber LineNumber property_description
--------------------------------------------
PO170006416 1 Antal timmar
PO170006416 1 Beskrivning
PO170006416 1 Kompetensområde
PO170006416 1 Roll
PO170006416 1 Ordernummer
PO170006416 1 Timpris
I can't find a way to add the value for each property in a correct way. Since the ordering of the values will always be same as the ordering of the AdditionalItemProperty i found solution to get ordering of the AdditionalItemProperty and then i could use rownumber and i was then hoping to input the rownumber value into the bracket in
ap.value(N'(../Value/text())[1]', 'nvarchar(50)') property_description
but SQL Server throws exception that it has to be string literal.
So to be clear what I tried doing with something like:
ap.value(CONCAT( N'(../Value/text())[', CAST(ROWNUMBER as varchar) ,']'), 'nvarchar(50)') property_description
SQL Server uses XQuery 1.0.
You can make use of the >> Node Comparison operator to find the Value sibling node with code similar to the following:
-- Purchaseorderrowattributes
select top(10)
i.value(N'(./Header/OrderNumber/text())[1]', 'nvarchar(30)') as OrderNumber
,ap.value(N'(../LineNumber/text())[1]', 'nvarchar(30)') as LineNumber
,ap.value(N'(#Description)', 'nvarchar(50)') property_description
,ap.query('
let $property := .
return (../Value[. >> $property][1])/text()
').value('.[1]', 'varchar(20)') as property_value
from load.proceedo_orders t
outer apply t.xml_data.nodes('Envelope/Order') AS i(i)
outer apply i.nodes('Lines/Line/AdditionalItemProperty') as ap(ap)
where file_name = #filename
So what's going on here?
let $property := . is creating a reference to the current AdditionalItemProperty node.
../Value[. >> $property] is ascending to the parent node, Line, and descending again to find Value nodes after the AditionalItemProperty node reference in document order, with [1] selecting the first one of those nodes.
See the 3.5.3 Node Comparisons section for a little more detail.

SQL Server - XQuery: delete parent node based on child value substring

I want to delete all parent nodes TxDtls of the following XML where position 20 of child value Ref is 2.
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.04" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.054.001.04 camt.054.001.04.xsd">
<BkToCstmrDbtCdtNtfctn>
<Ntfctn>
<Ntry>
<TtlChrgsAndTaxAmt Ccy="CHF">1.60</TtlChrgsAndTaxAmt>
<Rcrd>
<Amt Ccy="CHF">1.60</Amt>
<CdtDbtInd>DBIT</CdtDbtInd>
<ChrgInclInd>false</ChrgInclInd>
<Tp>
<Prtry>
<Id>2</Id>
</Prtry>
</Tp>
</Rcrd>
</Chrgs>
<NtryDtls>
<TxDtls>
<RmtInf>
<Strd>
<CdtrRefInf>
<Ref>111118144400000000020076766</Ref>
</CdtrRefInf>
</Strd>
</RmtInf>
</TxDtls>
<TxDtls>
<RmtInf>
<Strd>
<CdtrRefInf>
<Ref>111117645600000000030076281</Ref>
</CdtrRefInf>
</Strd>
</RmtInf>
</TxDtls>
</NtryDtls>
</Ntry>
</Ntfctn>
</BkToCstmrDbtCdtNtfctn>
</Document>
So I want to delete the first TxDtls node (substring position 20 = 2) while I want to keep the second one (substring position 20 <> 2).
I tried this:
UPDATE mytable SET XMLData.modify('delete .//TxDtls[RmtInf/Strd/CdtrRefInf/Ref/substring(text(),20,1) = ''2'']')
However, I get the error "The XQuery syntax '/function()' is not supported". Any hints on how to achieve this?
Thanks
What a difference made by the partially provided minimal reproducible example.
The XML is still not well-formed. I had to comment out the following tag: </Chrgs>
A default namespace is easily handled by its declaration in the XQuery method.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE (ID INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO #tbl (xmldata) VALUES
(N'<?xml version="1.0"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.04"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.054.001.04 camt.054.001.04.xsd">
<BkToCstmrDbtCdtNtfctn>
<Ntfctn>
<Ntry>
<TtlChrgsAndTaxAmt Ccy="CHF">1.60</TtlChrgsAndTaxAmt>
<Rcrd>
<Amt Ccy="CHF">1.60</Amt>
<CdtDbtInd>DBIT</CdtDbtInd>
<ChrgInclInd>false</ChrgInclInd>
<Tp>
<Prtry>
<Id>2</Id>
</Prtry>
</Tp>
</Rcrd>
<!--</Chrgs>-->
<NtryDtls>
<TxDtls>
<RmtInf>
<Strd>
<CdtrRefInf>
<Ref>111118144400000000020076766</Ref>
</CdtrRefInf>
</Strd>
</RmtInf>
</TxDtls>
<TxDtls>
<RmtInf>
<Strd>
<CdtrRefInf>
<Ref>111117645600000000030076281</Ref>
</CdtrRefInf>
</Strd>
</RmtInf>
</TxDtls>
</NtryDtls>
</Ntry>
</Ntfctn>
</BkToCstmrDbtCdtNtfctn>
</Document>');
-- DDL and sample data population, end
-- before
SELECT * FROM #tbl;
UPDATE #tbl SET xmldata.modify('declare default element namespace "urn:iso:std:iso:20022:tech:xsd:camt.054.001.04";
delete /Document/BkToCstmrDbtCdtNtfctn/Ntfctn/Ntry/NtryDtls/TxDtls[RmtInf/Strd/CdtrRefInf/Ref[substring(./text()[1],20,1) = "2"]]');
-- after
SELECT * FROM #tbl;

SQL replace value of with always random xml code

I have the following SQL XML in several rows of a table (table is tbldatafeed column in configuration_xml). All of the UserName="" and Password="" is different each time for each row and does not repeat so I can not find/replace off of that. I am trying to write a query that finds all of those and replaces them with Username/Passwords I choose.
<DataFeed xmlns="http://www.tech.com/datafeed/dfx/2010/04" xmlns:plugin="pluginExtensions" Type="TODO" Guid="TODO" UserAccount="DF_LEAN_PopulateCommentsSubForm" Locale="en-US" DateFormat="" ThousandSeparator="" NegativeSymbol="" DecimalSymbol="" SendingNotifications="false" SendJobStatusNotifications="false" RecipientUserIds="" RecipientGroupIds="" RecipientEmailAddresses="" Name="CI_C11.01_Lean-Lean_Reject Comments_A2A" >
<Transporter>
<transporters:ArcherWebServiceTransportActivity xmlns:transporters="clr-namespace:ArcherTech.DataFeed.Activities.Transporters;assembly=ArcherTech.DataFeed" xmlns:out="clr-namespace:ArcherTech.DataFeed;assembly=ArcherTech.DataFeed" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:compModel="clr-namespace:ArcherTech.DataFeed.ComponentModel;assembly=ArcherTech.DataFeed" xmlns:channel="clr-namespace:ArcherTech.DataFeed.Engine.Channel;assembly=ArcherTech.DataFeed" xmlns:engine="clr-namespace:ArcherTech.DataFeed.Engine;assembly=ArcherTech.DataFeed" xmlns:kernel="clr-namespace:ArcherTech.Kernel.Channel;assembly=ArcherTech.Kernel" xmlns="clr-namespace:ArcherTech.DataFeed;assembly=ArcherTech.DataFeed" xmlns:schema="clr-namespace:System.Xml.Schema;assembly=System.Xml" xmlns:xmlLinq="clr-namespace:System.Xml.Linq;assembly=System.Xml" xmlns:domain="clr-namespace:ArcherTech.Common.Domain;assembly=ArcherTech.Common" xmlns:s="clr-namespace:System;assembly=mscorlib" x:Key="transportActivity" SearchType="ReportId" Uri="https://arcs-d" RecordsPerFile="100" ReportID="EC514865-88D5-49CE-A200-7769EC1C2A88" UseWindowsAuth="false" IsWindowsAuthSpecific="false" WindowsAuthUserName="i9XzCczAQ7J2rHwkg6wG9QF8+O9NCYJZP6y5Kzw4be0+cdvUaGu/9+rHuLstU736pnQrRcwmnSIhd6oPKIvnLA==" WindowsAuthPassword="+y0tCAKysxEMSGv1unpHxfg6WjH5XWylgP45P5MLRdQ6+zAdOLSVy7s3KJa3+9j2i83qn8I8K7+1+QBlCJT1E7sLQHWRFOCEdJgXaIr1gWfUEO+7kjuJnZcIEKZJa2wHyqc2Z08J2SKfdCLh7HoLtg==" WindowsAuthDomain="" ProxyName="" ProxyPort="8080" ProxyUsername="" ProxyPassword="" ProxyDomain="" IsProxyActive="False" ProxyOption="None" InstanceName="ARCS-D" TempFileOnSuccessAction="DoNothing" TempFileOnSuccessRenameString="" TempFileOnErrorAction="DoNothing" TempFileOnErrorRenameString="" Transform="{engine:DataFeedBinding Path=Transform}" SessionContext="{engine:DataFeedBinding Path=Session}">
<transporters:ArcherWebServiceTransportActivity.Credentials>
<NetworkCredentialWrapper UserName="TeSZmI1SqO0eJ0G2nDVU+glFg/9eZfeMppYQnPfbeg8=" Password="Slt4VHqjkYscWyCwZK40QJ7KOQroG9OTKr+RGt9bQjE=" />
</transporters:ArcherWebServiceTransportActivity.Credentials>
</transporters:ArcherWebServiceTransportActivity>
</Transporter>
</DataFeed>
I need to be able to set a value and replace it with a query
I have written the following
select #config_xml=configuration_xml from bldatafeed where datafeed_name = 'REMOVED'
update tbldatafeed set configuration_xml.modify(//*:NetworkCredentialWrapper/#UserName)[1] with "abc" ')
where datafeed_name = 'REMOVED'
This does the trick but it only works if I set the "abc" password each time in each area and in some cases I am running this against 50+ rows.
I also tried:
Declare #server nvarchar(max) = 'abc'
Declare #config_xml xml
select #config_xml=configuration_xml from bldatafeed where datafeed_name = 'REMOVED'
update tbldatafeed set configuration_xml.modify(//*:NetworkCredentialWrapper/#UserName)[1] with #server ')
where datafeed_name = 'REMOVED'
The error from this is that: XQuery [tbldatafeed.configuration_xml.modify()]: Top-level attribute nodes are not supported
What I would like to be able to do is set my variable and utilize that as I will be setting this up for multiple rows and unfortunately this error is making this a very difficult problem to solve.
Thanks for any help, this has kept me confused for a bit.
Use the function sql:variable() to use a variable in the XQuery expression.
declare #T table(X xml);
insert into #T values('<X UserName=""/>');
declare #UserName nvarchar(max) = 'abc'
update #T set
X.modify('replace value of (/X/#UserName)[1]
with sql:variable("#UserName")');

Parse an XML value with a colon in SQL Server

I need help to parse XML in SQL Server. I need to get "d1p1:Val2" value and concatenation of values for "d2p1:string".
<FirstData xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:d1p1="http://XXXXXX" xmlns="http://YYYYYY" i:type="d1p1:StaticInfo">
<Timestamp>0</Timestamp>
<ActionResult i:nil="true" />
<d1p1:Val1 xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:string>1</d2p1:string>
<d2p1:string>2</d2p1:string>
<d2p1:string>3</d2p1:string>
<d2p1:string>4</d2p1:string>
</d1p1:Val1>
<d1p1:Val2>false</d1p1:Val2>
</FirstData>
Your question is not very clear, but this might help you:
DECLARE #xml XML=
N'<FirstData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d1p1="http://XXXXXX" xmlns="http://YYYYYY" i:type="d1p1:StaticInfo">
<Timestamp>0</Timestamp>
<ActionResult i:nil="true" />
<d1p1:Val1 xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:string>1</d2p1:string>
<d2p1:string>2</d2p1:string>
<d2p1:string>3</d2p1:string>
<d2p1:string>4</d2p1:string>
</d1p1:Val1>
<d1p1:Val2>false</d1p1:Val2>
</FirstData>';
WITH XMLNAMESPACES(DEFAULT 'http://YYYYYY'
,'http://XXXXXX' AS d1p1
,'http://schemas.microsoft.com/2003/10/Serialization/Arrays' AS d2p1)
SELECT #xml.value(N'(/FirstData/d1p1:Val2/text())[1]','bit') AS D1P1_Val2
,#xml.query(N'data(/FirstData/d1p1:Val1/d2p1:string/text())').value(N'text()[1]',N'nvarchar(max)') AS AllStrings;
The result
D1P1_Val2 AllStrings
0 1 2 3 4
This is the - not recommended - minimal query:
SELECT #xml.value(N'(//*:Val2)[1]','bit') AS D1P1_Val2
,#xml.query(N'data(//*:string)').value(N'.',N'nvarchar(max)') AS AllStrings;
Try something like this (can't test it myself ) :
SELECT Instructions.query('
declare namespace d1p1="http://XXXXXX";
declare namespace d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays";
concat(//d1p1:Val2, " ", //d2p1:string[1]);
')
I think you just have to elaborate a bit on this

MSSQL Parsing of XML that Contains same Sibling

I am trying to query a MSSQL database that has XML and parse it out.
here is the data:
<Root>
<Relatives>
<Relative>
<Relation>Father</Relation>
<BcAge>50</BcAge>
<BilatAge>0</BilatAge>
<OcAge>0</OcAge>
</Relative>
<Relative>
<Relation>Mother</Relation>
<BcAge>58</BcAge>
<BilatAge>0</BilatAge>
<OcAge>0</OcAge>
</Relative>
Here is the desired result
Relation BcAge BilatAge OcAge Relation BcAge BilatAge OcAge
Father 50 0 0 Mother 58 0 0
I doubt, that you really want to get your result side-by-side... This is no valid resultset, your column names are duplicated.
If you really need it this way you might do this:
DECLARE #xml XML=
N'<Root>
<Relatives>
<Relative>
<Relation>Father</Relation>
<BcAge>50</BcAge>
<BilatAge>0</BilatAge>
<OcAge>0</OcAge>
</Relative>
<Relative>
<Relation>Mother</Relation>
<BcAge>58</BcAge>
<BilatAge>0</BilatAge>
<OcAge>0</OcAge>
</Relative>
</Relatives>
</Root>';
The query will use a CTE to get the Father and the Mother with an XQuery-predicate
WITH Parents AS
(
SELECT #xml.query(N'/Root/Relatives/Relative[(Relation/text())[1]="Father"]') AS Father
,#xml.query(N'/Root/Relatives/Relative[(Relation/text())[1]="Mother"]') AS Mother
)
SELECT 'Father' AS F_Relation
,Father.value(N'(/Relative/BcAge)[1]',N'int') AS F_BcAge
,Father.value(N'(/Relative/BilatAge)[1]',N'int') AS F_BilatAge
,Father.value(N'(/Relative/OcAge)[1]',N'int') AS F_OcAge
,'Mother' AS M_Relation
,Mother.value(N'(/Relative/BcAge)[1]',N'int') AS M_BcAge
,Mother.value(N'(/Relative/BilatAge)[1]',N'int') AS M_BilatAge
,Mother.value(N'(/Relative/OcAge)[1]',N'int') AS M_OcAge
FROM Parents;
But probably it's this you are looking for (any count of <Relative>):
SELECT rel.value(N'(Relation/text())[1]',N'nvarchar(max)') AS Relation
,rel.value(N'(BcAge/text())[1]',N'int') AS BcAge
,rel.value(N'(BilatAge/text())[1]',N'int') AS BilatAge
,rel.value(N'(OcAge/text())[1]',N'int') AS OcAge
FROM #xml.nodes(N'/Root/Relatives/Relative') AS A(rel)

Resources