Solved

DELETE WHERE CURRENT OF with SQL2000

Posted on 2002-06-14
5
392 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
  • 3
  • 2
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
Comment Utility
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
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now