Results 1 to 3 of 3

Thread: SQL Server 2000 Cursor Problem

  1. #1

    Thread Starter
    Addicted Member hyousuf2's Avatar
    Join Date
    Dec 2004
    Location
    Dublin
    Posts
    226

    SQL Server 2000 Cursor Problem

    hello guys, i know this not the right forum but i thought there are quite a good number of SQL Server 2000 gurus as well, to help me out. i have the following piece of Code in T-SQL.
    Code:
    DECLARE @strIMEIColl VarChar(8000)
    DECLARE @strIMEI VarChar(15)
    
    DECLARE curMatched CURSOR
    FOR 
    SELECT DISTINCT(IMEI) FROM IC_Data
    OPEN curMatched
    
    FETCH NEXT FROM curMatched INTO @strIMEI
    SELECT @strIMEIColl =  CHAR(39) + @strIMEI + Char(39)
    
    FETCH NEXT FROM curMatched INTO @strIMEI
    	WHILE @@FETCH_STATUS = 0 
    	BEGIN
    		select @strIMEIColl = @strIMEIColl + ',' + CHAR(39) + @strIMEI + Char(39)			
    		FETCH NEXT FROM curMatched INTO	@strIMEI
    	END
    
    WHILE @strIMEI = '' BEGIN
    	SELECT @strIMEIColl = @strIMEIColl + ',' + @strIMEI	
    	FETCH NEXT FROM curMatched INTO @strIMEI
    END
    
    CLOSE curMatched
    DEALLOCATE curMatched
    PRINT @strIMEIColl
    SELECT * FROM IC_Data WHERE IMEI IN(@strIMEIColl)
    The variable @strIMEIColl has more than 20 Comma Seperated list of IMEI numbers, but still the Select statement wont show any thing. The PRINT @strIMEIColl statement shows the values correctly.
    I tried using
    Please tell me where am i getting wrong, or is there an alternative to it also ?!?!
    Last edited by Hack; Aug 24th, 2006 at 06:37 AM. Reason: Added [code] [/code] tags for more clarity.
    Women ...r like tea bags, you neva know how strong they really r untill u put them in hot water

    Huzefa Yousuf
    Software Engineer
    Verticity Inc.
    +92-345-2235303

    [email protected]

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SQL Server 2000 Cursor Problem

    Moved to Database section

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server 2000 Cursor Problem

    IN () simply does not work that way.

    IN ('A','B','C','D') works - those are hardwired values.

    You should be building a temp table or table variable with each item you want selected as a separate row.

    Then use

    WHERE... EXISTS IN (SELECT IMEI FROM #TEMPTABLE)

    or even better

    FROM #TEMPTABLE TX
    LEFT JOIN OTHERTABLES OX ON OX.IMEI=TX.IMEI

    btw - avoid the CURSOR - use a loop.

    Here's a thread that talks about this very issue

    http://www.vbforums.com/showthread.p...ghlight=exists

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width