views:

899

answers:

2

I have the following XML:

<Field FieldRowId="1000">
    <Items>
        <Item Name="CODE"/>
        <Item Name="DATE"/>
    </Items>
</Field>

I need to get the FieldRowId using OPENXML. The SQL i have so far:

INSERT INTO @tmpField
      ([name], [fieldRowId])
SELECT [Name], --Need to get row id of the parent node
 FROM OPENXML (@idoc, '/Field/Items/Item', 1)
+2  A: 

EDIT: I added a root node to the xml. and demonstrated grabbing the ID. I'm assuming you have more than one field element in the xml. This is assuming you have the starting XML; are you given an Item and have to traverse upwards?

DECLARE @T varchar(max) 
SET @T = 
'<root>
    <Field FieldRowId="1000">
     <Items>
      <Item Name="CODE"/>
      <Item Name="DATE"/>
     </Items>
    </Field>
    <Field FieldRowId="2000">
        <Items>
      <Item Name="CODE"/>
      <Item Name="DATE"/>
     </Items>
    </Field>
</root>'

DECLARE @X xml

SET @X = CAST(@T as xml)
SELECT Y.ID.value('../../@FieldRowId', 'int') as FieldID, 
       Y.ID.value('@Name', 'varchar(max)') as "Name"
FROM @X.nodes('/root/Field/Items/Item') as Y(ID)
Lurker Indeed
So, If once I have the xml variable, how do I do what I am trying to achieve?
DotnetDude
You can either use it directly as Lurker showed or you can use OPENXML as I talk about http://searchsqlserver.techtarget.com/tip/0,289483,sid87_gci1347994,00.html.
mrdenny
A: 

Using nodes is the way to go. OPENXML takes 1/8 of the SQL server memory every time it is used. Although OPENXML and nodes will generally have the same query performance.

Deepfreezed