Solved

convert XML path to semicolon delimeted list by joining tables

Posted on 2016-09-13
1
56 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
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.

749 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