Microsoft DynamicsFree generatorPDF, Excel, Word exportMicrosoft Dynamics AX 2012All Microsoft Dynamics AX 2012 tools

Dynamics AX 2012 SQL Reporting Query Generator: Sales Lines, Enums and Table Numbers

This generator writes read-only T-SQL for a Dynamics AX 2012 R2 or R3 business database: sales lines with the SalesStatus enum decoded to labels and customer names from the global address book, filtered by DATAAREAID and PARTITION. A second query lists table numbers and field IDs from SQLDICTIONARY.

Fill in

5637144576 is the default partition in R2 and R3.

1 = Open order, 2 = Delivered, 3 = Invoiced, IN (...) = all.

Generated code
Download:

Query 1: sales order lines with status decoded

SELECT st.SALESID,
       st.CUSTACCOUNT,
       dpt.NAME AS CUSTOMERNAME,
       sl.LINENUM,
       sl.ITEMID,
       sl.SALESQTY,
       sl.REMAINSALESPHYSICAL,
       sl.SHIPPINGDATEREQUESTED,
       CASE sl.SALESSTATUS
            WHEN 1 THEN 'Open order'
            WHEN 2 THEN 'Delivered'
            WHEN 3 THEN 'Invoiced'
            WHEN 4 THEN 'Canceled'
            ELSE 'None'
       END AS SALESSTATUSLABEL
FROM SALESTABLE st
JOIN SALESLINE sl
  ON sl.SALESID = st.SALESID
 AND sl.DATAAREAID = st.DATAAREAID
 AND sl.PARTITION = st.PARTITION
LEFT JOIN CUSTTABLE ct
  ON ct.ACCOUNTNUM = st.CUSTACCOUNT
 AND ct.DATAAREAID = st.DATAAREAID
 AND ct.PARTITION = st.PARTITION
LEFT JOIN DIRPARTYTABLE dpt
  ON dpt.RECID = ct.PARTY
 AND dpt.PARTITION = ct.PARTITION
WHERE st.DATAAREAID = 'usmf'
  AND st.PARTITION = 5637144576
  AND st.CREATEDDATETIME >= '2026-01-01'
  AND sl.SALESSTATUS = 1
ORDER BY st.SALESID, sl.LINENUM;

Query 2: table numbers (table IDs) from SQLDICTIONARY

-- FIELDID = 0 rows describe the table itself
SELECT TABLEID, NAME, SQLNAME
FROM SQLDICTIONARY
WHERE FIELDID = 0
  AND NAME LIKE 'SALES%'
ORDER BY NAME;

-- fields of one table (FIELDID <> 0)
SELECT TABLEID, FIELDID, NAME, SQLNAME
FROM SQLDICTIONARY
WHERE TABLEID = 366
  AND FIELDID <> 0
ORDER BY FIELDID;

Read-only reporting only: never insert, update or delete AX tables in SQL, because AX caches, RecId allocation and business logic are bypassed. Verify on your database: the PARTITION column exists from AX 2012 R2 (remove the partition lines on RTM or Feature Pack), the default partition RecId in your PARTITIONS table, and the table ID you query (look it up with Query 2 first; the 366 default is only a placeholder). CREATEDDATETIME is stored in UTC; confirm the column exists on SALESTABLE in your database (it depends on the table's CreatedDateTime property) or filter on another date. DIRPARTYTABLE is a shared table and has no DATAAREAID.

Need a reporting copy of AX 2012 people can query safely?

Tell us your AX version and reporting needs, and we will outline a read-only reporting setup and the key queries.

Optional. The tool and every export work without this. No spam.

Was this tool useful?

How to use it

Enter the company code as stored in DATAAREAID (lower case in AX 2012, for example usmf), the partition RecId, a creation date and the status filter. Run Query 1 against a reporting copy or with a read-only login. Use Query 2 when you need a table's ID, for example to decode REFTABLEID on DOCUREF (document attachments), or to list a table's fields and their SQL column names before building a report.

  • Always filter DATAAREAID and PARTITION on company-specific tables
  • Join on DATAAREAID and PARTITION as well as the business key
  • Enums are integers: decode them in a CASE
  • utcDateTime fields are UTC; convert for local reporting

Where this lives in AX 2012

The business database (often named MicrosoftDynamicsAX) holds one SQL table per AOT table, with upper-case column names. Company-specific tables carry DATAAREAID; from R2 every table carries PARTITION, and the default partition is created with RecId 5637144576. SQLDICTIONARY is the kernel's map of AOT tables and fields to SQL: rows with FIELDID 0 are table definitions, other rows are fields. In AX 2012 the customer name is not on CUSTTABLE; CUSTTABLE.PARTY points to DIRPARTYTABLE.RECID in the global address book.

Worked example

For company usmf, default partition, orders created since 2026-01-01 and status 1, Query 1 returns open order lines with REMAINSALESPHYSICAL showing what is still to deliver and SALESSTATUSLABEL 'Open order'. To check which table ID 'SALESLINE' has on your system, set the pattern to SALESLINE and run Query 2; then put that ID in the table ID field to list its fields. Use the IDs you find, not IDs copied from another installation, because custom and ISV tables differ per system.

Version differences and gotchas

SalesStatus values are None 0, Open order (Backorder) 1, Delivered 2, Invoiced 3, Canceled 4. AX 2012 RTM and Feature Pack have no PARTITION column; R2 and R3 do, and most installations use only the default partition. Table IDs for standard tables are stable within a version, but custom and ISV tables get installation-specific IDs, so always look them up. Direct SQL ignores AX security and record-level policies, so restrict who can run these reports.

Frequently Asked Questions

How do I find Dynamics AX table numbers?

Query SQLDICTIONARY in the business database: SELECT TABLEID, NAME, SQLNAME FROM SQLDICTIONARY WHERE FIELDID = 0. Each row is one table with its ID. In X++ use tableNum(TableName) or tableId2Name(id). IDs for custom tables vary between installations, so look them up rather than copying.

What is PARTITION 5637144576 in AX 2012?

AX 2012 R2 introduced data partitions, adding a PARTITION column to every table. 5637144576 is the RecId of the default partition, which most installations use. Filter and join on PARTITION in SQL reports so results do not mix partitions if more are ever created.

How are enum values stored in the AX database?

As integers. SalesLine.SalesStatus stores 1 for Open order, 2 for Delivered, 3 for Invoiced and 4 for Canceled. Decode them with a CASE expression or a lookup table. The label text shown in the client comes from the enum's labels in the AOT and can differ by language.

Why does CUSTTABLE have no NAME column in AX 2012?

AX 2012 moved names and addresses to the global address book. CUSTTABLE.PARTY holds the RecId of a DIRPARTYTABLE row, which holds NAME. DIRPARTYTABLE is shared across companies, so join it on RECID and PARTITION, not DATAAREAID.

If you want users to ask AX 2012 for open orders or table details in plain English, answered from a read-only copy on your own servers, we can build that layer.

ArchRay builder
Design the on-prem AI stack for Microsoft Dynamics AX 2012: model, GPUs, RAG pipeline, guardrails
See memory, speed and cost as you swap parts, then export the diagram. Free, in your browser.
Open the builder

Keep exploring

Free tools

ERP fixes and how-tos

AI on your ERP

Integrations

Software