Active Query Builder support area

How to add or modify TOP, LIMIT, FETCH FIRST clauses or ROWNUM condition programmatically?

Last modified:


This article provides source code samples of adding or modifying server-specific row limiting clauses in the query.

Please note that there is a better way to cope with this task in general, especially if you want to implement pagination of result data in your application: QueryTransformer API.

The following samples apply to all SQL syntax providers.

Read current limit and offset values:

if (syntaxProvider.IsSupportLimitCount() ||
    syntaxProvider.IsSupportLimitOffset())
{
    var queryAst = sqlQuery.QueryRoot.ResultQueryAST;
        // for old AQB 2.x use the queryBuilder.ResultQueryAST property instead

    string offset;
    string count;

    if (syntaxProvider.GetQueryLimits(queryAst, out offset, out count))
    {
        MessageBox.Show("Offset:" + offset + "\nCount: " + count);
    }
}

Apply new values:

string offset = "10";
string count = "20";

if (syntaxProvider.IsSupportLimitCount() ||
    syntaxProvider.IsSupportLimitOffset())
{
    var queryAst = sqlQuery.QueryRoot.ResultQueryAST;

    var newQuery = syntaxProvider.TransformQueryLimits(queryAst, offset, count);

    var newQuerySql = newQuery.GetSQL();
    MessageBox.Show(newQuerySql);
}

Is this article helpful for you?