BUGFIX: Fixed limit/offset code in MSSQL

This commit is contained in:
Sam Minnee 2009-06-16 04:04:01 +00:00
parent bff10ffc99
commit 0585ef3908

View File

@ -1006,9 +1006,11 @@ class MSSQLDatabase extends Database {
} else { } else {
//could be a comma delimited string //could be a comma delimited string
$bits=explode(',', $sqlQuery->limit); $bits=explode(',', $sqlQuery->limit);
$limit=trim($bits[0]); if(sizeof($bits) > 1) {
if(isset($bits[1])) list($offset, $limit) = $bits;
$offset=trim($bits[1]); } else {
$limit = $bits[0];
}
} }
$text = ''; $text = '';
@ -1030,9 +1032,14 @@ class MSSQLDatabase extends Database {
// If there's a limit and an offset, then we need to do a subselect // If there's a limit and an offset, then we need to do a subselect
} else if($limit && $offset) { } else if($limit && $offset) {
$text = "SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY $sqlQuery->orderby)" if($sqlQuery->orderby) {
. " AS Number, "; $rowNumber = "ROW_NUMBER() OVER (ORDER BY $sqlQuery->orderby) AS Number";
$suffixText .= ") AS Numbered WHERE Number BETWEEN $offset AND " . ($offset+$limit) } else {
$firstCol = reset($sqlQuery->select);
$rowNumber = "ROW_NUMBER() OVER (ORDER BY $firstCol) AS Number";
}
$text = "SELECT * FROM ( SELECT $rowNumber, ";
$suffixText .= ") AS Numbered WHERE Number BETWEEN " . ($offset+1) ." AND " . ($offset+$limit)
. " ORDER BY Number"; . " ORDER BY Number";
$nestedQuery = true; $nestedQuery = true;