Click here to Skip to main content
Rate this: bad
good
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
 
Regards,
Srivikas
Posted 22-Mar-13 22:17pm
Comments
phil.o at 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 at 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
good
Please Sign up or sign in to vote.

Solution 2

Here is your Store Procedure :
 
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
  Permalink  
Comments
Srivikas at 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
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2014
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