Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
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

Regards,
Srivikas
Posted
Comments
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

Here is your Store Procedure :

SQL
create procedure Example1
As
begin

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 ,C.name

End

--Exec Example1

Output :

userid	Name	Roleid
1	X	1,2,3
2	Y	2,3
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900