Batch
updating can improve the performance of your application. SAS permits
batch updates when the data set has been opened with an exclusive
member lock. To enable an exclusive member lock, you set the customized
Recordset property SAS Optimistic Locking to
False
, as shown in the sample code.
After
granting exclusive member-level access to one user, you can use the
adLockBatchOptimistic lock type, which does not lock the open Recordset
until the UpdateBatch method is called. When UpdateBatch is called,
the provider compares each record in the local buffer against the
underlying data source to ensure that it has not been changed by another
process. If a record has been changed, the batch update will fail.
The Recordset Open must specify the adCmdTableDirect option, and the
CursorType property must be set to adOpenStatic or adOpenDynamic.
The following Visual Basic code shows how this task is done:
' obConnection is an open Connection object.
Dim obRecordset As New ADODB.Recordset
' Set exclusive member access.
obRecordset.ActiveConnection = obConnection
obRecordset.Properties("SAS Optimistic Locking") = False
'Set the maximum number of rows for batch update.
obRecordset.Properties("Maximum Open Rows") = 1000
obRecordset.LockType = adLockBatchOptimistic
obRecordset.CursorType = adOpenStatic
obRecordset.Open "mydata.customers", , , , adCmdTableDirect
'Change all of the records.
While Not obRecordset.EOF
rs!balance = 0
obRecordset.MoveNext
Wend
' The record is locked, updated, and then unlocked.
obRecordset.UpdateBatch
Note: If a data
set has not been opened with an exclusive member lock, then the UpdateBatch
method ignores the batch request and updates the records one at a
time, and there will be no performance gain.