Solved

DELETE WHERE CURRENT OF with SQL2000

Posted on 2002-06-14
5
400 Views
Last Modified: 2012-05-04
Hi,

Can someone please explain to me why this SQL doesn't give me an empty resultset, I receive 299 rows from id=701 and up. And is there a workaround?

I'm using SQL 2000 sp2, I also tried it without sp. But it works with SQL 6.5 and 7.

----------------------------------

SET NOCOUNT ON

CREATE TABLE #mytable (id int NOT NULL,txt varchar(255))

DECLARE @i int
SELECT @i=1
WHILE @i < 1000 BEGIN
  INSERT INTO #mytable VALUES (@i,'AAAAA')
  SELECT @i=@i+1
END

DECLARE @id int

DECLARE mycursor CURSOR
  FOR SELECT id FROM #mytable
  FOR UPDATE

OPEN mycursor
FETCH NEXT FROM mycursor INTO @id

WHILE @@FETCH_STATUS<>-1 BEGIN
  DELETE FROM #mytable WHERE CURRENT OF mycursor
  FETCH NEXT FROM mycursor INTO @id
END

CLOSE mycursor
DEALLOCATE mycursor

SELECT * from #mytable

DROP TABLE #mytable
GO

----------------------------------

Thanks,
pivar
0
Comment
Question by:pivar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 7077556
change the temp table to have a primary key:

CREATE TABLE #mytable (id int NOT NULL,txt varchar(255), primary key(id))

and it will work (again)

CHeers
0
 
LVL 22

Author Comment

by:pivar
ID: 7077565
Thanks,

That did it. Do you know if this is by design or is it a bug? Are MS likely to correct this behaviour?

/pivar
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7077572
I guess this is "by design". In fact, when you start deleting using a cursor, the physical emplacement of the data can change, and thus the WHERE CURRENT OF will actually fail to find the row, but won't say anything because it simply works like a normal where clause internally (-> no matches found).

I don't think they will change this behaviour...

Of course, you shall not use cursors whenever possible, because they are very bad in performance, and you can almost always simulate by using TSQL

CHeers
0
 
LVL 22

Author Comment

by:pivar
ID: 7077587
Thanks,

That did it. Do you know if this is by design or is it a bug? Are MS likely to correct this behaviour?

/pivar
0
 
LVL 22

Author Comment

by:pivar
ID: 7077611
Ooops, it's not wise to refresh the inputpage to see if theres any answers... 8-)

Yes, actually I think you and I had this conversation about cursors before. And I agree with you, but I have this cursor I don't think I can do without. I'm doing the delete if another procedure returns a certain result. This involves several selects and other checks. Also I have to use the same functionality in several different selects, so I don't want to have to update codechanges at several locations. (I've to check r/w rights on a row-by-row basis for several SELECTs and TRIGGERs). If I could do this another way I would gladly do that. This is of course timeconsuming.

/pivar
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question