General X++ Development

What is the difference between `while select` and `QueryRun` in X++?

Asked by Sunny Vasty· Aug 13, 2026· 1 answers

What is the difference between while select and QueryRun in X++?

SSunny Vasty75 reputation · answered Aug 13, 2026

Use while select when the query is known at compile time and the structure is straightforward.

Use QueryRun when you need to build or change the query dynamically.

X++
Query query = new Query();
QueryBuildDataSource qbds = query.addDataSource(tableNum(CustTable));
QueryRun queryRun = new QueryRun(query);

while (queryRun.next())
{
    CustTable custTable = queryRun.get(tableNum(CustTable));
}

If the query is static, I normally keep it simple and use while select. If the user or runtime logic is changing datasources, ranges, or joins, QueryRun makes more sense.