Microsoft Dynamics 365 Blog

The command COUNT is pretty simple. But on the SQL option there are a number of different ways to count records, and each can give a different result. This is how the different ways of counting works in NAV on SQL:

There are two commands you can use: COUNT or COUNTAPPROX. As the name suggests, COUNTAPPROX is only designed to give an approximate count. But this does not necessarily mean that COUN is exact (I will come back to this later).

This command doesn’t actually count anything. It only asks SQL Server to make a query plan for a SELECT statement. Then it reads the estimated number of records from the query plan. This is how COUNTAPPROX looks in a profiler trace:




* FROM “CRONUS International Ltd_$Standard Text” WITH (READUNCOMMITTED)



It is the same as if you click on Query -> “Display Estimated Execution Plan (Ctrl+L)” in SQL Server Management Studio, instead of running a query normally (Ctrl+E / F5). This tells SQL Server to only compile a query plan, but not actually run the query. The query plan will contain “estimated number of rows”, which in turn is based on SQL Server statistics. This is the number that COUNTAPPROX returns.


COUNT works differently, depending on whether there is a filter on the table or not. Without a filter it can just go to Table-Information, and get the number of records from there. This is quicker than going to the actual table and count every record. On the SQL Side, it means counting the number of rows from the sysindexes table, like this:


SUM(rows), SUM(reserved) FROM [dbo].[sysindexes]

But sysindexes is not always up to date! Only just after having updated statistics, can you rely on the numbers in this table. So COUNT, without a filter, will not always give you the correct number.

If you have a filter, for example:

Cust.SETRANGE(“Currency Code”,’EUR’);

i := Cust.COUNT;

Then the method of just looking up table-information doesn’t work. So NAV has to count the records the hard way. In this case we finally get an actual COUNT SQL command:


COUNT(*) FROM “CRONUS International Ltd_$Customer”

But even this method of counting is still not necessarily accurate, because it reads uncommitted data. If you run the following code from another client, and leave the CONFIRM-dialog: 

Cust.”Currency Code” := ‘EUR’;


ERROR(‘Transaction rollback.’); 

Then, the COUNT above will include a record which never existed / was never committed.

The only way to get an exact count, is by applying a lock. For example:


i := Cust.COUNT;

This will give you an exact count, whether you have a filter or not. On the SQL side it will look like this:


COUNT(*) FROM “CRONUS International Ltd_$Customer” WITH (UPDLOCK)

Because of the UPDLOCK it only counts committed records. And because of the lock, NAV knows not to just look up the number of records from table information, but to run a SELECT COUNT on SQL Server.


Lars Lohndorf-Larsen (Lohndorf)
Microsoft Dynamics UK

We're always looking for feedback and would like to hear from you. Please head to the Dynamics 365 Community to start a discussion, ask questions, and tell us what you think!