Results 1 to 3 of 3

Thread: SQL Server 2000 Cursor Problem

  1. #1
    Addicted Member hyousuf2's Avatar
    Join Date
    Dec 04
    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

    huzefa.yousuf@verticity.com

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: SQL Server 2000 Cursor Problem

    Moved to Database section
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 04
    Location
    CT
    Posts
    14,406

    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
  •