C Copyright © Oracle Corporation 1995. All Rights Reserved. SUBROUTINE delete_record C----------------------------------------------------- C This subroutine deletes records from the database. C----------------------------------------------------- IMPLICIT NONE LOGICAL success,found_employee INTEGER retry_count,trans1*4 CHARACTER employee_id*5,confirm,data_base_key*8 &RDB& DATABASE EXTERNAL pers = FILENAME 'MF_PERSONNEL' &RDB& DBKEY SCOPE IS FINISH WRITE (6,90) 90 FORMAT ('1',T25,'**** DELETE EMPLOYEE ****'///) C---------------------------------------------------------- C Prompt user the for ID of the employee records that he C or she wants to delete from the database. C---------------------------------------------------------- TYPE 100 100 FORMAT (' Please enter the ID of the Employee ') TYPE 110 110 FORMAT ('$',' you want to delete or type exit: ') ACCEPT 120, employee_id 120 FORMAT (A) DO WHILE ((employee_id.NE.'EXIT ').AND.(employee_id.NE.'exit ')) confirm = 'N' success = .TRUE. DO WHILE (confirm .EQ. 'N') &RDB& START_TRANSACTION (TRANSACTION_HANDLE trans1) &RDB& READ_WRITE RESERVING EMPLOYEES FOR SHARED READ found_employee = .FALSE. C------------------------------------------------------ C Find the record of the employee that the user wants C to delete. If an error occurs during the FOR operation C call an error handler. C-------------------------------------------------------- &RDB& FOR (TRANSACTION_HANDLE trans1) &RDB& E IN EMPLOYEES WITH &RDB& E.EMPLOYEE_ID = employee_id &RDB& ON ERROR success = .FALSE. CALL error_handler(RDB$STATUS,success) IF (success) THEN retry_count = 5 END IF &RDB& END_ERROR C-------------------------------------------------------- C Get the database key of the employee that the user C wants to delete. C-------------------------------------------------------- &RDB& GET &RDB& ON ERROR success = .FALSE. &RDB& END_ERROR &RDB& data_base_key = E.RDB$DB_KEY &RDB& END_GET found_employee = .TRUE. &RDB& END_FOR IF (.NOT.(found_employee)) THEN TYPE 2020, employee_id 2020 FORMAT (' Employee id: ',A,' is 1 not on file') C-------------------------------------------------------------- C Pass the dbkey to an external routine "CALL_OTHER" C to print out the record to which the dbkey points. C Note that using an external routine is neither necessesary C nor recommended for performing this task. It is done in this C example only to show how values are passed between routines C in an RDBPRE FORTRAN program. C--------------------------------------------------------------- ELSE IF (success) THEN CALL call_other(data_base_key,trans1) END IF &RDB& COMMIT (TRANSACTION_HANDLE trans1) C-------------------------------------------------------------- C Ask user for confirmation that this is the employee whose C records he or she wants to delete. C------------------------------------------------------------- IF (found_employee) THEN PRINT *, ' ' TYPE 10000 10000 FORMAT ('$',' Is this the employee 1 you want to delete? (Y/N): ') ACCEPT 10010, confirm 10010 FORMAT (A) END IF IF (confirm .EQ. 'N') THEN TYPE 4000, employee_id 4000 FORMAT (' Employee id: ',A,' not 1deleted',/) END IF IF ((confirm .EQ. 'N').OR.(.NOT.(found_employee))) THEN PRINT *, ' ' TYPE 100 TYPE 110 ACCEPT 120, employee_id IF ((employee_id.EQ.'EXIT ').OR. 1 (employee_id.EQ.'exit ')) THEN confirm = 'Y' END IF END IF END DO IF ((employee_id.NE.'EXIT ').AND. 1 (employee_id.NE.'exit ')) THEN C--------------------------------------------------------------- C If the user wants to delete the employee's records, then start C a read_write transaction and delete the employee's records C from all relations in which his employee_id appears. Note C that this is all done in one transaction. You would C not want to split this task across transactions. If one C of many transactions failed, you would not be certain C that the employee's record were deleted from all the relations. C-------------------------------------------------------------- &RDB& START_TRANSACTION READ_WRITE RESERVING &RDB& EMPLOYEES, SALARY_HISTORY, JOB_HISTORY, &RDB& DEPARTMENTS, DEGREES, WORK_STATUS, &RDB& RESUMES FOR SHARED WRITE &RDB& FOR E IN EMPLOYEES WITH &RDB& E.RDB$DB_KEY = data_base_key &RDB& FOR JH IN JOB_HISTORY WITH &RDB& JH.EMPLOYEE_ID = E.EMPLOYEE_ID &RDB& ERASE JH &RDB& END_FOR &RDB& FOR SH IN SALARY_HISTORY WITH &RDB& SH.EMPLOYEE_ID = E.EMPLOYEE_ID &RDB& ERASE SH &RDB& END_FOR &RDB& FOR D IN DEGREES WITH &RDB& D.EMPLOYEE_ID = E.EMPLOYEE_ID &RDB& ERASE D &RDB& END_FOR &RDB& FOR R IN RESUMES WITH &RDB& R.EMPLOYEE_ID = E.EMPLOYEE_ID &RDB& ERASE R &RDB& END_FOR &RDB& ERASE E &RDB& END_FOR &RDB& COMMIT PRINT *, ' ' TYPE 100 TYPE 110 ACCEPT 120, employee_id END IF END DO RETURN END