using WHILE with SELECT (not working)

Last post 07-06-2009 2:12 AM by eralper. 22 replies.

Sort Posts:

  • Re: using WHILE with SELECT (not working)

    07-03-2009, 3:34 AM
    • Participant
      1,364 point Participant
    • someguy198650
    • Member since 06-02-2008, 2:08 AM
    • Midgard
    • Posts 469

    Hi, try:

    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
    		PRINT @username
    		FETCH NEXT 
    		FROM @getID INTO @ID
    	END
    CLOSE @getID
    DEALLOCATE @getID

    p.s. dont forget to dispose ur temp table:

    drop table #Users
    drop table #UserFriends


     


     

  • Re: using WHILE with SELECT (not working)

    07-03-2009, 3:49 AM

    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

    Avantha Siriwardana
    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)
    http://www.linkedin.com/in/avanthasiriwardana
  • Re: using WHILE with SELECT (not working)

    07-03-2009, 4:02 AM
    • Participant
      1,364 point Participant
    • someguy198650
    • Member since 06-02-2008, 2:08 AM
    • Midgard
    • Posts 469

    AvanthaSiriwardana:
    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

     

    huh???

    try:

    create table #listofusernames(UserName nvarchar(50))
      
    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
            insert into #listofusernames
            select @username   
            FETCH NEXT    
            FROM @getID INTO @ID   
        END  
    CLOSE @getID   
    DEALLOCATE @getID 
    
    select * from #listofusernames
    drop table #listofusernames


     

     

  • Re: using WHILE with SELECT (not working)

    07-03-2009, 12:44 PM
    • All-Star
      30,536 point All-Star
    • Naom
    • Member since 12-31-2007, 7:08 PM
    • Wisconsin
    • Posts 6,781

    In my sample remove the second join with CTE (try with only one JOIN with CTE).

    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
  • Re: using WHILE with SELECT (not working)

    07-03-2009, 12:48 PM
    • All-Star
      30,536 point All-Star
    • Naom
    • Member since 12-31-2007, 7:08 PM
    • Wisconsin
    • Posts 6,781

    I don't understand - why do we need to use CURSOR here at all?


    Isn't it the same as

    with cte_Friends as (select FriendID from UserFriends where UserID = @SomeID)

    select UserName from Users where UserID in (select FriendID from cte_Friends)


    Can you please explain me what I'm missing here?

    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
  • Re: using WHILE with SELECT (not working)

    07-05-2009, 7:00 AM
    • Member
      476 point Member
    • aliusmankhan
    • Member since 07-02-2008, 10:41 AM
    • Posts 396

    and i am not getting any thing out of it


    aliusmankhan
    C#
    .NET 3.5
    Web.Dev
    AJAX
  • Re: using WHILE with SELECT (not working)

    07-05-2009, 1:07 PM
    • All-Star
      30,536 point All-Star
    • Naom
    • Member since 12-31-2007, 7:08 PM
    • Wisconsin
    • Posts 6,781

     Post your tables creation script along with some data - then we can suggest something.

    Looking for a job opportunity.

    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)

    Visit my blog

    PluralSight Learning Library
  • Re: using WHILE with SELECT (not working)

    07-06-2009, 2:12 AM
    • Contributor
      2,216 point Contributor
    • eralper
    • Member since 10-11-2002, 12:26 AM
    • Turkey
    • Posts 339

    Why you are using WHILE ?

     

    This is used for loops, but your query does not require a loop, am I wrong?

     

    An IF statement might solve the problem

     

    IF @id IS NOT NULL

     

     

    Eralper

    T-SQL Programming  for Developers

    http://www.kodyaz.com
    http://www.eralper.com

    Our true mentor in life is science
    Peace At Home, Peace In The World
Page 2 of 2 (23 items) < Previous 1 2