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

Joining a shared table and a non-shared table in SQL for AX 2009

(0) ShareShare
ReportReport
Posted on by 315

How do I write a SQL statement that joins a shared table, InventTable, and a non-shared table, ProdTable? Since the DataAreaId of the shared table is now the virtual company, XVC, and the DataAreaID of the non-shared table is one of the companies contain within the virtual company, XXX

Before creating a virtual company my SQL statement would look like: 

  SELECT *

  FROM PRODTABLE LEFT OUTER JOIN INVENTTABLE  on PRODTABLE.itemid = INVENTTABLE .itemid and PRODTABLE.dataareaid = INVENTTABLE .dataareaid 

I am new to the virtual company concept and how databases handle it so please fill me in on anything I'm missing.

Thanks in advance for any help

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aarin McNabb Profile Picture
    10 on at

    As you mentioned in the question, the shared table will have DataAreaId values of the virtual company id rather than an actual company id.

    If you need to be somewhat dynamic and span multiple companies and potentially multiple virtual companies, you can make the query more complex and lookup the virtual company id by table collection name.

    The virtual company to actual company mapping can be seen on the SQL side in the VIRTUALDATAAREALIST table.  

    SELECT * FROM VIRTUALDATAAREALIST ORDER BY VIRTUALDATAAREA

    The Table Collection associations to Virtual Companies can be found in the TABLECOLLECTIONLIST table.

    SELECT * FROM TABLECOLLECTIONLIST

    Putting this together with the query in your question could look something like this (untested):

    SELECT *

    FROM PRODTABLE

    LEFT OUTER JOIN INVENTTABLE ON PRODTABLE.ITEMID = INVENTTABLE.ITEMID

    AND (

    PRODTABLE.DATAAREAID = INVENTTABLE.DATAAREAID

    OR

    INVENTTABLE.DATAAREAID = (

    SELECT VIRTUALDATAAREALIST.VIRTUALDATAAREA

    FROM VIRTUALDATAAREALIST

    INNER JOIN TABLECOLLECTIONLIST ON VIRTUALDATAAREALIST.VIRTUALDATAAREA = TABLECOLLECTIONLIST.VIRTUALDATAAREA

    WHERE ID = PRODTABLE.DATAAREAID

    AND TABLECOLLECTIONLIST.TABLECOLLECTION = '<SHARED_TABLE_TABLE_COLLECTION_NAME>'

    )

    )

    This probably isn't the best approach in terms of performance, but it's only meant to provide an example of how the data is handled.  In most cases, I would assume this could be simplified to just include the company id(s), both virtual and actual, that you're concerned with.

    A more common approach may be to limit the query with a where clause with at least DataAreaId.  With that, you could select and store the related InventTable DataAreaId before-hand, then use that parameter in the later select statement to avoid the repeated sub-selects.

    Yet another approach could be to avoid using InventTable.DataAreaId altogether, depending upon your data and your system.  It is possible to adjust table indexes such that DataAreaId is not the leftmost column if your exact scenario warrants.

  • SquirrelChaser Profile Picture
    106 on at

    You can just skip the DataAreaId link, assuming you don't have any old pre-virtualization data in the InventTable.

    SELECT *  FROM PRODTABLE LEFT OUTER JOIN INVENTTABLE  on PRODTABLE.itemid = INVENTTABLE .itemid

    You may want to add a WHERE statement to limit the results to one company based on the PRODTABLE DataAreaId.

  • Matt Krumpelbeck Profile Picture
    315 on at

    The answer suggested by Microsoft is explained in this white paper: www.microsoft.com/.../details.aspx

    They want you to create a view for each virtualized table containing a row for each company so that data integrity stays intact. This essentially doubles, triples, quadruples, etc (depends on how many companies/data area id's you have).. your dataset and thus kills performance. But it is the easiest way to alter code since you only have to change the table references to now reference the new views and again integrity stays in place.

    I'm interested in testing Aarin's suggestion since it can't be any worse on performance than what Microsoft suggests doing.

     

    SquirrelChaser -  DataAreaId is part of the primary key in all tables. Although in some small select statements this may work, in the long run it will hurt you more than help you. Performance and integrity wise.

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

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans