Skip to content

Commit a1faa83

Browse files
committed
Mssql: Use OFFSET n ROWS FETCH NEXT n ROWS ONLY for LIMIT OFFSET
1 parent dd1e467 commit a1faa83

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

src/QueryBuilder.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -633,12 +633,24 @@ public function buildLimitOffset($limit = null, $offset = null)
633633
{
634634
$sql = [];
635635

636-
if ($limit !== null) {
637-
$sql[] = "LIMIT $limit";
638-
}
636+
if ($this->adapter instanceof Mssql) {
637+
if ($offset !== null || $limit !== null) {
638+
// If offset is null, sprintf will convert it to 0
639+
$sql[] = sprintf('OFFSET %d ROWS', $offset);
640+
}
639641

640-
if ($offset !== null) {
641-
$sql[] = "OFFSET $offset";
642+
if ($limit !== null) {
643+
// FETCH FIRST n ROWS ONLY for OFFSET 0 would be an alternative here
644+
$sql[] = "FETCH NEXT $limit ROWS ONLY";
645+
}
646+
} else {
647+
if ($limit !== null) {
648+
$sql[] = "LIMIT $limit";
649+
}
650+
651+
if ($offset !== null) {
652+
$sql[] = "OFFSET $offset";
653+
}
642654
}
643655

644656
return implode($this->separator, $sql);

0 commit comments

Comments
 (0)