Trying to figure out why this snippet does not work.
DECLARE #changeStatus XML, #changeSet XML
SELECT #changeSet = TOP 1 ChangeSet
FROM MyTable
SET #changeStatus = '<change id="' + CAST(NEWID() AS NVARCHAR(36)) + '" by="' + #authorizingUserName + '" byAccountId="' + CAST(#authorizingUserId AS NVARCHAR(36)) + '" when="' + CONVERT(NVARCHAR(50),GETUTCDATE(),127) + 'Z">'
+ '<property id="Status" name="Status" old="' + #status + '" new="Closed" />'
+ '<collections />'
+ '</change>'
-- THIS DOES NOT ERROR OUT AND DOES NOT DO ANYTHING!!
SET #changeSet.modify('insert sql:variable("#changeStatus") as last into (/changes)[1]')
The overall structure of the XML is:
<changes>
<change id="" by="" byAccountId="" when="">
<property />
<collections />
</change>
</changes>
When I run the script and check the #changeSet before and after processing, they are identical. i.e.: The #changeStatus was never added to the XML contained in #changeSet.
If the original was:
<changes>
<change id="change01" by="" byAccountId="" when="">
<property ... />
<collections />
</change>
<change id="change02" by="" byAccountId="" when="">
<property ... />
<collections />
</change>
</changes>
I expected to see:
<changes>
<change id="change01" by="" byAccountId="" when="">
<property ... />
<collections />
</change>
<change id="change02" by="" byAccountId="" when="">
<property ... />
<collections />
</change>
<change id="82ECB3C5-D3BA-4CD2-B62C-89C083E4BAA1" by="me#mydomain.com" byAccountId="1E910737-D78C-E711-9C04-00090FFE0001" when="2018-01-17T00:12:33.700Z">
<property id="Status" name="Status" old="In Review" new="Closed" />
<collections />
</change>
</changes>
Does anyone see what might be wrong?
You should never create an XML on string level!
This has various side-effects.
If one of the variables is NULL the whole string will be NULL
Try SELECT '<x>' + NULL + '</x>';
XML is not just text with some fancy extras. Using T-SQL's FOR XML will - for sure! - create valid XML. Now imagine one of your string contains forbidden characters...
Try SELECT '<x>' + 'The arrow "->" might appear in normal text' + '</x>';
And try to cast it to XML
Many values are not stored in readable format but in some binary format. Depending on your system's settings the conversion of such types (numbers, date and time, BLOBs) will vary. Using FOR XML will use - for sure! - the correct format in and out. You can write anything within your XML, but on another system it might fail to read it.
Try it this way:
DECLARE #changeSet XML = N'<changes/>';
DECLARE #changeStatus XML;
SET #changeStatus=
(
SELECT NEWID() AS [#id]
,'SomeAuthor' AS [#by]
,'SomeAuthorId' AS [#byAccountId]
,GETUTCDATE() AS [#when]
,'Status' AS [property/#id]
,'Status' AS [property/#name]
,'In Review' AS [property/#old]
,'Closed' AS [property/#new]
,'' AS [collections]
FOR XML PATH('change')
);
--your code works fine
SET #changeSet.modify(N'insert sql:variable("#changeStatus") as last into (/changes)[1]');
--you see the first change integrated
SELECT #changeSet;
--Now let us create one more element
SET #changeStatus=
(
SELECT NEWID() AS [#id]
,'Another' AS [#by]
,'OneMore' AS [#byAccountId]
,GETUTCDATE() AS [#when]
,'Status' AS [property/#id]
,'Status' AS [property/#name]
,'In Review' AS [property/#old]
,'Closed' AS [property/#new]
,'' AS [collections]
FOR XML PATH('change')
);
--and insert it
SET #changeSet.modify(N'insert sql:variable("#changeStatus") as last into (/changes)[1]');
--yeah, worked!
SELECT #changeSet;
The query you posted works in general. I ran following test:
http://rextester.com/RNX55555
However, if any of the injected parameters is null, the change will not be inserted and no error will be thrown as you try to insert NULL aka nothing.
Related
This XML comes from public treasury. SQL Server seems to think it is not XML? My result says the XLT method can only be involved with column type XML. Is this result set from the treasury really XML?
How can I parse it into a SQL table?
treasury daily market yield
DECLARE #XML NVARCHAR(MAX)
SELECT #XML =
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<feed xml:base="http://data.treasury.gov/Feed.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">DailyTreasuryYieldCurveRateData</title>
<id>http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData</id>
<updated>2020-07-14T05:40:26Z</updated>
<link rel="self" title="DailyTreasuryYieldCurveRateData" href="DailyTreasuryYieldCurveRateData"/>
<entry>
<id>http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(7633)</id>
<title type="text"/>
<updated>2020-07-14T05:40:26Z</updated>
<author>
<name/>
</author>
<link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(7633)"/>
<category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">7633</d:Id>
<d:NEW_DATE m:type="Edm.DateTime">2020-07-01T00:00:00</d:NEW_DATE>
<d:BC_1MONTH m:type="Edm.Double">0.12</d:BC_1MONTH>
<d:BC_2MONTH m:type="Edm.Double">0.12</d:BC_2MONTH>
<d:BC_3MONTH m:type="Edm.Double">0.14</d:BC_3MONTH>
<d:BC_6MONTH m:type="Edm.Double">0.17</d:BC_6MONTH>
<d:BC_1YEAR m:type="Edm.Double">0.16</d:BC_1YEAR>
<d:BC_2YEAR m:type="Edm.Double">0.17</d:BC_2YEAR>
<d:BC_3YEAR m:type="Edm.Double">0.19</d:BC_3YEAR>
<d:BC_5YEAR m:type="Edm.Double">0.31</d:BC_5YEAR>
<d:BC_7YEAR m:type="Edm.Double">0.52</d:BC_7YEAR>
<d:BC_10YEAR m:type="Edm.Double">0.69</d:BC_10YEAR>
<d:BC_20YEAR m:type="Edm.Double">1.2</d:BC_20YEAR>
<d:BC_30YEAR m:type="Edm.Double">1.43</d:BC_30YEAR>
<d:BC_30YEARDISPLAY m:type="Edm.Double">1.43</d:BC_30YEARDISPLAY>
</m:properties>
</content>
</entry>
</feed>'
SELECT b.value('#d:Id', 'varchar(28)') as d_id
,b.value('#d:BC_30YEAR', 'double') as d_BC_30YEAR
FROM #XML.nodes('feed/entry/content type/m:properties/') as a(b)
First, define your #XML to be of type XML:
DECLARE #XML XML;
Then: you need to define the relevant XML namespaces involved!
Try this snippet of code:
-- define XML namespaces!
;WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2005/Atom',
'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata' AS m,
'http://schemas.microsoft.com/ado/2007/08/dataservices' AS d)
SELECT
b.value('(d:Id)[1]', 'varchar(28)') as d_id,
b.value('(d:BC_30YEAR)[1]', 'decimal(20,2)') as d_BC_30YEAR
FROM
#XML.nodes('feed/entry/content/m:properties') as a(b)
This returns the following value:
Fixes I made:
FROM #XML.nodes('feed/entry/content type/m:properties/') as a(b)
^^^^^^^^^^^^
Wrong node name - the node is <content> (not "content type") - the type is just an attribute on the node, no relevant in this XPath here.
SELECT b.value('#d:Id', 'varchar(28)') as d_id
^^^^^^
You really want to select the XML element (node) d:Id - not an attribute - the # in #d:Id denotes an attribute!
b.value('#d:BC_30YEAR', 'double') as d_BC_30YEAR
^^^^^^^^^
You need to use a proper T-SQL datatype - like decimal(20,2) - not "double"
here....
I have this XML stored in a record, how can I get a value of a specific ID?
<Attributes>
<CustomerAttribute ID="4">
<CustomerAttributeValue>
<Value>1</Value>
</CustomerAttributeValue>
</CustomerAttribute>
<CustomerAttribute **ID="5"**>
<CustomerAttributeValue>
<Value>**aaaaa**</Value>
</CustomerAttributeValue>
</CustomerAttribute>
Assuming the ** surrounding ID="5" and "aaaaa" denotes the nodes you want to select, below is one method.
SELECT XmlColumn.value('(/Attributes/CustomerAttribute[#ID="5"]/CustomerAttributeValue/Value)[1]', 'varchar(100)')
FROM dbo.xml
WHERE XmlColumn.exist('/Attributes/CustomerAttribute[#ID="5"]') = 1;
This assumes the actual XML value is like:
<Attributes>
<CustomerAttribute ID="4">
<CustomerAttributeValue>
<Value>1</Value>
</CustomerAttributeValue>
</CustomerAttribute>
<CustomerAttribute ID="5">
<CustomerAttributeValue>
<Value>aaaaa</Value>
</CustomerAttributeValue>
</CustomerAttribute>
</Attributes>
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
I got a question about replacing XML columns, here is an small example about what I like to change.
<i><recipes n="0" />
<CGrecipes cg="4" r0="302053" r1="302084" r2="302049" r3="302068" />
<HArecipes ha="4" r0="302103" r1="302083" r2="302050" r3="302087" />
<KHrecipes kh="10" r0="302100" r1="302090" r2="302078" r3="302074"
r4="302094" r5="302082" r6="302066" r7="302051"
r8="302086" r9="302070" />
<KHNrecipes khn="10" r0="302102" r1="302089" r2="302056" r3="302077"
r4="302052" r5="302069" r6="302081" r7="302073"
r8="302093" r9="302085" />
<IMITARrecipes imitar="2" r0="302110" r1="302057" />
<MAUSERrecipes mauser="1" r0="302106" />
<SVDrecipes svd="1" r0="302059" />
<BLASERrecipes blaser="2" r0="302105" r1="302060" />
<SIGSAUERrecipes sigsauer="2" r0="302109" r1="302061" />
<HONEYBADGERrecipes honeybadger="1" r0="302062" />
<SMALLBACKPACKrecipes smallbackpack="1" r0="302095" />
<MEDIUMBACKPACKrecipes mediumbackpack="1" r0="302096" />
<MILITARYBACKPACKrecipes militarybackpack="1" r0="302097" />
<LARGEBACKPACKrecipes largebackpack="1" r0="302098" />
<TEDDYBACKPACKrecipes teddybackpack="1" r0="302099" />
<ALICEBACKPACKrecipes alicebackpack="1" r0="302112" />
<M82recipes m82="1" r0="302107" />
<AWMrecipes awm="1" r0="302108" />
<B93Rrecipes b93r="1" r0="302111" />
</i>
And I want to change it with a script to:
<i><recipes r="0" r0="302053" r1="302084" r2="302049" r3="302068" r4="302103" r5="302083" r6="302050" r7="302087" r8="302100" r9="302090" r10="302078" r11="302074" r12="302094" r13="302082" r14="302066" r15="302051" r16="302086" r17="302070" r18="302102" r19="302089" r20="302056" r21="302077" r22="302052" r23="302069" r24="302081" r25="302073" r26="302093" r27="302085" r28="302110" r29="302057" r30="302106" r31="302059" r32="302105" r33="302060" r34="302109" r35="302061" r36="302062" r37="302095" r38="302096" r39="302097" r40="302098" r41="302099" r42="302112" r43="302107" r44="302108" r45="302111" /></i>
I would like to get help and suggestions!
Well, it couldn't be simpler (literally, I think), thanks to T-SQL's very limited implementation of XQuery, and the general hate for dynamic XML of any kind. Let #xml contain your XML in a variable (if it's a column, add a FROM as required).
SELECT CONVERT(XML, REPLACE((
SELECT #xml.value('/i[1]/recipes[1]/#n', 'int') AS [#r], '' AS [#marker]
FOR XML PATH('recipes'), ROOT('i')
), 'marker=""', (
SELECT ' ' + REPLACE(REPLACE('r#="$v"',
'#', ROW_NUMBER() OVER (ORDER BY (SELECT 1)) - 1),
'$v', t.value('text()[1]', 'int'))
FROM (
SELECT #xml.query('
for $a in //*/#*[substring(local-name(),1,1)="r"]
return <r>{string($a)}</r>
') AS a
) _ CROSS APPLY a.nodes('r') AS x(t)
FOR XML PATH('')
)))
From inner to outer: we unpack all the r* attributes into elements, attach a row number to them, then fold the result back into XML by lamely concatenating strings. For a finale, we transform the n attribute of recipes into r and substitute our string concatenation into the outer element.
Why is this code so terrible? Because the data model is terrible (well, and because SQL Server's implementation of XQuery is quite limited, omitting most advanced features that could simplify this). It's an abuse of XML in every way. Consider changing the attributes into child elements. Don't use concatenated element names like ALICEBACKPACKrecipes, generalize this to recipes name='ALICEBACKPACK' or suchlike. Think static names and repeating content:
<i>
<recipes name="" value="0"></recipes>
<recipes name="cg" value="4">
<r>302053</r>
<r>302084</r>
...
</recipes>
...
<recipes name="ALICEBACKPACK" value="1">
<r>302112</r>
</recipes>
...
</i>
This is far easier to query and process for anything that isn't a fully fledged programming language.
I'm trying to query some XML data that I was sent. I have followed various examples, but can't even address any of the elements beneath the root. I'm not sure what I'm missing. I've tried simply pulling the xml beneath /KitchenSupply/StoreInfo and nothing. Is the namespace the culprit for me not being able to return anything?
declare #myxmlinv as xml =
'<?xml version="1.0" encoding="utf-8"?>
<KitchenSupply xmlns="http://www.starstandards.org/STAR" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="KitchenSupply.xsd">
<StoreInfo>
<RCPT>
<SubTaskCode>ZZZ</SubTaskCode>
<SubComponentCode>BS1</SubComponentCode>
<StoreNumber>2241</StoreNumber>
<USRegionNumber>SE</USRegionNumber>
</RCPT>
<SentDateTime>02/04/2015</SentDateTime>
<IntId>ABC1234587</IntId>
<Destinations>
<DestinationFormatType>KS_XML_v3</DestinationFormatType>
</Destinations>
</StoreInfo>
<KitchenOrder>
<KsRecord SalesTransactionPayType="CC" SalesTransactionType="Closed">
<ReceiptAmt RcptTotalAmt="0.00" CustRcptTotalAmt="25.22" CCPostDate="01/07/2015" InvoiceDate="01/05/2015" CustName="JOHN SMITH" CustNo="13998" RcptNo="78476221" />
<KsCosts>
<SaleAmts UnitCost="15.00" TxblAmt="15.00" PayType="Cust" />
<SalesInfo JobStatus="F" JobNo="1" ItemCode="HT093" ItemDesc="Hand Towel">
<EmpInfo EmpRate="16.00" EmpCommPct="1.2" EmpName="DOUG ROGERS" EmpNo="998331" />
</SalesInfo>
</KsCosts>
</KsRecord>
<CustomerRecord>
<ContactInfo LastName="SMITH" FreqFlag="Y">
<Address Zip="90210" State="CA" City="BEV" Addr1="123 MAIN ST" Type="Business" />
<Email MailTo="FAKE#USA.COM" />
<Phone Num="1235551212" Type="H" />
<Phone Num="1235551213" Type="B" />
</ContactInfo>
</CustomerRecord>
</KitchenOrder>
<KitchenOrder>
<KsRecord SalesTransactionPayType="CC" SalesTransactionType="Closed">
<ReceiptAmt RcptTotalAmt="0.00" CustRcptTotalAmt="5.71" CCPostDate="01/08/2015" InvoiceDate="01/07/2015" CustName="SARAH BALDWIN" CustNo="14421" RcptNo="78476242" />
<KsCosts>
<SaleAmts UnitCost="2.00" TxblAmt="2.00" PayType="Cust" />
<SalesInfo JobStatus="F" JobNo="1" ItemCode="HS044" ItemDesc="Hand Soap">
<EmpInfo EmpRate="16.00" EmpCommPct="1.2" EmpName="DOUG ROGERS" EmpNo="998331" />
</SalesInfo>
</KsCosts>
</KsRecord>
<CustomerRecord>
<ContactInfo LastName="BALDWIN" FreqFlag="N">
<Address Zip="90210" State="CA" City="BEV" Addr1="123 VINE ST" Type="Home" />
<Email MailTo="FAKESARAH#USA.COM" />
<Phone Num="1235555512" Type="H" />
<Phone Num="1235556613" Type="M" />
</ContactInfo>
</CustomerRecord>
</KitchenOrder>
</KitchenSupply>';
declare #myxmlinv_table as table (
row_id tinyint,
inv_xml xml
);
insert into #myxmlinv_table(row_id,inv_xml) values('1',#myxmlinv);
Display the XML document and pull the IntId column: (doesn't work)
select i.row_id, i.inv_xml, i.inv_xml.value('(/KitchenSupply/StoreInfo/IntId)[1]','varchar(255)') as data_description
from #myxmlinv_table i
Attempt to use XMLNAMESPACES to display the XML document at the StoreInfo level: (also doesn't work)
WITH XMLNAMESPACES ('http://www.starstandards.org/STAR' as ks)
SELECT
XmlCol.query('/ks:KitchenSupply/StoreInfo')
FROM T;
Ideally, I'd like to use the nodes to extract all the data out and in to separate tables for querying.
KitchenOrders all within one table, CustomerRecord within another, etc.
Any ideas?
You need to use something like this:
-- define the XML namespace as the "default" so you don't have to
-- prefix each and every XPath element with the XML namespace prefix
WITH XMLNAMESPACES( DEFAULT 'http://www.starstandards.org/STAR')
SELECT
-- reach into the <KsRecord> subnode under <KitchenOrder>
ReceiptNo = XC.value('(KsRecord/ReceiptAmt/#RcptNo)[1]', 'int'),
CustName = XC.value('(KsRecord/ReceiptAmt/#CustName)[1]', 'varchar(25)'),
-- reach into the <CustomerRecord> subnode under <KitchenOrder>
CustomerLastName = xc.value('(CustomerRecord/ContactInfo/#LastName)[1]', 'varchar(50)'),
CustomerEmail = xc.value('(CustomerRecord/ContactInfo/Email/#MailTo)[1]', 'varchar(50)')
FROM
-- get a "virtual" table of XML fragments, one for each <KitchenOrder> node
#myxmlinv.nodes('/KitchenSupply/KitchenOrder') AS XT(XC)
Yes, it's the namespace. To get SQL Server to ignore the namespace you can use local-name:
SELECT *
FROM #myxmlinv_table
WHERE inv_xml.exist('//*[local-name()="KitchenSupply"]') = 1