create table #UserFriends(FriendId numeric,UserId numeric, [name] nvarchar(100))
insert into #UserFriends
select 1,21,'a' union all
select 2,21,'b' union all
select 3,21,'c' union all
select 4,22,'d' union all
select 5,22,'e' union all
select 6,23,'f' union all
select 7,23,'g'
create table #Users(UserId numeric, UserName nvarchar(100))
insert into #Users
select 1,'a' union all
select 4,'d' union all
select 6,'f' union all
select 7,'g'
select * from #UserFriends
select * from #Users
DECLARE @ID numeric
DECLARE @username NVARCHAR(50)
DECLARE @getID CURSOR
SET @getID = CURSOR FOR SELECT FriendId from #UserFriends where UserId=21
OPEN @getID FETCH NEXT FROM @getID INTO @ID
WHILE @@FETCH_STATUS = 0
BEGIN
--select @username = UserName from #Users where UserId=@id
SET @username = (SELECT UserName from #Users where UserId=@id)
PRINT @username
FETCH NEXT
FROM @getID INTO @ID
END
CLOSE @getID
DEALLOCATE @getID
create table #UserFriends(FriendId numeric,UserId numeric, [name] nvarchar(100))
insert into #UserFriends
select 1,21,'a' union all
select 2,21,'b' union all
select 3,21,'c' union all
select 4,22,'d' union all
select 5,22,'e' union all
select 6,23,'f' union all
select 7,23,'g'
create table #Users(UserId numeric, UserName nvarchar(100))
insert into #Users
select 1,'a' union all
select 4,'d' union all
select 6,'f' union all
select 7,'g'
select * from #UserFriends
select * from #Users
DECLARE @ID numeric
DECLARE @username NVARCHAR(50)
DECLARE @getID CURSOR
SET @getID = CURSOR FOR SELECT FriendId from #UserFriends where UserId=21
OPEN @getID FETCH NEXT FROM @getID INTO @ID
WHILE @@FETCH_STATUS = 0
BEGIN
--select @username = UserName from #Users where UserId=@id
SET @username = (SELECT UserName from #Users where UserId=@id)
PRINT @username
FETCH NEXT
FROM @getID INTO @ID
END
CLOSE @getID
DEALLOCATE @getID