php - PDO SQLSRV Using bindValue for SELECT TOP -


i'm trying use pdo sqlsrv select data table limit (top). however, when this.

$limit = 20;  $sql = "select top :rowslimit * table order id desc"; $query = $this->db->prepare($sql); $parameters = array(':rowslimit' => $limit);  $query->execute($parameters); 

i error this.

warning: pdostatement::execute(): sqlstate[42000]: syntax error or access violation: 102 [microsoft][odbc driver 11 sql server][sql server]incorrect syntax near '@p1'. 

i tried removing paremeters , adding bindvalue instead, same error occurs either of these.

$query->bindvalue(':rowslimit', (int) trim($limit), pdo::param_int); 

or

$query->bindvalue(':rowslimit', intval(trim($limit)), pdo::param_int); 

so how can bind parameter top in pdo sqlsrv?

you can't use parameters top value, there workaround issue.

what need use row_number() over() syntax , manually filter out top @x rows.

see sqlfiddle example.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -