Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 230
Justin Homes Avatar asked Apr 29 '16 20:04

Justin Homes


2 Answers

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

SELECT TOP (@MaxLimit) FROM ...
like image 88
Martin Smith Avatar answered Nov 06 '22 12:11

Martin Smith


Newer versions of dapper have literal replacements and they work great in this case:

var sql = "Select Top {=MaxLimit} from Table WHere data = @Id";
like image 14
Lars Avatar answered Nov 06 '22 11:11

Lars