Compartir a través de


BEGIN TRANSACTION Command

Begins a transaction. Transactions are supported only for tables in a database. See CREATE DATABASE and ADD TABLE for information on how to create and add tables to a database.

BEGIN TRANSACTION

Remarks

To save any modifications you made and end the transaction, issue END TRANSACTION. If the transaction fails (the server fails, the workstation fails, or you exit Visual FoxPro without committing the transaction) or if you issue ROLLBACK, the file or files in the transaction are restored to their original state.

Transactions can be nested five levels deep. An error is generated if you attempt a sixth level of nesting.

When you modify records in a table that is part of a transaction, other users on the network do not have access (read or write) to the records until you end the transaction.

When other users on the network try to access records you have modified, they must wait until you end your transaction. They receive the message "Record not available ... please wait" until the records become available. Because of this, it is important to keep the length of the transaction to a minimum or conduct the transaction during times when others do not need access.

The following commands and functions are not supported during a transaction:

Commands and functions  
ADD TABLE DELETE CONNECTION
APPEND PROCEDURES DELETE DATABASE
CLEAR ALL DELETE TRIGGER
CLOSE ALL1 DELETE VIEW
CLOSE DATABASES1 MODIFY CONNECTION
COPY INDEXES MODIFY DATABASE
COPY PROCEDURES MODIFY PROCEDURE
CREATE CONNECTION MODIFY VIEW
CREATE DATABASE REMOVE TABLE
CREATE TRIGGER RENAME TABLE
CREATE VIEW REQUERY( )
CREATE SQL VIEW  

1 If CLOSE ALL is issued while a transaction is in progress, all tables in all open databases are closed. However, the databases remain open. Issuing CLOSE DATABASES within a transaction closes all tables in the current database, but the database remains open.

Also, the following commands and functions cannot be issued for a specific table participating in a transaction:

Commands and functions  
ALTER TABLE MODIFY STRUCTURE
CREATE TABLE PACK
CURSORSETPROP( ) REINDEX
DELETE TAG TABLEREVERT( )
INDEX ZAP
INSERT  

Example

In the following example, the customer table in the testdata database is opened. Optimistic table buffering is set for the customer table. The contents of the cust_id and company fields are displayed, and then the contents of the company field are replaced within the buffered data.

BEGIN TRANSACTION is issued to start a transaction. The TABLEUPDATE( ) function is used to write the changes to the table. The new contents are displayed, and ROLLBACK is issued to restore the original contents of the company field. The cust_id and company fields are displayed again with the company field containing its original values.

CLEAR
CLOSE DATABASES

* Transactions are only supported within a DBC
OPEN DATABASE (HOME(2) + 'Data\testdata')

SET MULTILOCKS ON      && Required for buffering


USE customer
=CURSORSETPROP("Buffering",5)
? 'The original company field'
LIST FIELDS cust_id, company NEXT 5
REPLACE ALL company WITH "***" && Change field contents

BEGIN TRANSACTION
   =TABLEUPDATE(.T.)
   GO TOP
   ? 'The modified company field'
   LIST FIELDS cust_id, company NEXT 5
   ROLLBACK           && Restore original field contents

=TABLEREVERT(.T.)
GO TOP
? 'The restored company field'
LIST FIELDS cust_id, company NEXT 5

See Also

END TRANSACTION | ROLLBACK | TXNLEVEL( ) Function