Solved

convert XML path to semicolon delimeted list by joining tables

Posted on 2016-09-13
1
43 Views
Last Modified: 2016-09-20
I have the following query

select s.UserRoleId,
STUFF(( select eh.ChildUserRoleID from stgERMHierarchy eh
where eh.UserRoleID = s.UserRoleId
for XML Path('')),1,0,'') as concatenatedText
from StgERMUser s
inner join HubUser h on h.Login = s.NetworkID
inner join stgERMHierarchy eh on eh.UserRoleID = s.UserRoleId
group by s.UserRoleId
order by s.UserRoleId


The resultset that I get is something like this :

userRoleId                    concatenatedText
344321762               <ChildUserRoleID>326</ChildUserRoleID><ChildUserRoleID>343</ChildUserRoleID>
396433216              <ChildUserRoleID>105500128</ChildUserRoleID><ChildUserRoleID>105608599</ChildUserRoleID><ChildUserRoleID>343</ChildUserRoleID>

I want to take the concatenatedText column and read each childuserroleid and get actual login name for that particular userrole and show it in semicolon delimeted.

so it should be something like this

userRoleId                    concatenatedText                                                                                                                                                       login
344321762               <ChildUserRoleID>326</ChildUserRoleID><ChildUserRoleID>343</ChildUserRoleID>                                     ppp;sss  
396433216              <ChildUserRoleID>105500128</ChildUserRoleID>                                                                                                    ttt;yyy;zzz
                                   <ChildUserRoleID>105608599</ChildUserRoleID>          <ChildUserRoleID>343</ChildUserRoleID>

Thanks
0
Comment
Question by:PratikShah111
1 Comment
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 41796688
I cannot test it as I dont have test data.
I assume you have a login table and you can join to that table to get the name.
try something like this.
select s.UserRoleId,
STUFF(( select eh.ChildUserRoleID from stgERMHierarchy eh
where eh.UserRoleID = s.UserRoleId
for XML Path('')),1,0,'') as concatenatedText,
STUFF(( select lt.name from loginTable lt
where eh.ChildUserRoleID = lt.UserRoleID
for XML Path('')),1,0,'') as loginNames
from StgERMUser s
inner join HubUser h on h.Login = s.NetworkID
inner join stgERMHierarchy eh on eh.UserRoleID = s.UserRoleId
group by s.UserRoleId
order by s.UserRoleId

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now