Solved

DELETE WHERE CURRENT OF with SQL2000

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

773 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