TABLEUPDATE( ) Function

Commits changes made to a buffered row, a buffered table, cursor, or cursor adapter.

TABLEUPDATE( [nRows [, lForce]] [, cTableAlias | nWorkArea] [, cErrorArray] )

Parameters

  • nRows
    Specifies which changes made to the table or cursor should be committed.

    Note   Visual FoxPro enables Optimistic Row Buffering by default for those cursors associated with a CursorAdapter object.

    The following table describes the values for nRows.

    nRows Description
    0 If row or table buffering is enabled, commit only the changes made to the current row in the cursor. (Default)

    When working with CursorAdapter objects, Visual FoxPro executes the appropriate command in the InsertCmd, UpdateCmd, or DeleteCmd property for that row only.

    1 If table buffering is enabled, commit changes made to all records to the table or cursor.

    If row buffering is enabled, commit only changes made to the current record in the table or cursor.

    When working with CursorAdapter objects, Visual FoxPro executes the appropriate commands in the InsertCmd, UpdateCmd, and DeleteCmd properties for each affected row.

    2 Commit changes made to the table or cursor in the same manner as when nRows = 1. However, an error does not occur when a change cannot be committed. Visual FoxPro continues to process any remaining records in the table or cursor.

    If cErrorArray is included, an array containing error information is created when an error occurs.

    For compatibility with previous Visual FoxPro applications, the nRows parameter also accepts False (.F.) and True (.T.) instead of 0 and 1 respectively.

    When specifying 0 or 1 for nRows, the record pointer remains on the record where changes could not be committed. To determine why the changes could not be committed, use the AERROR( ) function.

    When working with CursorAdapter objects and specifying 1 or 2 for nRows, all changes made to the cursor in the following CursorAdapter events must be committed during the same call to TABLEUPDATE( ) unless an error occurs**:**

    • BeforeInsert
    • AfterInsert
    • BeforeDelete
    • AfterDelete
    • BeforeUpdate
    • AfterUpdate

    Visual FoxPro passes the values of nRows to the CursorAdapterBeforeCursorUpdate event.

  • lForce
    Determines whether Visual FoxPro overwrites changes made to the table or cursor by another user on a network.

    The following table describes the values for lForce.

    lForce Description
    False (.F.) Commits changes to the table or cursor, starting with the first record and continuing towards the end of the table or cursor. (Default)

    If a record modified by another user on the network is encountered, Visual FoxPro generates an error. When Visual FoxPro generates the error, you can handle the error through an ON ERROR routine, and the ON ERROR routine can issue TABLEUPDATE( ) with lForce set to True (.T.) to commit changes to the record. Alternately, if a transaction is in progress, the ON ERROR routine can handle the error and then issue a ROLLBACK command to revert the table or cursor to its original state.

    True (.T.) Overwrites any changes made to the table or cursor by another user on a network. The WHERE clause uses only key fields.

    When working with CursorAdapter objects, Visual FoxPro passes the value of lForce to the following CursorAdapter events:

  • cTableAlias
    Specifies the alias of the table or cursor in which the changes are committed. If you include a table or cursor alias, you must include the lForce argument.

  • nWorkArea
    Specifies the work area of the table or cursor in which the changes are committed. If you include a work area, you must include the lForce argument.

  • cErrorArray
    Specifies the name of an array created when nRows = 2 and changes to a record cannot be committed. The array contains a single column containing the record numbers of the records for which changes could not be committed. If you include an array name, you must include either a table or cursor alias cTableAlias or a work area number nWorkArea.

    Note   If an error other than a simple commit error occurs while updating records, the first element of cErrorArray will contain –1, and you can then use AERROR( ) to determine the why the changes could not be committed.

    Visual FoxPro passes the value of cErrorArray, when it exists, to the CursorAdapterAfterCursorUpdate event.

Return Values

Logical data type. TABLEUPDATE( ) returns True (.T.) if changes to all records are committed. Otherwise, TABLEUPDATE( ) returns False (.F.).

Remarks

TABLEUPDATE( ) cannot commit changes made to a table or cursor that does not have row or table buffering enabled. If you issue TABLEUPDATE( ) and row or table buffering is not enabled, Visual FoxPro generates an error message. However, TABLEUPDATE( ) can still commit changes to a table or cursor that has validation rules. To enable or disable row and table buffering, use CURSORSETPROP( ).

Changes are committed to the table or cursor open in the currently selected work area if TABLEUPDATE( ) is issued without the optional cTableAlias or nWorkArea arguments.

If table buffering is used and multiple records are updated, TABLEUPDATE( ) moves the record pointer to the last record updated.

Note   Calling TABLEUPDATE( ) for a local table or view that does not use key fields generates a long WHERE clause to find the update row. The default number of fields supported in the WHERE clause is 40. If you receive the error 1812 - SQL: Statement Too Long, you should either use a key field for the update or increase the complexity of the WHERE clause with SYS(3055). If you use the SYS(3055) function, increase its value to a number that is eight times the number of fields in the table as shown in the following example:

SYS(3055, 8 * MIN(40, FCOUNT( ))

Interaction with CursorAdapter Objects   The following behaviors apply when working with CursorAdapter objects:

  • TABLEUPDATE( ) operates only on the cursor associated with the CursorAdapter object.

  • TABLEUPDATE( ) executes commands according to the data source type and those stored in the CursorAdapterInsertCmd, UpdateCmd, or DeleteCmd properties against the current cursor row or rows as appropriate.

  • TABLEUPDATE( ) passes the value of GETFLDSTATE(1) to the following CursorAdapter events of each row affected:

    For more information about GETFLDSTATE( ), see GETFLDSTATE( ) Function.

  • The successful completion of TABLEUPDATE( ) resets the field states according to usual TABLEUPDATE( ) behavior.

  • When table buffering is enabled, you can move away from the current record in the following CursorAdapter events:

    You can also modify data in the cursor. This functionality supports scenarios such as retrieving the autoincrement value from the base table and inserting it into the cursor. When this scenario occurs, the CursorAdapter object should automatically return to the record whose changes are about to be committed after the event has occurred and commit the changes.

Example

The following example demonstrates how you can use TABLEUPDATE( ) to commit changes made to a buffered table. A table named employees is created, and a SQL INSERT statement inserts the value "Smith" into the cLastName field.

MULTILOCKS is set to ON, which is a requirement for table buffering. CURSORSETPROP( ) is used to set the buffering mode to Optimistic Table Buffering (5).

The original value of the cLastName field (Smith) is displayed, and the cLastName field is modified with REPLACE. The new value of the cLastName field (Jones) is displayed. TABLEUPDATE( ) is used to commit changes to the table (TABLEREVERT( ) could be issued instead to discard the changes). The updated value of the cLastName field (Jones) is then displayed.

CLOSE DATABASES
CREATE TABLE employee (cLastName C(10))
SET MULTILOCKS ON  && Must turn on for table buffering.
= CURSORSETPROP('Buffering', 5, 'employee' )  && Enable table buffering.
INSERT INTO employee (cLastName) VALUES ('Smith')

CLEAR
? 'Original cLastName value: '
?? cLastName  && Displays current cLastName value (Smith).

REPLACE cLastName WITH 'Jones'
? 'New cLastName value: '
?? cLastName  && Displays new cLastName value (Jones).

= TABLEUPDATE(.T.)  && Commits changes.
? 'Updated cLastName value: '
?? cLastName  && Displays current cLastName value (Jones).

See Also

Functions | CURSORSETPROP( ) | CURVAL( ) | OLDVAL( ) | TABLEREVERT( ) | Cursor Object