SELECTThiemeItemCode
(1)
SQL Server
(1)
ItemStocks.Item.value
(1)
CREATE TABLE
(1)
LevInfoXML
(1)
Nvarchar
(1)
Varchar
(1)
ThiemeItemCodeis
(1)

I fail to put a XML into a table

Asked By geir
04-Nov-09 05:13 PM
Hi all.
I just cannot get to the item values and hope anyone can help me. Here is the
XML i have got into at one row tabel an XML datatype column.

CREATE TABLE #LevInfoXML (pk int primary key identity, XmlCol xml)




The following 2 examples just give me NULL as values.


SELECT ThiemeItemCode = R.ref.value('@ThiemeItemCode', 'nvarchar(64)')
FROM #LevInfoXML cross apply xmlcol.nodes('//Item') as R(ref)

AND

SELECT ItemStocks.Item.value('@ThiemeItemCode', 'varchar(20)')
FROM
@XML.nodes('/ItemStocks/Item') AS ItemStocks(Item)


This should be really simple, but I just cant get to the items values.



--
Thanks all
Regards Geir

geir wrote:With XQuery/XPath@ThiemeItemCodeis short

Martin Honnen replied to geir
05-Nov-09 07:03 AM
With XQuery/XPath
@ThiemeItemCode
is short for
attribute::ThiemeItemCode
but your 'Item' elements do not have any attributes at all. Instead they
have child elements so you want
child::ThiemeItemCode
or simply
ThiemeItemCode
I think to make the Microsoft SQL Server XQuery implementation of the
'value' function happy you need to add '[1]'
e.g.
R.ref.value('ThiemeItemCode[1]', 'nvarchar(64)')

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

Try this:SELECTThiemeItemCode = R.ref.

Bob replied to geir
05-Nov-09 09:38 AM
Try this:

SELECT
ThiemeItemCode = R.ref.value('ThiemeItemCode[1]', 'nvarchar(64)')
FROM #LevInfoXML cross apply xmlcol.nodes('/ItemStocks/Item') as R(ref)

HTH
wBob
Post Question To EggHeadCafe