How to: Reference: |
The DELETE command identifies segment instances from a transaction source (a stack or the Current Area) and deletes the corresponding instances from the data source.
When you issue the command, you specify an anchor segment. For each row in the transaction source, DELETE searches the data source for a matching segment instance. When it finds a match, it deletes that anchor instance and all the anchor's descendants.
If the anchor segment is not the root, you must establish a current instance in each of the anchor's ancestor segments, or provide ancestor segment key values in the source stack. This ensures that DELETE can navigate from the root to the anchor segment's first instance.
The syntax of the DELETE command is
[FOR {int|ALL}] DELETE segment [FROM stack[(row)]] [;]
where:
Is used with ALL or an integer to specify how many stack rows to use to identify segment instances. If FOR is omitted, one stack row will be used.
When you specify FOR, you must also specify FROM to identify a source stack.
Is an integer constant or variable that indicates the number of stack rows to use to identify segment instances to be deleted.
Specifies that the entire stack is used to delete the corresponding records in the data source.
Specifies the anchor segment of the path you wish to delete. To specify a segment, provide the name of the segment or of a field within the segment.
Is used to specify a stack whose key columns identify records to delete. If no stack is specified, data from the Current Area is used.
Is a stack name. Only one stack can be specified.
Is a subscript that specifies which stack row to begin with.
Terminates the command. Although the semicolon is optional, it is recommended that you include it to allow for flexible syntax and better processing. For more information about the benefits of including the semicolon, see Terminating a Command's Syntax.
The DELETE command removes the lowest specified segment and all of its descendant segments. For example, if a data source structure has four segments in a single path (named First, Second, Third, and Fourth), the command
DELETE First.Field1 Second.Field2;
will delete instances from the Second, Third, and Fourth segments.
If you issue the command
DELETE First.Field1;
you will delete the entire data source path.
In the following example the data in rows 2, 3, and 4 of the Stkemp stack is used to delete data from the data source. The stack subscript indicates start in the second row of the stack and the FOR 3 means DELETE data in the data source based on the data in the next 3 rows.
FOR 3 DELETE Emp_ID FROM Stkemp(2);
The first example prompts the user for the employee ID in the EmployeeIDForm form. If the employee is already in the data source, all records for that employee are deleted from the data source. This includes the employee's instance in the root segment and all descendent instances (such as pay dates, addresses, etc.). In order to find out if the employee is in the data source, a MATCH command is issued:
MAINTAIN FILE Employee Winform Show EmployeeIDForm; CASE DELEMP MATCH Emp_ID; ON MATCH DELETE Emp_ID; ON NOMATCH TYPE "Employee id <Emp_ID not found. Reenter"; COMMIT; ENDCASE END
When the user presses Enter, function DELEMP is triggered from a Winform. Control is then passed back to EmployeeIDForm.
The second example provides the same functionality. The only difference is that a MATCH is not used to determine whether the employee already exists in the data source. The DELETE can only work if the record exists. Therefore if an employee ID is entered that does not exist, the only action that can be taken is to display a message. In this case, the variable FocError is checked. If FocError is not equal to zero, then the DELETE failed and the message is displayed:
MAINTAIN FILE Employee INFER EMP_ID INTO STACKEMP Winform Show EmployeeIDForm; CASE DELEMP DELETE Emp_ID; IF FocError NE 0 THEN TYPE "Employee id <Stackemp.Emp_ID not found. Reenter"; COMMIT; ENDCASE END
Information Builders |