The update_recordset Operator
The behavior of the update_recordset operator is very similar to that of the insert_recordset
operator. This similarity is illustrated by the following piece of X++
code, in which all sizes for an item are updated with a new description.
static void UpdateSizes(Args _args) { InventSize inventSize; ; ttsbegin; update_recordset inventSize setting Description = 'This size is for item 1000' where inventSize.itemId == '1000'; ttscommit; }
|
The execution of update_recordset results in one statement being parsed to the database, which in SQL Server uses a syntax similar to UPDATE <table> <SET> <field and expression list> WHERE <predicates>. As with insert_recordset, update_recordset
provides a tremendous improvement in performance over the record-based
version, in which each record is updated individually. This improvement
is shown in the following X++ code, which serves the same purpose as
the preceding example. The code selects all the records qualified for
update, sets the new description value, and updates the record.
static void UpdateSizes(Args _args) { InventSize inventSize; ; ttsbegin; while select forupdate inventSize where inventSize.itemId == '1000' { inventSize.Description = 'This size is for item 1000'; inventSize.update(); } ttscommit; }
|
If 10 records qualified, 1 select statement and 10 update statements would be parsed to the database rather than the single update statement that would be parsed if you used update_recordset.
The update_recordset operation can be downgraded if specific methods have been overridden or the the Dynamics AX application is configured. The update_recordset operation is downgraded if any of the following conditions is true:
The table is entire-table cached.
The update method, the aosValidateUpdate method, or the aosValidateRead method is overridden on the target table.
Alerts have been set up to be triggered by update queries into the target table.
The database log has been configured to log update queries into the target table.
RLS is enabled on the target table.
The Dynamics AX application runtime automatically handles the downgrade and internally executes a scenario similar to the while select scenario shown in the preceding example.
You
can avoid any downgrade caused by the previously mentioned
functionality unless the table is entire-table cached. The record
buffer contains methods that turn off the checks that the application
runtime performs when determining whether to downgrade the update_recordset operation.
Calling skipDataMethods(true) prevents the check that determines whether the update method is overridden.
Calling skipAosValidation(true) prevents the checks on the aosValidateUpdate and aosValidateRead methods.
Calling skipDatabaseLog(true) prevents the check that determines whether the database log is configured to log updates to records in the table.
Calling skipEvents(true) prevents the check to determine whether any alerts have been set to be triggered by the update event on the table.
As
we explained earlier, you should use the skip methods with great
caution, and you should take the same precautions before using the skip
methods in combination with the update_recordset operation. Again, using the skip methods only influences whether the update_recordset operation is downgraded to a while select
scenario. If the operation is downgraded, database logging, alerting,
and execution of overridden methods occurs even though the respective
skip methods have been called.
Tip
If an update_recordset operation is downgraded to a while select scenario, the select statement uses the concurrency model specified at the table level. You can apply the optimisticlock and pessimisticlock keywords to the update_recordset statements and enforce a specific concurrency model to be used in case of downgrade. |
Dynamics AX 2009 supports inner and outer joins in update_recordset. Previous versions of Dynamics AX supported only exists and not exists joins. The support for joins in update_recordset enables an application to perform set-based operations when the source data is fetched from more than one related Data Source.
The following example illustrates the usage of joins with update_recordset.
static void UpdateRecordsetJoinsExample(Args _args) { NewTaxes taxTable; Orders ordersTable; ; ttsbegin; UPDATE_RECORDSET ordersTable SETTING Total = ordersTable.Total + ( ordersTable.Total * ( taxTable.TaxPercent / 100 ) ) JOIN taxTable WHERE ordersTable.CountryId == taxTable.CountryId ; ttscommit; }
|