?
Solved

convert XML path to semicolon delimeted list by joining tables

Posted on 2016-09-13
1
Medium Priority
?
63 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

718 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