Click here to Skip to main content
13,052,351 members (55,434 online)
Rate this:
Please Sign up or sign in to vote.
Hi everybody,
I have two tables Table -1 and Table -2 from which I should get resultant table as shown below

Table -1

UserID | Name

1 | X
2 | Y

Table -2

UserID | RoleId

1 | 1
1 | 2
1 | 3
2 | 2
2 | 3

The resultant table from the procedure should be like

UserID | Name | RoleId

1 | X | 1,2,3
2 | Y | 2,3

It is an urgent requirement for me. Please post your answers as soon as possible. Thanks in advance

Posted 22-Mar-13 21:17pm
phil.o 23-Mar-13 3:58am
Reported as not a question - This is not a code-ordering forum ; you have to make an effort about the problem which is presented to you
Homework has been assigned to you so you can learn what you have to. It's silly to ask someone else to do it for you without trying to think about the problem.
Moreover, reporting something as urgent is considered as rude on this forum. It won't make things go faster.
Srivikas 23-Mar-13 15:48pm
Its ok.. I solved it myself. I posted it when I was in a dilemma thinking of the solution.Thank u

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Here is your Store Procedure :

create procedure Example1
SELECT B.userid ,C.Name,
     Roleid = STUFF ( ( SELECT ','+A.Roleid FROM Tbl2 A
     WHERE A.userid = B.userid ORDER BY A.userid
     FOR XML PATH(''),TYPE ).value('.','VARCHAR(MAX)')
     , 1,1,SPACE(0)) FROM Tbl2 B, Tbl1 C
     where B.userid=C.userid GROUP BY B.userid ,

--Exec Example1

Output :

userid	Name	Roleid
1	X	1,2,3
2	Y	2,3
Srivikas 26-Mar-13 4:13am
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100