Please enable JavaScript to view this site.

ASQL Reference

Navigation: » No topics above this level «

DELETE’RECORD

Scroll Prev Top Next More

Rewritten March 2019

DELETE’RECORD #file-channel

DELETE'RECORD deletes the last accessed record and all associated keys. Like UPDATE'RECORD, it relies on the record being previously locked, but unlike UPDATE'RECORD, it ignores the value of the recnovar variable and instead always deletes the last accessed record. Also unlike UPDATE'RECORD, it removes the lock on completion of the operation.

All errors trap as ASB errors.

Examples

The typical delete scenario consists of a GET'LOCKED (or GET'NEXT'LOCKED) to read and lock the record to be deleted, followed by the DELETE'RECORD, e.g.

OPEN #ch, filespec, ISAMP'INDEXED, recnovar, fstatvar , wait'record

...

GET'LOCKED #ch, ISAM'KEY(0) = key$, rec

if fstatvar = ISAM_EQ then

    DELETE'RECORD #ch

endif

 

In a more complex scenario, you may want to lock several records, and then—perhaps based on user input or other logic—decide to delete just one of them. This requires re-accessing the record to be deleted, presumably by its unique key, prior to the delete. Note that as long as the record is locked by the first access, the second access will not affect that lock; i.e. it doesn't matter whether the second access uses the 'LOCKED option or not. In this example we lock a group of "items" using GET'NEXT'LOCKED operations, then allow the user to select one to be deleted:

defstruct ST_ITEM            ! record def

    map2 key,s,10            ! item key

    map2 description,s,30

    map2 price,f

endstruct

 

map1 item, ST_ITEM

dimx items(0), ST_ITEM, auto_extend

 

open #ch, filespec, ISAMP'INDEXED, recnovar, fstatvar , wait'record

...

? "Locking n items..."

for i = 1 to n

    GET'NEXT'LOCKED #ch, item

    if fstatvar # ISAM_NF then

        ? "item #";i;" ";item.key;" ";item.description

        items(i) = item      ! save items in array for future reference

    else

        exit    

    endif

next i

 

input "Enter item # to delete (or 0 for none): ",i

if i > 0 then

    GET #ch, ISAM'KEY(0) = items(i).key, item   ! re-access to set context

    if fstatvar = ISAM_EQ then

          DELETE'RECORD #ch 

    endif

endif

 

RELEASE'ALL #ch               ! release all the remaining locked recs

 

Note the critical step in the above example of doing another GET on the target record prior to the DELETE'RECORD, and that it doesn't matter if it was GET or GET'LOCKED since the record was already locked.

See the sample program ISPDEL.BP in EXLIB:[908,033] for a more complete working version of the above scenario.