2011-07-03 40 views
-1

我運行一個簡單的查詢得到最高5000行的一些表:SQL Server 2000中:起牀前N行

SELECT TOP 5000 
     accountid, account, accountmanagerid, mainphone, alternatephone, fax, email, webaddress, createuser, modifyuser, createdate, modifydate, addressid, shippingid 
    FROM sysdba.account 
    WHERE 1 = 1 
    AND 1 = 1 
ORDER BY accountid asc 

這完美地工作的SQL Server 2008上,但不幸的是,現在我發現自己想運行這個以前的工作腳本對SQL Server 2000.它似乎拒絕頂部的命令。

我該怎麼做才能在此版本的sql server上找回我的5000行?

+2

根據[MSDN文檔(http://msdn.microsoft .com/en-us/library/aa259187%28v = sql.80%29.aspx),TOP在2000年有效...... – 2011-07-03 20:34:36

+0

Keoki Zee是正確的 - 在SQL Server 2000中啓動了「TOP」支持。提供比「似乎拒絕命令」更多的細節。 IME,即使在撥回兼容級別後,我仍然可以使用後來支持的功能。 –

+3

從SQL Server 2005可以在TOP語句中使用表達式/變量。在SQL Server 2000中,它必須是一個整數值。您是否使用變量來指定值5000?如果你是,你可以改用SET ROWCOUNT。 –

回答

3

TOP關鍵字在SQL Server 2000中不可用。但是,您可以使用rowcount來過濾頂級記錄。您可以修改如下上面的查詢:


-- 1=1 is NOT needed unless you are appending this string as dynamic SQL 

SET ROWCOUNT 50 
SELECT 
     accountid, account, accountmanagerid, mainphone, alternatephone, fax, email, webaddress, createuser, modifyuser, createdate, modifydate, addressid, shippingid 
    FROM sysdba.account 
ORDER BY accountid asc 

+1

您的解決方案的工作,但是我只是檢查TOP在SQL Server 2000中有效 –

2

沒有熱門關鍵詞你也可以得到這個問題的解決方案爲後續

select * from 
(
    select t_1.*,rownum c_1 from 
    (
    select accountid,account,accountmanagerid,mainphone,alternatephone, 
     fax,email,webaddress,createuser, modifyuser,createdate,modifydate,addressid,hippingid 
    FROM sysdba.account  
    WHERE 1 = 1-- whatever condition 
    AND 1 = 1 -- condition 2 
    ORDER BY accountid asc 
)t_1 
) 
where c_1 <5000 
order by c_1