Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 XML - Queries

Author  Topic 

axt
Starting Member

5 Posts

Posted - 2009-08-21 : 09:13:29
Hi Folks,

i have the following XML-Structure

<user>
<user_name>user1</user_name>
<tables>
<table>table1</table>
</tables>
<tables>
<table>table1_1</table>
</tables>
</user>
<user>
<user_name>user2</user_name>
<tables>
<table>table2</table>
</tables>
</user>


and i want to get the output:
user1
--> table1
--> table1_1
user2
--> table2


but i get:
user1
--> table1
--> table1_1
--> table2

instead.

I use the following routine with nested loops:
declare user_name_ CURSOR FOR
select Attributes.Attribute.value
('.','varchar(max)') as AttributeValue
from @xml_user_struct.nodes('/root/user/user_name')
Attributes (Attribute);


declare table_ CURSOR FOR
select Attributes.Attribute.value
('.','varchar(max)') as AttributeValue
from @xml_user_struct.nodes('/root/user/tables/table')
Attributes (Attribute);




declare @user_string varchar(100);
declare @table_string varchar(100);

OPEN user_name_
OPEN table_

FETCH NEXT FROM user_name_ INTO @user_string

-- LOOP 1
WHILE @@FETCH_STATUS = 0
BEGIN

PRINT @user_string
FETCH NEXT FROM user_name_ INTO @user_string

FETCH NEXT FROM table_ INTO @table_string
-- LOOP 2
WHILE @@FETCH_STATUS = 0
begin
PRINT '--> ' + @table_string
FETCH NEXT FROM table_ INTO @table_string
end
close table_;
deallocate table_;

END

close user_name_;
deallocate user_name_;


What should i correct on my routine?

Thanks for your help.

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2009-08-21 : 10:51:20
Why do you want to do that? Based on the XML it looks like table2 should be grouped with user2.



An infinite universe is the ultimate cartesian product.
Go to Top of Page

axt
Starting Member

5 Posts

Posted - 2009-08-24 : 02:02:03
I want to do that to use XML as multidimensional array.

I will not describe my endtask, because it does not make sense to discuss it.

But your help to this topic is appreciated.
Go to Top of Page
   

- Advertisement -