Triggers [examples]

Creates a trigger for a table
CREATE_TRIGGER
CREATE TRIGGER ON customers FOR UPDATE AS not empty(custid)

Deletes a trigger from a table
DELETE_TRIGGER
USE accounts
CREATE TRIGGER ON customer FOR UPDATE AS not empty(CustName)
DELETE TRIGGER ON customer FOR BEFOREUPDATE

Display triggers associated with current table
DISPLAY_TRIGGERS
open database southwind
use customers
display triggers

Display triggers associated with current table
LIST_TRIGGERS
open database southwind
use customers
list triggers

Enable or disable the triggering of Database Events
SET_DBCEVENTS
set dbcevents off

Triggered following a delete operation when a database is open and runs dbc_afterdelete.prg (.dbo compiled/runtime) if it exists
//
// Database: dbcdoc
// Event: dbc_afterdelete
//
parameters cDatabase, cTable, cJSON
userlog("====================================")
userlog("After Delete in "+"dbcdoc database")
userlog("Database: " +cDatabase)
userlog("Table: " +cTable)
if ! isserver()
	userlog("Deleted record number: " + etos(recno())
else
	userlog("JSON: " + base64_decode(cJSON))
endif
userlog("End of After Delete")
userlog("====================================")
return .t.

Triggered following an insert operation when a database is open and runs dbc_afterinsert.prg (.dbo compiled/runtime) if it exists
// Database events are triggered by certain database operations 
//
// Database: dbcdoc 
// Event: dbc_afterinsert
//
parameters cDatabase, cTable, cJSON
userlog("====================================")
userlog("After Insert in "+"dbcdoc database")
userlog("Database: " +cDatabase)
userlog("Table: " +cTable)
if ! isserver()
	for i = 1 to fldcount()
		if type("&(field(i))") = "G"
			loop
		endif		
		userlog("Field: " + field(i))
		userlog("Value: " + etos(&(field(i))))
	endfor
else
	userlog("JSON: " + base64_decode(cJSON))
endif
userlog("End of After Insert")
userlog("====================================")
return .t.

Triggered following an update operation when a database is open and runs dbc_afterupdate.prg (.dbo compiled/runtime) if it exists
// Database events are triggered by certain database operations 
//
// Database: dbcdoc
// Event: dbc_afterupdate
//
parameters cDatabase, cTable, cJSON
userlog("====================================")
userlog("After Update in "+"dbcdoc database")
userlog("Database: " +cDatabase)
userlog("Table: " +cTable)
 
if ! isserver()
	userlog("Updated record number: " + etos(recno())
else
	userlog("JSON: " +base64_decode(cJSON)
endif
 
userlog("End of After Update")
userlog("====================================")
return .t.

https://www.lianja.com/doc/index.php/DBC_AFTERUPDATE


Triggered prior to committing a delete operation when a database is open and runs dbc_beforedelete.prg (.dbo compiled/runtime) if it exists
// Database events are triggered by certain database operations 
//
// Database: dbcdoc
// Event: dbc_beforedelete
//
parameters cDatabase, cTable, cJSON
userlog("====================================")
userlog("Before Delete in "+"dbcdoc database")
userlog("Database: " +cDatabase)
userlog("Table: " +cTable)
if ! isserver()
	userlog("Delete record number: " + etos(recno())
else
	userlog("JSON: " +base64_decode(cJSON))
endif
userlog("End of Before Delete")
userlog("====================================")
return .t.

Triggered prior to committing an insert operation when a database is open and runs dbc_beforeinsert.prg (.dbo compiled/runtime) if it exists
// Database events are triggered by certain database operations 
//
// Database: dbcdoc
// Event: dbc_beforeinsert
//
parameters cDatabase, cTable, cJSON
userlog("====================================")
userlog("Before Insert in "+"dbcdoc database")
userlog("Database: " +cDatabase)
userlog("Table: " +cTable)
if ! isserver()
	for i = 1 to fldcount()
		if type("&(field(i))") = "G"
			loop
		endif	
		userlog("Field: " + field(i))
		userlog("Value: " + etos(&(field(i))))
	endfor
else
	userlog("JSON: " +base64_decode(cJSON))
endif
userlog("End of Before Insert")
userlog("====================================")
return .t.

Triggered prior to committing an update operation when a database is open and runs dbc_beforeupdate.prg (.dbo compiled/runtime) if it exists
// Database events are triggered by certain database operations 
//
// Database: dbcdoc
// Event: dbc_beforeupdate
//
parameters cDatabase, cTable, cJSON
userlog("====================================")
userlog("Before Update in "+"dbcdoc database")
userlog("Database: " +cDatabase)
userlog("Table: " +cTable)
 
if ! isserver()
	userlog("Update record number: " + etos(recno())
	for i = 1 to fldcount()
		if type("&(field(i))") = "G"
			loop
		endif		
		userlog("Field: " + field(i))
		userlog("Curval(): " + etos(curval(field(i))))
		userlog("Value: " + etos(&(field(i))))
	endfor
else
	userlog("JSON: " +base64_decode(cJSON)
endif
 
userlog("End of Before Update")
userlog("====================================")
return .t.

Triggered by the closure of a database and runs dbc_closedata.prg (.dbo compiled/runtime) if it exists
// Database event triggered by the closure of a database 
// dbc_closedata.prg
lparameters cName, lAll
close procedures
return

Triggered when a database is opened and runs dbc_opendata.prg (.dbo compiled/runtime) if it exists
// Database events are triggered by certain database operations 
// dbc_opendata.prg
lparameters cName, lExcl, lRo, lValidate
// ...
return .T.

open database southwind
alter table customers modify onupdate "p_update"
alter table customers modify ondelete "p_delete"
alter table customers modify oninsert "p_insert"
alter table customers modify onopen "p_open"
alter table customers modify onclose "p_close"

https://www.lianja.com/doc/index.php/Securing_Your_Data


Table constraint activated after a record in the table is deleted

ALTER TABLE customer;
  modify ONFTERDELETE "customer_afterdelete"

https://www.lianja.com/doc/index.php/ONAFTERDELETE_Table_Constraint


Table constraint activated after a record in the table is inserted

ALTER TABLE customer;
  modify ONFTERINSERT "customer_afterinsert"

https://www.lianja.com/doc/index.php/ONAFTERINSERT_Table_Constraint


Table constraint activated after a record in the table is updated

ALTER TABLE customer;
  modify ONFTERUPDATE "customer_afterupdate"

https://www.lianja.com/doc/index.php/ONAFTERUPDATE_Table_Constraint


Table constraint activated when an attempt is made to delete a record in the table

ALTER TABLE customer;
  modify ONDELETE "customer_ondelete"
ALTER TABLE customer;
  modify ONBEFOREDELETE "customer_onbeforedelete"

https://www.lianja.com/doc/index.php/ONBEFOREDELETE_Table_Constraint


Table constraint activated when an attempt is made to delete a record in the table

ALTER TABLE customer;
  modify ONDELETE "customer_ondelete"
ALTER TABLE customer;
  modify ONBEFOREDELETE "customer_onbeforedelete"

https://www.lianja.com/doc/index.php/ONDELETE_Table_Constraint


Table constraint activated when an attempt is made to delete a record in the table

ALTER TABLE customer;
  modify ONDELETE "customer_ondelete"
ALTER TABLE customer;
  modify ONBEFOREDELETE "customer_onbeforedelete"

https://www.lianja.com/doc/index.php/ONBEFORE_Table_Constraint


Table constraint activated when an attempt is made to insert a new record into the table

ALTER TABLE customer;
  modify ONINSERT "customer_oninsert"
ALTER TABLE customer;
  modify ONBEFOREINSERT "customer_onbeforeinsert"

https://www.lianja.com/doc/index.php/ONBEFOREINSERT_Table_Constraint


Table constraint activated when an attempt is made to insert a new record into the table

ALTER TABLE customer;
  modify ONINSERT "customer_oninsert"
ALTER TABLE customer;
  modify ONBEFOREINSERT "customer_onbeforeinsert"

https://www.lianja.com/doc/index.php/ONINSERT_Table_Constraint


Table constraint activated when an attempt is made to update a record in the table

ALTER TABLE customer;
  modify ONUPDATE "customer_onupdate"
ALTER TABLE customer;
  modify ONBEFOREUPDATE "customer_onbeforeupdate"

https://www.lianja.com/doc/index.php/ONBEFOREUPDATE_Table_Constraint


Table constraint activated when an attempt is made to update a record in the table

ALTER TABLE customer;
  modify ONUPDATE "customer_onupdate"
ALTER TABLE customer;
  modify ONBEFOREUPDATE "customer_onbeforeupdate"

https://www.lianja.com/doc/index.php/ONUPDATE_Table_Constraint


Table constraint activated when the table is closed

ALTER TABLE customer;
  modify ONCLOSE "p_close"

https://www.lianja.com/doc/index.php/ONCLOSE_Table_Constraint


Table constraint activated when the table is opened

ALTER TABLE customer;
  modify ONOPEN "p_open"

https://www.lianja.com/doc/index.php/ONOPEN_Table_Constraint