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?
In SQL Server any top
expression other than a numeric constant needs to be in parentheses.
SELECT TOP (@MaxLimit) FROM ...
Newer versions of dapper have literal replacements and they work great in this case:
var sql = "Select Top {=MaxLimit} from Table WHere data = @Id";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With