Search code examples
c#sql-serverdapper

Dapper Parameter replace not working for Top


This is my sql

var maxLimit =100;
var sql = "Select Top @MaxLimit from Table WHere data =@Id"
conn.Query<Result>(sql, new  {
                Id = customerId,
                MaxLimit = maxLimit
            })

But I get a system error

incorrect syntax near @MaxLimit.

Is Dapper not able to parametrize fields like Top, or Fetch?


Solution

  • In SQL Server any top expression other than a numeric constant needs to be in parentheses.

    SELECT TOP (@MaxLimit) FROM ...