Solved

DELETE WHERE CURRENT OF with SQL2000

Posted on 2002-06-14
5
393 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
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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
triggered use of sp_send_dbmail failure 2 22
SQL Server Question 5 25
T-SQL: Nested CASE Statements 4 24
SSRS troubles 4 12
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

911 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

18 Experts available now in Live!

Get 1:1 Help Now