#1  
Old 26th November 2019, 04:07 AM
Roberto Nicchi
 
Posts: n/a
Default Deleting/updating records in a loop

I have just realized that if i want to delete/update records (depending
on some logic) in a loop i have to delete records using a second
instance of a table.

Example with two records:

the first record is updated
the first record is deleted

my first attempt has been:

tb.first;
while not(tb.eof) do
begin
if check_for_delete then
begin
tb.delete;
deleted:=true;
end
else begin
update_the_record;
deleted:=false;
end;

if not(deleted) then tb.next;
end;

this doesn't works correctly because after the second record is deleted
EOF is false (the first record became the current record) and so the
first record will be processed again.

the solution i have found is to use a second table to delete.

tb.first;
while not(tb.eof) do
begin
if check_for_delete then
begin
tb1.findkey...; tb1.delete;
end
else update_the_record;

tb.next;
end;

I'm wondering if this is the only solution.

thanks
  #2  
Old 26th November 2019, 07:50 AM
David Rose
 
Posts: n/a
Default Re: Deleting/updating records in a loop

>
> this doesn't works correctly because after the second record is deleted
> EOF is false (the first record became the current record) and so the
> first record will be processed again.
>
>

Use a reverse loop

tb.last;
while not tb.bof do
begin
if something then tb.delete else tb.prior;
end;
  #3  
Old 26th November 2019, 03:31 PM
Thorsten Engler [NDA]
 
Posts: n/a
Default Re: Deleting/updating records in a loop

David Rose wrote:

> >
> > this doesn't works correctly because after the second record is deleted
> > EOF is false (the first record became the current record) and so the first
> > record will be processed again.
> >
> >

> Use a reverse loop
>
> tb.last;
> while not tb.bof do
> begin
> if something then tb.delete else tb.prior;
> end;


Also, make sure the current index is NOT on any field that you are updating (or
you will be in the new position for the updated record after posting).
  #4  
Old 26th November 2019, 08:04 PM
Roberto Nicchi
 
Posts: n/a
Default Re: Deleting/updating records in a loop

Il 26/11/2019 05:31, Thorsten Engler [NDA] ha scritto:
> David Rose wrote:
>
>>>
>>> this doesn't works correctly because after the second record is deleted
>>> EOF is false (the first record became the current record) and so the first
>>> record will be processed again.
>>>
>>>

>> Use a reverse loop
>>
>> tb.last;
>> while not tb.bof do
>> begin
>> if something then tb.delete else tb.prior;
>> end;

>
> Also, make sure the current index is NOT on any field that you are updating (or
> you will be in the new position for the updated record after posting).
>


Thanks

Roberto
  #5  
Old 27th November 2019, 12:52 AM
David Marcus David Marcus is offline
Member
 
Join Date: Oct 2011
Posts: 85
Default

If I need to go forward and there is an ID field that the index is using, I save the ID as SavedID, do the delete, then do

if not Eof and ( ID <= SavedID ) then
Next


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting duplicate records Ad Franse nexusdb.public.support.sql 1 20th June 2012 01:22 PM
Deleting Duplicate Records John Turner nexusdb.public.support.sql 15 10th September 2010 12:42 AM
Deleting slave records gsachs nexusdb.public.support 4 9th March 2009 10:44 PM
OT: Updating ProgressBar in while loop Malcolm Cheyne nexusdb.public.discussions 6 6th March 2007 10:10 PM
Setrange problem deleting records Roberto Nicchi nexusdb.public.support 1 26th August 2003 02:30 AM


All times are GMT +11. The time now is 11:39 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.