Skip to content


DBAL-940: ORDER BY with LIMIT in SQL Server does not work correctly #2184

doctrinebot opened this Issue · 4 comments

2 participants


Jira issue originally created by user mklocke:

The function doModifyLimitQuery() in Doctrine\DBAL\Platforms\SQLServerPlatform does work correctly.

It removes the user generated ORDER BY (gets moved to OVER clause), but does not apply an ORDER BY on the row number created with ROW_NUMBER().

$orderBy = stristr($query, 'ORDER BY');

//Remove ORDER BY from $query (including nested parentheses in order by list).
$query = preg_replace('/\s<ins>ORDER\s</ins>BY\s<ins>([<sup>()]</ins>](\((?:(?:(?>[</sup>())<ins>)|(?R))*)\))</ins>/i', '', $query);

$format  = 'SELECT * FROM (%s) AS doctrine*tbl WHERE doctrine*rownum BETWEEN %d AND %d';

The last format string should be:

$format  = 'SELECT * FROM (%s) AS doctrine*tbl WHERE doctrine_rownum BETWEEN %d AND %d ORDER BY doctrine*rownum';

Comment created by @ocramius:

Possible relation with DBAL-927


Issue was closed with resolution "Fixed"


Comment created by @deeky666:

Fixed as of 040d49c

@doctrinebot doctrinebot added the Bug label
@deeky666 deeky666 was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.5 milestone
@doctrinebot doctrinebot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.