Hi All,
I Have A Query that returns rows like the picture below...
Now I want an xml like this - <Employees>
-
<Group GroupId="8">
-
<Employee>Manju</Employee>
-
<Employee>Sudip</Employee>
-
</Group>
-
<Group GroupId="12">
-
<Employee>Raja</Employee>
-
<Employee>Shambo</Employee>
-
......
-
</Group>
-
.......
-
</Employees>
from the query.
How do i do it? Or If Its Really Possible In SQL Server? 'Coz I read somewhere you can't use group by in select when using FOR XML Command.
Pls help. Thanks :)
4 1644
Hi sanndeb
There are a few possible ways of doing this. Here is what I would do:
1. Get the unique Group IDs into a temporary table
2. Use this temporary table with the original table to generate the XML.
Here is the code I wrote to create the XML:
-- Create a temp table with the Group Ids
SELECT DISTINCT GroupId
INTO #tmpGroups
FROM #EmployeeTable
-- Generate the XML
SELECT #tmpGroups.GroupId as '@GroupId',
(SELECT EmpName
FROM #EmployeeTable
WHERE #tmpGroups.GroupId = #EmployeeTable.GroupId
FOR XML PATH(''), TYPE)
FROM #tmpGroups
FOR XML PATH('Group'), ROOT('Employees')
DROP TABLE #tmpGroups
That should do the trick!
Hope this helps.
Brad Orders
@Brad Orders
That Was Perfect :)
Many Many Thanks... But Can It Be Done Without The Temp Table.
Sorry, used a sample that I used on the other post :) But the concept should be the same...
Try this: -
-
declare @TestData Table
-
(
-
STUDENT_ID VARCHAR(6),
-
grade Decimal(2,1)
-
)
-
-
insert into @TestData (student_id, grade)
-
select 'abc123', 0.2 UNION
-
select 'abc123', 0.7 UNION
-
select 'abc123', 0.4 UNION
-
select 'def123', 0.1 UNION
-
select 'def123', 0.5 UNION
-
select 'def123', 0.4
-
-
;with tag1
-
as
-
(
-
select distinct student_id
-
from @testdata
-
)
-
select
-
1 as tag,
-
0 as parent,
-
student_id as [Students!1!StudentID],
-
null as [StudentGrade!2!Grades]
-
from tag1
-
union all
-
select
-
2 as tag,
-
1 as parent,
-
t1.student_id as [Students!1!StudentID],
-
grade as [StudentGrade!2!Grades]
-
from tag1 t1
-
left join @testdata t2 on t1.student_id = t2.student_id
-
order by [Students!1!StudentID], [StudentGrade!2!Grades]
-
for xml explicit
-
-
Happy Coding!!!
~~ CK
@ck9663
Yep got the concept, will try it on my db and let you know. thanks for your effort. :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jos van der Velden |
last post by:
Hi,
I'm trying to find out if we can use bulkcopy via odbc in sql server 2005.
With sql server 200 we could use the odbcbcp.dll.
I can not find any info regarding this for ms sql server 2005.
...
|
by: Peter Nurse |
last post by:
I have just upgraded to SQL Server 2005 from SQL Server 2000.
In Microsoft SQL Server Management Studio, when I click on database
properties, I receive the following error:-
Cannot resolve the...
|
by: Henrik Dahl |
last post by:
Hello!
I have an xml schema which has a date typed attribute. I have used xsd.exe
to create a class library for XmlSerializer. The result of
XmlSerializer.Serialize(...) should be passed as the...
|
by: Bergs |
last post by:
I cannot block user access to tables. I have restriced accounts at the
server, database, group, schema, and table lavel and can still open
tables right up.
In other words, I have absolutely no...
|
by: Nishant Saini |
last post by:
Dear All,
We have a database which contains many tables which have millions of
records. When We attach the database with MS SQL Server 2005 Standard
Edition Server and run some queries (having...
|
by: aleemkhan |
last post by:
I am using SQL Server 2005 Local Reports for a web based project. The
problem is that the ASP.NET default Web Report Viewer does not have
print facility for Local Reports. When the report is...
|
by: m19peters |
last post by:
We have a script that I had to rework a little bit for 2005 that does
a full backup for every database on the server... For some reason on
some nights the script does not backup all databases......
|
by: Omar Abid |
last post by:
Hi every body,
Im using VB 2005 to create a program that open SQL Data base
The problem that i want to detect the tables of a database
so how can i know a data base tables instantly
Thank you...
|
by: Dimitri Furman |
last post by:
SQL Server 2005 SP2 (build 3054)
Consider the following scenario:
- A complex multi-statement table valued function is created. Let's call
it dbo.tfFunc(@Param1, @Param2)
- A SELECT statement...
|
by: pugalenthi |
last post by:
I need to connect to sql server 2005 from c# program, so that i can store the output of the c# code into the database.
Firstly i should get connected to the sql server, secondly i should create a...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |