I have a table called "Company" -
Company:
-
-
Id Name ParentId Level
-
1 A NULL 0
-
2 B 1 1
-
3 C 1 1
-
4 D 2 2
-
5 E 2 2
-
6 F 3 2
-
7 G 3 2
-
-
-
Here I want to return the list of all chaild companies in XML format.
-
-
The XML format should be like
-
-
<Node Id="1" Name="A">
-
<Node Id="2" Name="B">
-
<Node Id="4" Name="D" />
-
<Node Id="5" Name="E" />
-
</Node>
-
<Node Id="3" Name="C">
-
<Node Id="6" Name="F">
-
<Node Id="7" Name="G">
-
</Node>
-
</Node>
-
-
How can i do this... any help
2 1304
I'm not an expert on XML but this seems to me to be a sorting issue. Sort the records in the correct order and then its simply coverting the fields into a string.
Perhaps something like this will work for you. -
select '<Node Id="' + convert(varchar(6),ID) + '" Name="' + Name +'">'
-
from
-
(select top 100 percent a.ID,a.Name,a.ParentID,a.Level,
-
case when a.Level=0 then 0
-
else case when a.Level=1 then a.id
-
else case when a.Level=2 then a.ParentID
-
else case when a.Level=3 then b.ParentID
-
end end end end as srt,
-
-
case when a.Level<3 then a.ID
-
else case when a.Level=3 then a.ParentID
-
end end as srt2
-
from YourTable a
-
left join YourTable b on b.ID=a.ParentID
-
left join YourTable c on c.ID=b.ParentID
-
order by srt,Srt2,a.ID) a
-
This should handle up to 3 levels deep, I think :)
It did for my limited dataset anyhow.
you will need to add extra joins and Srt columns to handle deeper levels
Hope it helps
I'm not an expert on XML but this seems to me to be a sorting issue. Sort the records in the correct order and then its simply coverting the fields into a string.
Perhaps something like this will work for you. -
select '<Node Id="' + convert(varchar(6),ID) + '" Name="' + Name +'">'
-
from
-
(select top 100 percent a.ID,a.Name,a.ParentID,a.Level,
-
case when a.Level=0 then 0
-
else case when a.Level=1 then a.id
-
else case when a.Level=2 then a.ParentID
-
else case when a.Level=3 then b.ParentID
-
end end end end as srt,
-
-
case when a.Level<3 then a.ID
-
else case when a.Level=3 then a.ParentID
-
end end as srt2
-
from YourTable a
-
left join YourTable b on b.ID=a.ParentID
-
left join YourTable c on c.ID=b.ParentID
-
order by srt,Srt2,a.ID) a
-
This should handle up to 3 levels deep, I think :)
It did for my limited dataset anyhow.
you will need to add extra joins and Srt columns to handle deeper levels
Hope it helps
Hi,
Thanks for the reply.
Here I came across with another solution. I am using a function (calling recursively) to get all the child companies in a tree order. The code I am using is -
-
create table dbo.company
-
( Id smallint,
-
Name varchar(5),
-
ParentId smallint,
-
Level smallint
-
)
-
insert into dbo.company
-
select 1, 'A', null, 0 union all
-
select 2, 'B', 1, 1 union all
-
select 3, 'C', 1, 1 union all
-
select 4, 'D', 2, 2 union all
-
select 5, 'E', 2, 2 union all
-
select 6, 'F', 3, 2 union all
-
select 7, 'G', 3, 2
-
--select * from dbo.company
-
-
go
-
-
-
-
alter function dbo.getSubtree( @Id int, @level int)
-
returns xml
-
begin
-
-
-
-
return
-
( select
-
Id as [@Id],
-
Name as [@Name],
-
case when ParentId is not null
-
then dbo.GetSubtree (id, @level + 1)
-
end
-
from dbo.company
-
where parentId = @Id
-
order by Id
-
for xml path('Node'), type
-
)
-
-
-
-
end
-
-
-
-
go
-
-
-
-
select
-
Id as [@Id],
-
Name as [@Name],
-
dbo.GetSubtree(Id, 1)
-
from dbo.company
-
where parentId is null
-
order by Id
-
for xml path('Node'), type
-
-
Any suggestions please.
Thanks
Sign in to post your reply or Sign up for a free account.
Similar topics
by: John Fabiani |
last post by:
Hi,
I'm a newbie and I'm attempting to learn howto create a select statement.
When I use
>>> string1='18 Tadlock Place'
>>> cursor.execute("SELECT * FROM mytest where address = %s",string1)
All...
|
by: jimh |
last post by:
I'm not a SQL expert. I want to be able to write a stored procedure
that will return 'people who bought this product also bought this...'.
I have a user table that links to a transaction table...
|
by: Tcs |
last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't
loaded it yet. I'm still using MS Access. And no, I don't believe this is an
Access question. (But who knows? I...
|
by: Andrew McNab |
last post by:
Hi folks,
I have a problem with an MS Access SQL query which is being used in an
Access Report, and am wondering if anyone can help.
Basically, my query (shown below) gets some records from a...
|
by: Terencetrent |
last post by:
I having been using Access '97/2002 for about 4 years now and have
never really had the need or the time to learn visual basic. Well, I
think the time has finally come.
I need help with Visual...
|
by: Polly |
last post by:
I had a macro that ran a parameter query and created and opened an
Excel file with the system date as part of the file name, but I had to
change the file name by hand. So I converted the macro to...
|
by: jjturon |
last post by:
Can anyone help me??
I am trying to pass a Select Query variable to a table using Dlookup
and return the value to same select query but to another field.
Ex.
SalesManID ...
|
by: djflow |
last post by:
Hi!
II was wondering if you can help me with SQL query..
Below 7 separated select query works fine(only when they are retrieved separately)
But I want to combined them together and so that i...
|
by: P3Eddie |
last post by:
Hello all!
I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same.
My problem may be a simple find duplicates / do something...
|
by: dizzydangler |
last post by:
Hi, I'm new to access (2007, running on XP), but I've gotten some great help from this site on my project so far. However, the more I know, the more complex the problems seem to become....
I'm...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |