Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

SQL Server - Get Data in XML Format Based on Group by Columns in SQL Server with Example

Oct 19, 2014
Introduction:

Here in
sql server I will explain how to get data in xml format based on group by columns in sql server with example or sql server group columns and show in xml format.
Description:
In previous articles I explained cursor example in sql server, While loop example in sql server, nested while loop example in sql server, get only month and year from date in sql server, substring function in SQL server and many articles relating to SQL server. Now I will explain cursor how to get data in xml format based on group by columns in sql server with example.

I have a one table that contains data like as shown below


Now I want get data in xml format based on group by ID from above table like as shown below

get data in xml format based on group by columns in sql server with example
To display data in xml format based on group by Id in sql server we need to write the query like as shown below

Syntax to Create Cursor


DECLARE @T TABLE
(
ID INT,
Name VARCHAR(30)
)
INSERT INTO @T VALUES
(1, 'Functional Brochures'),
(1,   'Fliers'),
(2, 'Data Sheets'),
(2, 'Catalogs'),
(5, 'User Guides')
SELECT T1.Id AS '@Name',
(
SELECT T2.Name AS '@Name'
FROM @T AS T2
WHERE T2.Id = T1.Id
FOR XML PATH('Subject'), TYPE
)
FROM @T AS T1
GROUP BY T1.Id
FOR XML PATH('Id'), ROOT('SubjectDetails')
Output:

When we run above query we will get output like as shown below

get data in xml format based on group by columns in sql server with example

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.