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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

query of multiple tables join

(0) ShareShare
ReportReport
Posted on by 1,559

Hi Friends,

I try to build a query to join multiple tables. But, get a strange problem.  I run code to the line of 30 . But, in debug, I can see query like this, it's joined on different field.(AttributeGroup = Product), this is not right. But, where is the problem?

Many thanks,

SELECT * FROM WrkCtrTable(WrkCtrTable_1) 
WHERE ((WrkCtrId = N'1101')) 
JOIN * FROM SequencingGroupAttribute(SequencingGroupAttribute_1) 
ON WrkCtrTable.SequencingAttributeGroup = SequencingGroupAttribute.AttributeGroup 
JOIN * FROM SequencingProductAttributeValue(SequencingProductAttributeValue_1) 
ON SequencingGroupAttribute.AttributeGroup = SequencingProductAttributeValue.Product

Here is code:

    Query                   query = new Query();
    QueryRun                queryRun;

    WrkCtrTable             wrkCtrTable;    
    SequencingProductAttributeValue  SequencingProductAttributeValue;
    SequencingAttributeValue         SequencingAttributeValue;
    
    QueryBuildDataSource    qbdsMachineAttributeGroup;
    QueryBuildDataSource    qbdsAttributeGroup;
    QueryBuildDataSource    qbdsGroupAttribute;
    QueryBuildDataSource    qbdsProducAttributes;
    QueryBuildDataSource    qbdsAttributeValues;
    QueryBuildDataSource    qbdsChangeOverMatrix;
    QueryBuildRange         qbrMachine;
    QueryBuildRange         qbrProduct;
    ;

    qbdsMachineAttributeGroup = query.addDataSource(tableNum(WrkCtrTable));
    qbrMachine = qbdsMachineAttributeGroup.addRange(fieldNum(WrkCtrTable, WrkCtrId));
    //qbrMachine.value(queryValue(wrkCtrTable.WrkCtrId));
    qbrMachine.value('1101');


    qbdsGroupAttribute = qbdsMachineAttributeGroup.addDataSource(tableNum(SequencingGroupAttribute));
    qbdsGroupAttribute.joinMode(JoinMode::InnerJoin);
    qbdsGroupAttribute.addLink(fieldNum(SequencingGroupAttribute, AttributeGroup), fieldNum(WrkCtrTable, SequencingAttributeGroup));
    
    qbdsProducAttributes = qbdsGroupAttribute.addDataSource(tableNum(SequencingProductAttributeValue));
    qbdsProducAttributes.joinMode(JoinMode::InnerJoin);
    qbdsProducAttributes.addLink(fieldNum(SequencingProductAttributeValue, Attribute), fieldNum(SequencingGroupAttribute, Attribute));
   
    //qbrProduct = qbdsProducAttributes.addRange(fieldNum(SequencingProductAttributeValue, Product));
    //qbrProduct.value(queryValue(SequencingProductAttributeValue.Product));
    //qbrProduct.value('22565424963');
    
    qbdsProducAttributes.joinMode(JoinMode::InnerJoin);
    qbdsProducAttributes.addLink(fieldNum(SequencingProductAttributeValue, Attribute), fieldNum(SequencingGroupAttribute, Attribute));
    
    qbdsAttributeValues = qbdsProducAttributes.addDataSource(tableNum(SequencingAttributeValue));
    qbdsAttributeValues.relations(true);
    
    queryRun  = new queryRun(query);

   while(queryRun.next())

   {

   SequencingAttributeValue   = queryRun.get(tablenum(SequencingAttributeValue));


   info(strfmt("%1",SequencingAttributeValue.Value));

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    238,149 Most Valuable Professional on at

    You're getting wrong field IDs because the order of arguments in addLink() is wrong. Change it to qbdsProducAttributes.addLink(fieldNum(SequencingGroupAttribute, Attribute), fieldNum(SequencingProductAttributeValue, Attribute));.

  • Ganriver1 Profile Picture
    1,559 on at

    Hi Martin,

    It fixed problem, not sure why, the first join is in same sequence, but, it's ok.

    Also, what's difference when you use only one QueryBuildDatasouce versus multiple QueryBuildDatasources

    Thanks,

  • Suggested answer
    Martin Dráb Profile Picture
    238,149 Most Valuable Professional on at

    What fieldNum() returns is the field number, therefore addLink() gets arguments such as 5, 8. It knows nothing about to which table they belong. It will take field number 5 from the parent table and field number 8 from the child table.

    Of course, if both IDs are the same, it doesn't technically matter which order you use (but using the wrong order is a logical error anyway).

    I'm not sure what you mean by your other question. You use more data sources if you have a query with more than one table.

  • Ganriver1 Profile Picture
    1,559 on at

    I saw people use one data source for more than one table, that's why I ask.

  • Martin Dráb Profile Picture
    238,149 Most Valuable Professional on at

    You can assign another value to a variable if you want, if you don't have to access the previous value anymore. It's nothing specific to QueryBuildDataSource class.

    Nevertheless if you use descriptive names of variables (as you should), you can't suddenly start using a variable for something else. Reusing the same variable for different things is also often considered a bad practice.

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

News and Announcements

Season of Giving Solutions is Here!

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
TAHER Mehdi Profile Picture

TAHER Mehdi 3

#2
Nakul Profile Picture

Nakul 2

#2
Mea_ Profile Picture

Mea_ 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans