Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
How to use the FOR XML keywords in SQL to concatenate ids Part one of a three-part series of blogs |
---|
If you want to group items in a SQL query, showing a comma-delimited list of ids for each, generating an XML file is not the obvious place to start - but it should be.
|
This week I wanted to take a table like this (I've simplified the problem, but the underlying principles are the same):
This table shows for each person which course numbers they've attended.
And group the courses for each person to show this:
Here the courses are listed for each person.
The answer I found involves using the keywords FOR XML and the STUFF function:
-- show course ids for each person
SELECT
p.PersonName,
STUFF((
SELECT ',' + CAST(innerTable.CourseId AS varchar(10))
FROM tblPerson AS innerTable
WHERE innerTable.PersonName = p.PersonName
FOR XML PATH('')
),1,1,'') AS Ids
FROM
tblPerson AS p
GROUP BY
p.PersonName
It took me ages to decode this, so I decided to see if I could throw light on how it all works (the point of the rest of this blog).
There are more options for controlling the XML output produced by the FOR XML keywords than are covered in this blog, as this Simple Talk article shows.
If you want to try this out yourself, run this SQL script to generate the tblPerson table shown above:
-- create table of people attending courses
CREATE TABLE tblPerson(
PersonId int PRIMARY KEY,
PersonName varchar(8000),
CourseId int
)
-- add 5 people
INSERT INTO tblPerson(
PersonId, PersonName, CourseId
) VALUES (
1, 'Tulisa Bush', 14
)
INSERT INTO tblPerson(
PersonId, PersonName, CourseId
) VALUES (
2, 'George Minogue', 23
)
INSERT INTO tblPerson(
PersonId, PersonName, CourseId
) VALUES (
3, 'George Minogue', 15
)
INSERT INTO tblPerson(
PersonId, PersonName, CourseId
) VALUES (
4, 'Tulisa Bush', 19
)
INSERT INTO tblPerson(
PersonId, PersonName, CourseId
) VALUES (
5, 'George Minogue', 6
)
-- show results
SELECT * FROM tblPerson
Running this script should give you the table shown. Rather than hurrying full tilt towards the final answer, let's break the journey en route to look at what the FOR XML keywords actually do.
You can see more about how we can help you learn to program in SQL on our SQL training home page.
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.