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
02
defines a variabledel_ok
to be used as a flag to determine if the delete operation should continue. - Line
05
setsdel_ok
toFALSE
. - Lines
07
thru10
use thestore_num
value in themr_custrec
program record in an SQL statement to determine whether there are orders in the database for thatstore_num
. The variableord_count
is used to store the value returned by theSELECT
statement. - Lines
12
thru13
If the count is greater than zero, there are existing rows in theorders
table for thestore_num
. A message is displayed to the user.del_ok
remains set toFALSE
. - Lines
15
thru21
If the count is zero, the delete operation can continue. AMENU
statement is used to prompt the user to confirm theDelete
action. TheSTYLE
attribute is set to "dialog" to automatically display theMENU
in a modal dialog window. If the user selects Yes, the variabledel_ok
is set toTRUE
. Otherwise a message is displayed to the user indicating the delete will be canceled. - Line
24
returns the value ofdel_ok
to thedelete_cust
function.
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 FUNCTION
Note:
- Lines
04
and05
contains an embedded SQLDELETE
statement that uses thestore_num
value in the program recordmr_custrec
to delete the database row. The SQL statement is surrounded byWHENEVER ERROR
statements. This is a singleton transaction that will be automatically committed if it is successful. - Lines
07
thru12
check theSQLCA.SQLCODE
returned for the SQLDELETE
statement. If theDELETE
was successful, a message is displayed and themr_custrec
program record values are set toNULL
and automatically displayed on the form. Otherwise, an error message is displayed.