|
-
Aug 24th, 2006, 04:27 AM
#1
Thread Starter
Addicted Member
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]
-
Aug 24th, 2006, 06:38 AM
#2
Re: SQL Server 2000 Cursor Problem
Moved to Database section
-
Aug 24th, 2006, 07:19 AM
#3
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|