Deleting rows from a table
To delete a row from a database table, you use the following technique:
1. Open a database and issue a SELECT query to retrieve the row you want to delete and send the result table the AbtResultTable>>first message to access the row to be deleted.
2. Send the table the AbtTable>>deleteRow: message with the row returned from the first message.
The SELECT statement you issue to return the row to be deleted cannot contain a join operation. You can use this technique only to delete rows returned from a single table.
The following sample code contains two blocks of code to be evaluated separately.
The first block deletes the row created in the example for inserting a new row into a table:
1. Declares temporary variables for the database, a query specification to select the row to be deleted, and the row to be deleted
2. Returns the connection for the database manager
3. Opens the SAMPLE database
4. Defines a query specification to select a row
5. Executes the query and retrieves the row returned from the query
6. Deletes the row
The second block retrieves the STAFF table and converts it to an ordered collection that you can inspect.
Before you can evaluate this sample code, you need to connect to the database and create the row to be deleted.
To use these blocks of code, follow these steps:
1. Evaluate the first block using the Execute command.
2. Evaluate the second block using the Inspect command.
 
"Delete a row from a table"
| connection querySpec row |
connection := AbtDbmSystem activeDatabaseConnectionWithAlias: 'SampleConSpec'.
querySpec := (AbtQuerySpec new)
statement: 'SELECT * from STAFF
where ID = 999 '.
row := (connection resultTableFromQuerySpec: querySpec) first.
(connection openTableNamed: 'STAFF') deleteRow: row.
"Select table to show row has been deleted"
| connection querySpec result resultCollection |
resultCollection := OrderedCollection new.
connection := AbtDbmSystem activeDatabaseConnectionWithAlias: 'SampleConSpec'.
querySpec := (AbtQuerySpec new)
statement: 'SELECT * from STAFF'.
result := connection resultTableFromQuerySpec: querySpec.
result do: [:eachRow | resultCollection add: (eachRow asString)].
^resultCollection.
Last modified date: 01/29/2015