In a previous blog we discussed how tables in
The RecId column
To make it easier to identify records in AX tables and to avoid duplicates, AX adds a column to each table called RecId. The RecId column is a 64-bit integer column that is automatically assigned a unique number within the table whenever a new record is created. Once assigned, the RecId column cannot be edited using the AX interface.
Note that the RecId column is not a SQL Server auto-increment field and the number assigned to the record is generated by the AX system. If you insert a record using SQL Server’s query tools, the RecId column will be left blank.
How to query tables
The process to retrieve data in AX consists of the following steps:
- Write an AX SQL statement to retrieve the records.
- Loop through the set of records retrieved from the AX SQL statement.
- Assign the retrieved record to an AX variable.
- Query the AX variable data.
Most of these steps look similar to what a programmer would use to retrieve data from a database table, except for step number three.
In AX, you need to declare a variable of type “table”. So a table in AX is both a table and also a data type.
Here is a basic example that retrieves items from the item master table (InventTable) and display their item Id and description. (Line numbers were added for reference)
Let’s look closely at the code.
Line 1 declares a variable “inventTable” of type “InventTable”. Note the “camel-case” convention used to differentiate the variable name from the table name. AX is case-insensitive so this is only used to make it easier for the programmer to read.
Line 2 has the AX SQL statement. Note that there is no need to specify the columns to be retrieved or even the Transact-SQL “for” or “*” keywords if all columns from the AX table are to be retrieved. The “while” loop in this line will go through each one of the records retrieved by the “select” statement. On each loop, AX will assign the current record to the declared AX variable.
Line 4 calls two functions to send data to AX’s InfoLog window (Info) and the other to concatenate the resulting string.
Line 5 retrieves the ItemId column from the inventTable variable.
In line 6, you can see that it is possible to not only retrieve column data, but to also call methods defined in the table related to the current record retrieved. In this case, the item name is not stored in the InventTable table, so this method will look for it and retrieve it for you, simplifying the work for the programmer. Of course, this feature comes with a price: there will be as many calls to this method as the number of records retrieved by the query.
There is always the option to join tables together in order to retrieve columns like the “item name”, but to do this, you will need to understand how tables are related in AX.
It is up to the programmer to decide which path to follow.
This article was written by Eduardo Sicouret, Dynamics Technical Consultant for
by Tridea Partners