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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Convert Select statement to Query

(0) ShareShare
ReportReport
Posted on by

Hi everybody...
I want to self join the inventsum table using select statement there was no problem but when i try to convert it to query there was an error. 

while select itemId,InventLocationId,InventDimId,DataAreaId,AvailPhysical from inventSum
            where inventSum.inventLocationId != ""
            exists join itemId,InventLocationId,AvailPhysical from inventSum1 
                where inventSum1.ItemId           == inventSum.itemid
                   && inventSum1.InventLocationId == 11
                   && inventSum1.AvailPhysical    <= 100

I wrote this code but there was an error after execution.

query = new Query();
        qbds  = query.addDataSource(tableNum(InventSum));
        qbr  = qbds.AddRange(fieldNum(InventSum,InventLocationId));
        qbr.value(SysQuery::valueNotEmptyString());
         
        qbds1 = qbds.addDataSource(tableNum(InventSum));
        qbds1.relations(true);
        qbds1.joinMode(JoinMode::ExistsJoin);
        qbr1 = qbds1.addRange(fieldNum(InventSum,InventLocationId));
        qbr2 =qbds1.addRange(fieldNum(InventSum,AvailPhysical));
        qbr1.value('11');
        qbr2.value(strFmt('(AvailPhysical <= "%1")',100));

any help please

I have the same question (0)
  • Verified answer
    Bharani Preetham Peraka Profile Picture
    3,634 Moderator on at

    Remove the relations to true. Here you need to add a link between the 2 tables like recid relation. Also try debugging by returning the query or giving the query in infolog so that you can understand where it is missing or getting error.

    And what is the error you are getting?

    But can't we use a single query without self join? Like with a single select statement with all the said parameters from another buffer variable?

  • Verified answer
    André Arnaud de Calavon Profile Picture
    303,247 Super User 2026 Season 1 on at

    Hi 365 financial,

    It would be helpful if you provide the error message when you encounter an issue. Now we are guessing where to focus. The range you set for the field AvailPhysical can be done in another way. You can use a filter like '0..100' instead of the SQL statement between brackets.

  • Verified answer
    GirishS Profile Picture
    27,833 Moderator on at

    Hi 365 financial,

    What is the purpose of this self join? What error you got after running this?

    Don't make the relation to true. You are joining the same parent table not the child table. So you must specify the link between the tables using addLink.

    query = new Query();
            qbds  = query.addDataSource(tableNum(InventSum));
            qbr  = qbds.AddRange(fieldNum(InventSum,InventLocationId));
            qbr.value(SysQuery::valueNotEmptyString());
             
            qbds1 = qbds.addDataSource(tableNum(InventSum));
            qbds1.relations(false);
            qbds1.addLink(fieldNum(InventSum. ItemId),fieldnum(InventSum, ItemId));
            qbds1.joinMode(JoinMode::ExistsJoin);
            qbr1 = qbds1.addRange(fieldNum(InventSum,InventLocationId));
            qbr2 =qbds1.addRange(fieldNum(InventSum,AvailPhysical));
            qbr1.value('11');
            qbr2.value(strFmt('(AvailPhysical <= "%1")',100));

    Thanks,

    Girish S.

  • 365 financial Profile Picture
    on at

    hi all thanls for replay

    this is the error

    Fault was thrown by the service for request 44964947-a8df-429f-b9be-4022ff7c6a37. Exception details:

    Type: Microsoft.Dynamics.Ax.Xpp.ErrorException

    Message: Cannot select a record in On-hand inventory (InventSum).

    The SQL database has issued an error.

  • 365 financial Profile Picture
    on at

    @Girish S

    the error was thrown after i make the relation false and added link as you mentioned

  • 365 financial Profile Picture
    on at

    Ok thank you all the query done and successful.

    i convert the relation to false and change the parameter value of availphysical to ..100

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 545 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 408

#3
Adis Profile Picture

Adis 267 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans