在 MSSQL 数据库驱动中,“_limit”函数无效,所以要修改“mssql_driver.php”这个文件。
修改文件“system/database/drivers/mssql/mssql_driver.php”中的“_limit”函数为:
function _limit($sql, $limit, $offset)
{
$cursorName = 'mi_cursor_'.md5(strtolower($sql));
$sql = "DECLARE ".$cursorName." CURSOR DYNAMIC READ_ONLY FOR
".$sql."
OPEN ".$cursorName."
DECLARE @CURSOR AS INT
SELECT @CURSOR = CURSOR_HANDLE FROM MASTER.DBO.SYSCURSORS
WHERE CURSOR_NAME = '".$cursorName."'
EXEC SP_CURSORFETCH @CURSOR, 32, ".($offset+1).", ".$limit."
EXEC SP_CURSORCLOSE @CURSOR";
return $sql;
}
此方法sqlserver 2005测试不通过。
参考以下网址再解决
http://www.cnblogs.com/didasoft/archive/2007/07/20/SqlServer-Paging.html
function _limit($sql, $limit, $offset)
{
//$i = $limit + $offset;
//return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
// echo $sql;
$str=strtolower(str_replace("\n",' ',$sql));
$strtemp=substr($str,strpos(strtolower($str),'from ')+5);
$tablename=substr($strtemp,0,stripos($strtemp,' '));
$orderby=substr($str,strpos(strtolower($str),'order by ')+9);
// $orderby=substr($str,strpos(strtolower($str),'where')+9);
if(strpos(strtolower($str),'where ')){
$where=substr($str,strpos(strtolower($str),'where ')+6, strpos(strtolower($str),'order by ')-strpos(strtolower($str),'where ')-6);
if($where)$where='where '.$where;
}
// $orderby=substr($strtemp,0,stripos($strtemp,' '));
// echo $str;
$sql = "
SELECT *
FROM
( SELECT *, ROW_NUMBER() OVER (ORDER BY $orderby) as NroFila
FROM $tablename $where
) AS PAG_TABLEXXX
WHERE
NroFila BETWEEN ".($offset?$offset+1:1)." AND ".($offset+$limit)."
";
return $sql;
}