KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Getting the last table number
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: October 7, 2024

4D Tables can be identified by a table number. When a table is created a number is assigned starting from 1 and sequentially incrementing for each new table. Various 4D commands can target tables based on the table number.

In some cases the last table number may be needed. 4D provides the command Get last table number.
The behavior of this command is as follows:
- For 4D single user and 4D server, with direct access to the structure file, the largest number ever used is returned.
- For a 4D Remote client, since it does not have direct access to the structure file, it will instead parse the existing tables and return the largest number of the existing tables.

This means that if 10 tables were created, and the last 2 tables (table number 9 and number 10) are deleted, the Get last table number command will return 10 and for a 4D remote client connected to the same 4D server, calling the command from it would return 8 instead of 10.
This may be a little confusing when 4D Server returns 10 and 4D remote client returns 8.
When looping through table numbers, it is suggested to use the command Is table number valid to implement safe coding just in case of deleted tables.

If consistency is needed, below is a utility method that will return the largest number of the existing tables:

#DECLARE()->$last_l : Integer
Begin SQL
   SELECT max(TABLE_ID) FROM _USER_TABLES
   INTO :$last_l;
End SQL

The utility method will allow both 4D Server and 4D remote client to both return the same value. This can also improve performance for looping through the table numbers and not needing to perform additional iterations for deleted tables.

It is still suggested to utilize the Is table number valid command, for tables deleted with numbers less than the largest table number.