web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Select Random Rows or Records From table using X++ (No Direct SQL Please)

(0) ShareShare
ReportReport
Posted on by 2,546

Hello Folks,

I need to Select Random Rows or Records From table using X++.

In SQL below query is working fine.

SELECT TOP 10 * 
FROM CustTable
ORDER BY NEWID();

but I want to achieve the same result in D365 using X++ without any Direct SQL statement

Please suggest
I have the same question (0)
  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    You can't choose random records in Ax like this (at least I couldn't remember). However, it is possible to generate a random value and compare(>= or <=) it to a field like RecId and use the first 10 records in a while loop.

    You can use System.Random to generate random value.

    docs.microsoft.com/.../system.random

    You can identify a range by looking at the first and last RecIds.

  • Suggested answer
    Jack D Profile Picture
    60 on at

    If you would explain the business case why you would need random records it could maybe help with providing a better solution. Since D365 is using a relational database you will always get ordered data instead of random records.

    simple idea;

    To get some sort of random data is selecting for example the first 100 records. Put the records in an array (container) and generate a random value between 1 and 100. When for example the number is 15 get the data in the 15th element of the array.

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi Piyush,

    what's the business requirement that you're trying to solve by fetching random records?

  • Piyush Adhikari Profile Picture
    2,546 on at

    Business Requirement -

    we have to a specific number (Configurable) of records or rows from Work Orders Table for Sample Collection.  

    After Sample Collection , Auditor will work on those work Orders

    This activity will be repeated more than 1 . So every time should pick random work orders for sample collection

  • Piyush Adhikari Profile Picture
    2,546 on at

    ***Business Requirement -

    we have to Select a specific number (Configurable) of records or rows from Work Orders Table for Sample Collection.  

    After Sample Collection , Auditor will work on those work Orders

    This activity will be repeated more than 1 . So every time should pick random work orders for sample collection

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Perhaps this is something that can be discussed with the auditor. I have not yet heard similar requirement - usually the "random" samples are picked manually and not via some random generator. I wonder why the auditor wants to let the system decide which records to pick - isn't this something that could potentially compromise the quality of the to-be-audited data?

    Anyway, one simple approach would be to check lowest and highest RecId in the table, then generate random numbers between those two numbers, and fetch the corresponding record. If there's no record for the generated random RecId, pick new.

    You can of course also get all RecIds and select random values from there but it would be a bit slower since the system would need to provide all existing RecId values.

    But most likely you need more criteria - otherwise the auditor would most likely get records from past years and not from the year that they're auditing.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 549 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans