Syntax error near @Top

T_Sql query statement
 
string sql = "select top @Top Id, PublishHouse from T_Publishers";
 
SqlParameter para = new SqlParameter("@Top", Top);
 
 
-> At runtime, an error is reported: Syntax error near @Top
 
-> Reason: @Top is a query condition, not a column in the table
 
-> Solution: replace @Top with string concatenation
 
StringBuilder strSql = new StringBuilder();
strSql.Append("select ");
if (Top > 0)
{
    strSql.Append(" top " + Top.ToString());
}
strSql.Append(" Id, PublishHouse");
 
strSql.Append(" from T_Publishers");

 

More exchanges; q619267707

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325547600&siteId=291194637