Example: Deleting a Row
Function delete_check is added to the
custquery.4gl module to check whether a
store has any orders in the database before allowing the user to delete the
store from the customer table. If there are no existing
orders, a dialog MENU is used to prompt the user for
confirmation.
Function delete_check
(custquery.4gl)
01 FUNCTION delete_check()
02 DEFINE del_ok SMALLINT,
03 ord_count SMALLINT
04
05 LET del_ok = FALSE
06
07 SELECT COUNT(*) INTO ord_count
08 FROM orders
09 WHERE orders.store_num =
10 mr_custrec.store_num
11
12 IF ord_count > 0 THEN
13 MESSAGE "Store has existing orders"
14 ELSE
15 MENU "Delete" ATTRIBUTES (STYLE="dialog",
16 COMMENT="Delete the row?")
17 COMMAND "Yes"
18 LET del_ok = TRUE
19 COMMAND "No"
20 MESSAGE "Delete canceled"
21 END MENU
22 END IF
23
24 RETURN del_ok
25
26 END FUNCTION- Line
02defines a variabledel_okto be used as a flag to determine if the delete operation should continue. - Line
05setsdel_oktoFALSE. - Lines
07thru10use thestore_numvalue in themr_custrecprogram record in an SQL statement to determine whether there are orders in the database for thatstore_num. The variableord_countis used to store the value returned by theSELECTstatement. - Lines
12thru13If the count is greater than zero, there are existing rows in theorderstable for thestore_num. A message is displayed to the user.del_okremains set toFALSE. - Lines
15thru21If the count is zero, the delete operation can continue. AMENUstatement is used to prompt the user to confirm theDeleteaction. TheSTYLEattribute is set to "dialog" to automatically display theMENUin a modal dialog window. If the user selects Yes, the variabledel_okis set toTRUE. Otherwise a message is displayed to the user indicating the delete will be canceled. - Line
24returns the value ofdel_okto thedelete_custfunction.
The function delete_cust is added to the custquery.4gl
module to delete the row from the customer
table.
Function
delete_cust
(custquery.4gl)01 FUNCTION delete_cust()
02
03 WHENEVER ERROR CONTINUE
04 DELETE FROM customer
05 WHERE store_num = mr_custrec.store_num
06 WHENEVER ERROR STOP
07 IF SQLCA.SQLCODE = 0 THEN
08 MESSAGE "Row deleted"
09 INITIALIZE mr_custrec.* TO NULL
10 ELSE
11 ERROR SQLERRMESSAGE
12 END IF
13
14 END FUNCTIONNote:
- Lines
04and05contains an embedded SQLDELETEstatement that uses thestore_numvalue in the program recordmr_custrecto delete the database row. The SQL statement is surrounded byWHENEVER ERRORstatements. This is a singleton transaction that will be automatically committed if it is successful. - Lines
07thru12check theSQLCA.SQLCODEreturned for the SQLDELETEstatement. If theDELETEwas successful, a message is displayed and themr_custrecprogram record values are set toNULLand automatically displayed on the form. Otherwise, an error message is displayed.