I have the following in an xml variable @ResultData
<EntityKey_x005B__x005D_>
<EntityKey>
<KeyData xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey">
<KeyField>
<Field>JournalNum</Field>
<Value>LJRN000071</Value>
</KeyField>
</KeyData>
</EntityKey>
<EntityKey>
<KeyData xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey">
<KeyField>
<Field>JournalNum</Field>
<Value>LJRN000072</Value>
</KeyField>
</KeyData>
</EntityKey>
<EntityKey>
<KeyData xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey">
<KeyField>
<Field>JournalNum</Field>
<Value>LJRN000073</Value>
</KeyField>
</KeyData>
</EntityKey>
<EntityKey>
<KeyData xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey">
<KeyField>
<Field>JournalNum</Field>
<Value>LJRN000074</Value>
</KeyField>
</KeyData>
</EntityKey>
</EntityKey_x005B__x005D_>
But I can't seem to select the JournalNum values from it because of the xmlns=...
on the node. In .Net I can do something like "{http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey}KeyData"
to retrieve it, but I get a syntax error in SQL.
I just want to get a list of the Value nodes, in document order into a temp table and this doesn't work either....
SELECT IDENTITY(int,1,1) as 'ID',
c.query('(KeyData/KeyField/Value)[1]') as 'JournalNum'
INTO #tmpBatches
FROM @ResultData.nodes('//EntityKey') t(c)
Thoughts? Suggestions? Solutions?
You can optionally retrieve formal results of a SQL query as XML by specifying the FOR XML clause in the query. The FOR XML clause can be used in top-level queries and in subqueries. The top-level FOR XML clause can be used only in the SELECT statement.
XML Namespaces - The xmlns Attribute When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".
You can transfer XML data into SQL Server in several ways. For example: If you have your data in an [n]text or image column in a SQL Server database, you can import the table by using Integration Services. Change the column type to XML by using the ALTER TABLE statement.
Got it...of course, right after asking
;WITH XMLNAMESPACES (N'http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey' as DYN)
SELECT IDENTITY(int,1,1)
as 'ID',
c.value('(DYN:KeyData/DYN:KeyField/DYN:Value)[1]', 'VARCHAR(40)')
as 'JournalNum'
INTO #tmpBatches
FROM @ResultData.nodes('//EntityKey') t(c)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With