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.
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
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
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. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.