How to see the SQL generated by an X++ query in D365FO
When an X++ query returns unexpected rows, duplicates, or slow performance, the fastest way to debug it is to inspect the SQL that D365FO actually generates. This article shows the practical ways to print the query text and why the result is not always the same as the SQL you would write by hand.
How to see the SQL generated by an X++ query in D365FO#
If your X++ query is behaving strangely, the first thing to check is the query text D365FO is actually generating. In practice, that usually tells you whether the problem is a bad join, an unexpected range, a missing relation, or simply a query that is not doing what you thought it was doing.
The short version is:
query.toString()orqbds.toString()shows the X++ query representation.select generateonlyplusgetSQLStatement()can give you the generated SQL for a select statement.- The SQL you see is the one D365FO generates, not necessarily the SQL you would have written manually in SSMS.
Quick answer#
If you are debugging a form datasource query, start with:
info(someFormDataSource.query().toString());If you are debugging a select statement in a runnable class or job, use:
CustTable custTable;
select generateonly custTable
where custTable.AccountNum == 'US-001';
info(custTable.getSQLStatement());That gives you a much better picture of what the runtime is actually asking SQL Server to do. Microsoft documents the query object model and confirms that QueryRun executes the query, Query holds the definition, and QueryBuildDataSource defines each data source in that definition. Microsoft also documents generateonly and getSQLStatement() as the pattern for retrieving the generated SQL for a select statement. (learn.microsoft.com)
Why this matters#
A lot of D365FO query bugs are really query-shape bugs.
A query can look fine in X++ and still fail because:
- a relation is missing or disabled,
- a join is on the wrong datasource,
- a range is applied to the wrong level,
- a child datasource changes the result set in a way you did not expect,
- or the query is valid X++, but not the same thing you would hand-write in SQL.
Microsoft’s query documentation notes that a QueryBuildDataSource represents a single datasource, that child datasources create joins, and that sibling datasources can produce separate SQL statements. That is exactly why “it looks right in code” is not always enough. (learn.microsoft.com)
The most practical ways to inspect the query#
1) Print the query object#
For a query object or a form datasource, this is the fastest first check.
Query query = new Query();
QueryBuildDataSource qbds;
qbds = query.addDataSource(tableNum(CustTable));
qbds.addRange(fieldNum(CustTable, AccountNum)).value(queryValue('US-001'));
info(query.toString());
info(qbds.toString());This is usually enough to confirm whether the datasource tree and ranges are what you intended. Community answers and blog examples commonly use this pattern when troubleshooting query behavior in AX/D365FO, especially from forms. (community.dynamics.com)
2) Print the query from a form datasource#
When the problem is on a form, inspect the datasource query directly.
FormDataSource fds = MyFormDataSource;
info(fds.query().toString());If you want to inspect a specific datasource in the query tree:
info(fds.query().dataSourceNo(1).toString());This is useful when a form has multiple datasources and you need to see where the filter was actually applied. Microsoft’s query model documentation supports this structure, and community threads frequently use the same approach for debugging form datasource issues. (learn.microsoft.com)
3) Retrieve the generated SQL for a select#
If you need the SQL Server text, use generateonly and getSQLStatement().
CustTable custTable;
select generateonly custTable
where custTable.AccountNum == 'US-001';
info(custTable.getSQLStatement());That is the most direct way to see the SQL generated from a select statement in X++. A practical blog example shows the same pattern for troubleshooting query behavior in D365FO. (ariste.info)
Important limitation: this is still not hand-written SQL#
This is where people get tripped up.
The string returned by D365FO is the generated SQL, but it is still the product of the X++ query engine. That means it may not look like a query you would have written manually, especially when joins, exists join, notexists join, and form datasource relations are involved. Microsoft’s query documentation explains the query model, and community examples show that the generated SQL can differ significantly from the apparent X++ structure. (learn.microsoft.com)
So the goal is not to “translate everything into SSMS SQL.” The goal is to answer questions like:
- Which datasource is driving the result?
- Did my range land on the right table?
- Did the join become what I expected?
- Am I accidentally filtering out rows?
Common mistake: checking the wrong thing#
A frequent mistake is to print the wrong level of the query.
For example, if you are debugging a form, printing only the form datasource name or a top-level object will not tell you much. You need the query text or the relevant datasource node. The same applies when a query has nested datasources: the outer query may look fine while the problem sits in a child datasource or an incorrect link. Microsoft’s documentation on QueryBuildDataSource and query joins is the key reason for that behavior. (learn.microsoft.com)
A simple troubleshooting workflow#
When an X++ query looks wrong, I usually check it in this order:
- Print the query text.
- Confirm the correct datasource has the range.
- Confirm the join type.
- Confirm relations and manual links are not conflicting.
- If needed, use
generateonlyandgetSQLStatement()for the exact select.
That workflow is usually faster than guessing from the code alone.
Example: debugging a joined query#
Query query = new Query();
QueryBuildDataSource custDs;
QueryBuildDataSource dirPartyDs;
custDs = query.addDataSource(tableNum(CustTable));
dirPartyDs = custDs.addDataSource(tableNum(DirPartyTable));
dirPartyDs.relations(true);
dirPartyDs.addRange(fieldNum(DirPartyTable, Name)).value(queryValue('Contoso'));
info(query.toString());If the result is not what you expect, the printed query helps you see whether the child datasource was created as a join and whether the range is applied where you intended. Microsoft states that a child datasource creates a join between the two datasources, which is why this inspection step is so useful. (learn.microsoft.com)
When this is especially useful#
This technique is worth using when you are working on:
- form datasource filters,
- dynamic queries in classes or batch jobs,
- data entity source queries,
- query performance issues,
- and “why am I getting too many rows?” problems.
Those are all cases where the real problem is often not the X++ syntax itself, but the generated query shape. Microsoft’s data entity and query documentation both reinforce that datasources, relations, and ranges control how the runtime builds the query. (learn.microsoft.com)
Bottom line#
If a D365FO query is misbehaving, do not guess.
Print the query, inspect the datasource tree, and when you need the exact SQL text for a select, use generateonly with getSQLStatement(). That usually exposes the problem in seconds instead of after a long debugging session. (ariste.info)
Sources#
- X++ class library - Finance & Operations — Microsoft Learn
- QueryBuildDataSource Class (Dynamics.AX.Application) — Microsoft Learn
- Get the T-SQL query from X++ — ariste.info
- Get sql value of query. — Dynamics Community Forums
Missing something?
Suggest a topic you'd like to see covered next. The most requested topics help shape future XppForge documentation.
Sign in to suggest a topic