Skip to main content
Post a question

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id : 1jeluKRsi4WjRM3K+itG9b
Microsoft Dynamics AX (Archived)

Select Top in X++ code

Like (0) ShareShare
ReportReport
Posted on 17 Jan 2017 01:48:19 by

Hi everybody,

I have a problem that needs to select top x records like SQL in X++ code.

I found that X++ has firstonly, firstonly10, firstonly100 and firstonly1000.

but i need to select x records, and x is a variable.

What can i do for this case.

Thank for your help.

*This post is locked for comments

  • Community Member Profile Picture
    on 17 Jan 2017 at 09:19:51
    RE: Select Top in X++ code

    Thank you very much for your help.

    I have resolved this problem using counter.

  • Verified answer
    Martin Dráb Profile Picture
    231,797 Most Valuable Professional on 17 Jan 2017 at 07:02:13
    RE: Select Top in X++ code

    Select Statement Syntax documents what's supported, and you can see that there is no TOP or anything similar. It's unfortunate, but that's all what we have.

    Selecting more records and ignoring what you don't need (e.g. selecting with firstonly10 and taking just 7) is usually a reasonable approach.

    Another solution is using a view with a computed column with RANK() or ROW_NUMBER(). Then you can filter by this computed column (WHERE RowNumber < 8). It's more complicated, but it's useful in some situations.

  • Suggested answer
    Luan Nguyen Profile Picture
    689 on 17 Jan 2017 at 06:51:29
    RE: Select Top in X++ code

    you may have 2 ways to do it:

    1. use Counter then break;

    2. use a temporary record buffer then remove the records that you don't need.

  • Suggested answer
    startax Profile Picture
    1,845 on 17 Jan 2017 at 05:05:16
    RE: Select Top in X++ code

    @ Tu Ngyen ,

    As David says you have to use Counter in this case ,

    community.dynamics.com/.../222422

  • Verified answer
    DavidGunawan Profile Picture
    1,381 on 17 Jan 2017 at 03:29:34
    RE: Select Top in X++ code

    Hi,

    As you know that there is no select top x in the select statement in x++. However, you can do while select or query and put a variable as a counter in the loop. Then use if else to validate the counter and exit the loop once counter reach the limit.

    Example:

    int counter;

    int limit = 5;

    CustTable custTable;

    ;

    while select custTable

    order by custTable.AccountNum

    {

    counter++;

    if(counter>=limit) break;

    }

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,933 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,797 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Loading complete