Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 looping users to create a list

Author  Topic 

lmayer4
Starting Member

33 Posts

Posted - 2015-04-07 : 15:32:53
Hello,

I have to take a list from AD and create a comma separated list for our cisco IM program. I really don't want to do this by hand so I thought I could import the list into SQL and manipulate it that way. Problem is I don't know how. The concept sounded easy until I started messing with it.

I have this test table:

create table #userList (UserId varchar(50),GrpList varchar(50))

insert into #userList values('TestUser1','MyGroup')
insert into #userList values('TestUser2','MyGroup')
insert into #userList values('TestUser3','MyGroup')
insert into #userList values('TestUser4','MyGroup')

select *
from #userList


Which gives you this:


UserId GrpList
TestUser1 MyGroup
TestUser2 MyGroup
TestUser3 MyGroup
TestUser4 MyGroup


I need to have a way to make it come out like this. I can't have the contactid show up in the userid column because that would mess with the IM client. Can anyone show me how to do this in a loop?

Thanks in advance.


contactid UserId grplist
TestUser1 TestUser2 MyGroup
TestUser1 TestUser3 MyGroup
TestUser1 TestUser4 MyGroup
TestUser2 TestUser1 MyGroup
TestUser2 TestUser3 MyGroup
TestUser2 TestUser4 MyGroup
TestUser3 TestUser1 MyGroup
TestUser3 TestUser2 MyGroup
TestUser3 TestUser4 MyGroup
TestUser4 TestUser1 MyGroup
TestUser4 TestUser2 MyGroup
TestUser4 TestUser3 MyGroup

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-04-07 : 15:57:06
[code]
select u1.Userid, u2.Userid, u1.GrpList
from #userlist u1
cross join #userlist u2
where u1.Userid <> u2.Userid
order by u1.Userid, u2.Userid, u1.GrpList
[/code]
Go to Top of Page

lmayer4
Starting Member

33 Posts

Posted - 2015-04-07 : 15:58:53
Thank you thank you!

This is perfect!
Go to Top of Page
   

- Advertisement -