Debugging queries (kind of)

The Query objects in Dynamics AX are very flexible and powerful tools. Unfortunately they tend to have quite a steep learning curve and debugging aren’t always as easy as with select statements. Because of that stuff that could benefit from using the query objects are often written as select statements.

The problem in regards to debugging is that you add all sorts a datasources and ranges, maybe even joining the datasources and at the end you wonder how the resulting SQL actually looks. So here is a short tip on how you can get valuable debugging information twisted out of your queries.

If you call the toString method of the first datasource in query object you will actually get a string with the SQL statement that corresponds the query object. Example:

 static void debugQuery(Args _args)
    Query           query = new Query();

    query.addDataSource(tableNum(CustTable)).addRange(fieldNum(CustTable, CustGroup)).value('XYZ');

    info (query.dataSourceNo(1).toString());

Note: The SQL statement string you see is not the actual statement sent to the database. That would have all sorts of lower level stuff in it.

This posting is provided "AS IS" with no warranties, and confers no rights.