Exploring Alternatives for SELECT TOP in X++ - D365 Finance and Supply Chain

Recently, I had to write a query that retrieves Batches with the oldest Manufacturing Date in D365 Finance and Supply Chain, utilizing the "SELECT TOP" functionality. However, as you may be aware, the TOP keyword is natively supported in SQL but not when composing a select statement in D365 and X++. Nevertheless, there are alternative techniques to accomplish the same objective. Additionally, I have included some performance metrics to assist you in choosing the most appropriate method based on your specific requirements.

Scenario: Write a query to get the top 3 batches and locations which have the oldest Manufacturing Date for an item and have Available Physical > 0, grouped by the Item, Batch Id, Manufacturing Date and the location.

Assumptions: One legal entity, with one site and warehouse

Here's how the SELECT TOP Clause would look like in SQL:



Method 1: Select statement using a break 
Once we reach the required number of records, we break from the while loop.


Performance:


Method 2: Query
The Query class does have a topRows method which allows you to specific the max number of records to return.

Performance:


Method 3: SysDa framework
The SysDa Framework also has a top method on the SysDaQueryObject to indicate the number of records to return.

Performance:


Hope this helps in giving you some ideas and making the right decision for your business requirements.


Comments

  1. Nice post. Thanks Shail. I did not know about topRows() exists in Query class. Good to know. Meantime, SysDa is still something mystical for me, I have not worked with that yet))

    ReplyDelete

Post a Comment

Popular posts from this blog

Override a System Defined Refresh button in D365O

Printing PDF Attachments in MSDyn365FO