DB2 query for XML attribute produces: "Error QName=err:XPTY0019" - database

select xmlquery('$MESSAGE/Order/#OrderNo') from yfs_reprocess_error ;
DB2 query for fetching order number attribute from xml (stored in Message column).
While i'm trying to Fetch XML data from the database in DB2 using the below query. Getting error
The result of an intermediate step expression in an XQuery path expression contains an atomic value.
Error QName=err:XPTY0019..

Let me guess your XML structure.
Use the following as an example.
select xmlcast(xmlquery('$MESSAGE/Order/#OrderNo' passing MSG as "MESSAGE") as int)
from (values xmlparse(document '<Order OrderNo="1"/>')) yfs_reprocess_error(MSG);
If your XML document has another structure, then please, provide its example.

Related

How to read XML items or data in a SQL Server stored procedure?

It looks like we are trying to parse XML and certain values? Do we do that always with T.item.value? and T(Item) an alias here? I found reference here https://learn.microsoft.com/en-us/sql/relational-databases/xml/load-xml-data?view=sql-server-2017 but still need clarification.
Need to understand what this code is doing and where can I learn more about writing such code like what is T, T(item), T.item.value here..
CREATE PROCEDURE [dbo].[MyTestSP]
#exServers xml = N'<a />' --sample: N'<a><s>abc.com</s><s u="user#example.com>outlook.com</s></a>'
AS
BEGIN
select
T.item.value('.', 'nvarchar(256)') as ExServer,
T.item.value('#u', 'nvarchar(256)') as Account
from #exServers.nodes('a/s') T(item)
END
GO
You can start by going through the tutorial Stairway to XML. After that all will be clear to you.
what is T?
T is an alias for a table of shredded xml fragments from #exServers.
what is T(item)?
This specifies the name T of the derived table from shredding the nodes and it is giving the column with the XML fragments a name item.
T.item.value
It is a way to extract a value from the xml in column item from the alias T.
nodes() Method (xml Data Type)
value() Method (xml Data Type)

Want to store XML in SQL Server 2014 table and apply select on same

Having data in the for of XML and I want to store that XML in a SQL Server table. I have created a column of datatype xml and tried to store my XML data in that newly created column, but I get an error on INSERT:
The data type 'xml' used in the VALUE method is invalid.
This is the Insert statement:
INSERT INTO UserDeatailtbl(RoleXml)
SELECT Y.ID.value('./RoleXml[1]', 'XML')
FROM #UserDetailsXML.nodes('UserDetails/UserDetailsRow') AS Y(ID)
Try to use the .query() operator instead of .value() - like this:
INSERT INTO UserDetailtbl(RoleXml)
SELECT ID.query('.')
FROM #UserDetailsXML.nodes('UserDetails/UserDetailsRow/RoleXml') AS Y(ID)
Maybe that'll work, since the .query() operator is returning an XML fragment by definition.
If it doesn't work, we'll need to know more about your XML structure - can you show us an example of your XML stored in the #UserDetailsXML variable? Which part of that do you want to insert into UserDetailTbl.RoleXml?

FOR XML AUTO, RAW SQL Server

I am reading for MS 70-461 exam and when taking a look on a previous test I face the following question:
Question:
You need to write a Transact-SQL query that displays all the products received by a singlesupplier in the following XML format:
<Suppliers SupplierID="22" Company="Company Name" ContactNumber="510 250 6400">
<Products ProductID="100" UnitPrice="249.00" UnitsInStock="7" />
<Products ProductID="118" UnitPrice="559.00" UnitsInStock="12" />
</Suppliers>
Correct Answer
SELECT Suppliers.SupplierID,
Suppliers.CompanyName AS [Company],
Suppliers.ContactNumber,
Products.ProductID,
Products.UnitPrice,
Products.UnitsInStock
FROM Suppliers
INNER JOIN Products ON Suppliers.SupplierID = Products.SupplierID
WHERE Suppliers.SupplierID = 22
FOR XML AUTO, RAW
I try to run similar query by using FOR XML AUTO, RAW in SQL server 2012 and SQL Azure and in both cases I receive an error. Is FOR XML AUTO, RAW valid?
How should I write the query in order to return the above XML?
Thank you for your help
Based on MSDN article, RAW and AUTO are two different modes and can not be used together:
In a FOR XML clause, you specify one of these modes:
RAW
AUTO
EXPLICIT
PATH
The RAW mode generates a single element per row in the rowset that is returned by the SELECT statement. You can generate XML hierarchy by writing nested FOR XML queries.
The AUTO mode generates nesting in the resulting XML by using heuristics based on the way the SELECT statement is specified. You have minimal control over the shape of the XML generated. The nested FOR XML queries can be written to generate XML hierarchy beyond the XML shape that is generated by AUTO mode heuristics.
So you should use FOR XML AUTO in order to generate output specified in question.

XQuery is Expected Error but XML already in SQL Table

Using 11.0 SP2 sql server manager.
I am trying to extract the values from the xml but I keep receiving the error 2205 "XQuery was expected". I do not understand why I am receiving that error since the XML is coming from a sql table. It is my understanding that the XQuery is needed to specify a path but if the data is already in the sql table referenced then why is a specific path needed?
Select
XmlData.value('(/ItemInformation Culture/title)[1]','varchar(max)') as Title
From
[ArcDev].[dbo].[XmlRetrieval2]
Msg 2205, Level 16, State 1, Line 3 XQuery
[ArcDev.dbo.XmlRetrieval2.XmlData.value()]: ")" was expected.
The table looks like this:
XmlRetrieval2
You picture does not show the structure of your XML, if this does not solve your problem you should paste a (reduced) example of your XML.
From the picture I take that there is one element "ESRI_ItemInformation" with an attribute "Culture". It might be "ESRI" and "ItemInformation_Culture", but I don't think so...
If "title" is an element one level below the "ESRI_ItemInformation" your query might be this:
Select
XmlData.value('(/ESRI_ItemInformation/title)[1]','varchar(max)') as Title
From
[ArcDev].[dbo].[XmlRetrieval2]

XML input getting truncated

I have an xml doc (size: 3.59 mb) with 3765815 total characters in it. My sql server 2008 database table has a column with xml data type. When I try to insert this xml into the column it seems to truncate it.
I thought xml data type can handle 2GB of data. Is this a correct understanding or am i missing something?
Thanks
Here is the query i am using
declare printxml nvarchar(max)
select printxml=cast(inputxml as varchar(max))
from TableA
where SomeKey='<some key>'
print printxml
Select the data directly instead of printing it to the messages window:
SELECT
inputxml
FROM TableA
WHERE SomeKey = '<somekey>'
The caveat is that you have to set up Management Studio to be able to return all the data to the window. You do that using the following option (the default setting is 2MB):

Resources