473,427 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,427 software developers and data experts.

Regarding FOR XML Output In SQL Server 2005

33
Hi All,

I Have A Query that returns rows like the picture below...

Now I want an xml like this

Expand|Select|Wrap|Line Numbers
  1. <Employees>
  2.   <Group GroupId="8">
  3.     <Employee>Manju</Employee>
  4.     <Employee>Sudip</Employee>
  5.   </Group>
  6.   <Group GroupId="12">
  7.     <Employee>Raja</Employee>
  8.     <Employee>Shambo</Employee>
  9.     ......
  10.   </Group>
  11.   .......
  12. </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 :)
Attached Images
File Type: jpg untitled.jpg (12.6 KB, 120 views)
Jun 30 '10 #1
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
Jun 30 '10 #2
sanndeb
33
@Brad Orders
That Was Perfect :)

Many Many Thanks... But Can It Be Done Without The Temp Table.
Jun 30 '10 #3
ck9663
2,878 Expert 2GB
Sorry, used a sample that I used on the other post :) But the concept should be the same...

Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. declare @TestData Table
  3. (
  4. STUDENT_ID VARCHAR(6),
  5. grade Decimal(2,1)
  6. )
  7.  
  8. insert into @TestData (student_id, grade)
  9. select 'abc123', 0.2 UNION
  10. select 'abc123', 0.7 UNION
  11. select 'abc123', 0.4 UNION
  12. select 'def123', 0.1 UNION
  13. select 'def123', 0.5 UNION
  14. select 'def123', 0.4
  15.  
  16. ;with tag1
  17. as
  18. (
  19.    select distinct student_id 
  20.    from @testdata
  21. )
  22. select 
  23.    1 as tag,
  24.    0 as parent,
  25.    student_id as [Students!1!StudentID],
  26.    null as [StudentGrade!2!Grades]
  27. from tag1
  28. union all
  29. select 
  30.    2 as tag,
  31.    1 as parent,
  32.    t1.student_id as [Students!1!StudentID],
  33.    grade as [StudentGrade!2!Grades]
  34. from tag1 t1 
  35.    left join @testdata t2 on t1.student_id = t2.student_id
  36. order by [Students!1!StudentID], [StudentGrade!2!Grades]
  37. for xml explicit
  38.  
  39.  

Happy Coding!!!

~~ CK
Jun 30 '10 #4
sanndeb
33
@ck9663
Yep got the concept, will try it on my db and let you know. thanks for your effort. :)
Jul 1 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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. ...
21
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...
10
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...
1
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...
23
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...
0
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...
2
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......
16
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...
11
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...
4
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.