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.
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.
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.
Related Tools
Dynamics AX 2012 X++ Table Methods Reference
The xRecord methods you override or call on an AX 2012 table, when the kernel calls each one, and the find/exist conventions, with tips that keep set-based operations fast.
Microsoft DynamicsDynamics AX 2012 AIF Inbound Sales Order Message Generator (File System Adapter)
Fill in customer, PO reference, item, quantity, site and warehouse to get a ready-to-drop AIF envelope for SalesOrderService create, with the AIF batch tasks and troubleshooting steps.
Microsoft DynamicsAX 2012 Table to D365 Data Entity Reference
Look up the D365 F&O data entity and OData collection that corresponds to a Dynamics AX 2012 table when planning data migration.
Microsoft DynamicsDynamics AX 2012 R3 Safety Stock Journal Minimum Calculator
Reproduces both Calculate proposal methods of the AX 2012 safety stock journal so you can check a proposed minimum before posting it to item coverage.
More Microsoft Dynamics tools
See all- AX 2012 to D365 Finance and Operations Upgrade Effort Estimator
- Dynamics AX Customization Inventory Worksheet
- Dynamics AX 2012 Retirement Project Charter Template
- AX 2012 to D365 Data Entity Mapping Worksheet
- AX 2012 Customization Remediation Effort Calculator
- D365 F&O Manufacturing Data Entities Reference
- D365 F&O OData URL Builder
- D365 Production Order Status Reference
- D365 Data Management Import Worksheet Template
Keep exploring
Free tools
- Dynamics AX 2012 Private AI Layer Blueprint GeneratorFill in your AX 2012 environment and get an architecture plan for a private AI assistant, with components, controls, a 3-phase rollout and open questions for IT.
- Dynamics AX Customization Inventory WorksheetEditable inventory of AX customizations with layer, disposition and rework estimate per object, totaled for upgrade planning.
- Dynamics AX 2012 AI Assistant Sizing and Value CalculatorSize the GPUs, retrieval index and monthly value of a private AI assistant for AX 2012 from your user count and question volume.
ERP fixes and how-tos
- Adding a Private AI Layer to Dynamics AX 2012 On-PremAdd AI to Dynamics AX 2012 on-prem without moving data: SQL replica, AIF services, retrieval index, local LLM, mirrored AX roles, approved write-back.
- Planning an AX 2012 to Dynamics 365 Finance and Operations upgradeHow an AX 2012 to Dynamics 365 Finance and Operations upgrade actually runs: LCS upgrade project, code upgrade of customizations, and data migration, with the main risks.
- Fixing "The CIL object was not found" in Dynamics AX 2012AX 2012 throws CIL object not found after code changes. What CIL generation actually does, why incremental CIL misses it, and the full rebuild fix.
Integrations
- How to add AI agents to Microsoft Dynamics AX 2012How to add AI agents to Dynamics AX 2012 using AIF services, SQL read paths and approval gates, without a forced move to Dynamics 365 first.
- How to add an AI chatbot to Microsoft Dynamics AX 2012Add an AI chatbot to Dynamics AX 2012 for planners, buyers and customers, grounded in ERP data and able to run fully on-prem with open models.