Skip to main content

Notifications

Announcements

No record found.

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

  • RE: Convert Select statement to Query

    Ok thank you all the query done and successful.

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

  • RE: Convert Select statement to Query

    @Girish S

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

  • RE: Convert Select statement to Query

    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.

  • Verified answer
    GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    RE: Convert Select statement to Query

    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.

  • Verified answer
    André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 292,162 Super User 2025 Season 1 on at
    RE: Convert Select statement to Query

    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
    Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,587 Super User 2024 Season 1 on at
    RE: Convert Select statement to Query

    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?

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,962 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans