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 1303
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: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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?
| |