2013-08-26 115 views
0

這是我的PHP代碼:limit命令的SQL Server

require_once ('ConfigSQL.php'); 

$per_page = 10; 
if(isset($_GET['page'])) { 
    $page = ($_GET['page'] - 1); 
} 
else { 
    $page = 0; 
} 
    $QueryCharacter = mssql_query (" 
     SELECT Character.AccountID, Character.Name, Character.CtlCode, 
     AccountCharacter.Number, AccountCharacter.ID, 
     memb___id, memb_name, memb__pwd2, mail_addr 
     FROM Character, AccountCharacter, MEMB_INFO 
     WHERE Character.AccountID=AccountCharacter.ID AND 
     AccountID=memb___id AND AccountCharacter.ID=memb___id 
     LIMIT {$page},{$per_page} 
     "); 

$rows = mssql_fetch_row($QueryCharacter); 
$pages = $rows/$per_page; 
$pages = $pages ? ((int)$pages == $pages) : ((int)$pages + 1); 

而且我的錯誤:

Warning: mssql_query() [function.mssql-query]: message: Line 2: Incorrect syntax near ','. (severity 15) in C:\xampp\htdocs\Pages\EditCharacter.php on line 19

Warning: mssql_query() [function.mssql-query]: Query failed in C:\xampp\htdocs\Pages\EditCharacter.php on line 19

Warning: mssql_fetch_row(): supplied argument is not a valid MS SQL-result resource in C:\xampp\htdocs\Pages\EditCharacter.php on line 21

我知道這個問題是在我的SQL查詢中的 '限價'。

'SELECT'需要如何?

+0

SQL服務器[TOP支持(http://technet.microsoft.com/en-us/library/ms189463.aspx),不會限制 –

+0

什麼版本的SQL Server?在2012年,您可以使用OFFSET ... FETCH,而以前的版本分頁要複雜得多(使用CTE和ROW_NUMBER(),或者在前幾頁時使用TOP)。 SQL Server不支持LIMIT(我很高興它不是 - 它不完全是標準的)。 –

+0

可能重複的[SQL Server中的行偏移](http://stackoverflow.com/questions/187998/row-offset-in-sql-server) –

回答

0

試着改變你的查詢到這一點:

WITH paging AS (
    SELECT 
     ,Character.AccountID 
     ,Character.Name 
     ,Character.CtlCode 
     ,AccountCharacter.Number 
     ,AccountCharacter.ID 
     ,memb___id 
     ,memb_name 
     ,memb__pwd2 
     ,mail_addr 
     ,ROW_NUMBER() OVER (ORDER BY Character.AccountID) AS RowNr 
    FROM 
     Character, 
     AccountCharacter, 
     MEMB_INFO 
    WHERE 
     Character.AccountID = AccountCharacter.ID 
     AND AccountID=memb___id 
     AND AccountCharacter.ID=memb___id 
) 
SELECT TOP ({$per_page}) * 
FROM paging 
WHERE RowNr > {$page} * {$per_page} 
ORDER BY RowNr 

需要注意的是第0頁第一頁,1是第二等

這使用於2005年推出MSSQL通用表表達式,爲早期版本中,這樣的事情或許應該工作(來源Efficient Paging (Limit) Query in SQLServer 2000?):

DECLARE @Sort int 

SET ROWCOUNT {$page} * {$per_page} 
SELECT @Sort = AccountID FROM Character ORDER BY AccountID 

SET ROWCOUNT {$per_page}  
SELECT 
    ,Character.AccountID 
    ,Character.Name 
    ,Character.CtlCode 
    ,AccountCharacter.Number 
    ,AccountCharacter.ID 
    ,memb___id 
    ,memb_name 
    ,memb__pwd2 
    ,mail_addr 
    ,ROW_NUMBER() OVER (ORDER BY Character.AccountID) AS RowNr 
FROM 
    Character, 
    AccountCharacter, 
    MEMB_INFO 
WHERE 
    Character.AccountID > @Sort 
    AND Character.AccountID = AccountCharacter.ID 
    AND AccountID=memb___id 
    AND AccountCharacter.ID=memb___id 
ORDER BY 
    Character.AccountID 
+0

它不起作用,我在SQL 2000中使用 – Almog

+0

啊,2000很久以前:) ..我已經用我可以嘗試的其他東西更新了我的答案。 – TheQ