Re: Recursive Xml Quert
- From: Dan Sullivan <dan@xxxxxxxxxxxxxxx>
- Date: Thu, 15 Jun 2006 16:22:09 +0000 (UTC)
It may not be possible to do the recursion you want, but it is not clear exactly what recursion you want to do. The initializer for your recursive CTE refers to an element that is not in the document.
SELECT VIRT.node.query('./Items') as Items, VIRT.node.value('Key[1]', 'int') as [Key],
VIRT.node.value('Description[1]', 'varchar(max)') as Description,
0 as Level
FROM @xml.nodes('/Item/Item') as VIRT(node)
There is no /Item/Item in the document.
What do you expect as output? Are you trying to get a table of all of the key and descriptions in the document? This would do that...
DECLARE @xdata xml
SET @xdata = '<Root>........</Root>'
SELECT VIRT.node.value('Key[1]', 'int') as [Key],
VIRT.node.value('Description[1]', 'varchar(max)') as Description
FROM @xdata.nodes('
for $i in (//Item)
return $i
') AS VIRT(node)
Dan
I am trying to perform a recursive query in Sql 2005 that essentially
parses an xml string.
When I run the query I get the following error message:
Msg 107, Level 15, State 1, Line 83
The column prefix 'CTE_ITEMS' does not match with a table name or
alias name
used in the query.
Msg 9506, Level 16, State 1, Line 83
The XMLDT method 'nodes' can only be invoked on columns of type xml.
Am I wrong to believe that recursing xml can be done or am I going
about it wrong?
Sample code is below.
Thanks,
David Archuleta
DECLARE @xml xml
SET @xml = cast ('<?xml version="1.0" encoding="utf-8"?>
<Root>
<Item><Key>-1</Key>
<Description>Test -1</Description>
<Items>
<ItemCollection>
<Item>
<Key>0</Key>
<Description>Test 0</Description>
<Items>
<ItemCollection>
<Item><Key>1</Key>
<Description>Test 1</Description>
<Items>
<ItemCollection />
</Items>
</Item>
<Item>
<Key>2</Key>
<Description>Test 2</Description>
<Items>
<ItemCollection />
</Items>
</Item>
<Item>
<Key>3</Key>
<Description>Test 3</Description>
<Items>
<ItemCollection />
</Items>
</Item>
<Item>
<Key>11</Key>
<Description>Test 11</Description>
<Items>
<ItemCollection>
<Item>
<Key>12</Key>
<Description>Test 12</Description>
<Items>
<ItemCollection />
</Items>
</Item>
</ItemCollection>
</Items>
</Item>
</ItemCollection>
</Items>
</Item>
</ItemCollection>
</Items>
</Item>
</Root>' as xml);
/*
--TEST QUERY
SELECT
VIRT.n.query('./Items') as Items,
VIRT.n.value('Key[1]', 'int') as [Key],
VIRT.n.value('Description[1]', 'varchar(max)') as Description
FROM @xml.nodes('//Item') as VIRT(n);
*/
WITH CTE_ITEMS (Items, [Key], Description, Level)
AS
(
SELECT
VIRT.node.query('./Items') as Items,
VIRT.node.value('Key[1]', 'int') as [Key],
VIRT.node.value('Description[1]', 'varchar(max)') as Description,
0 as Level
FROM @xml.nodes('/Item/Item') as VIRT(node)
UNION ALL
SELECT
VIRT.node.query('./Items') as Items,
VIRT.node.value('Key[1]', 'int') as [Key],
VIRT.node.value('Description[1]', 'varchar(max)') as Description,
Level + 1
FROM
CTE_ITEMS.Items.nodes('//ItemCollection') as VIRT(node)
)
SELECT * FROM CTE_ITEMS
.
- References:
- Recursive Xml Quert
- From: David Archuleta
- Recursive Xml Quert
- Prev by Date: Re: ancestor using XQuery in SQL Server 2005
- Next by Date: INSERT child node which contains variable data
- Previous by thread: Recursive Xml Quert
- Next by thread: Re: Recursive Xml Quert
- Index(es):
Relevant Pages
|
Loading