上周﹐被一位網友問到在sql2000中,想選取一張表的第N條記錄怎麼寫?(即如下語句select ID,Name,Title from WebSite order by ID desc 如何改寫以達到目的)
我當時直接就給他回
select ID,Name,Title from WebSite where ID= N
最后這位老兄說如果是ID是連續的話就可以,如果有記錄刪除的話,ID不連續了,就不行了 ,至此才明白他的真正意思。剛好要輪到公司的周會時間﹐于是想了一下。給出如下答案給他﹕
-- select top 1 [ID],[Name,Title from WebSite
-- where not [ID] in (select top 11 [ID] from WebSite order by [ID] asc) order by [ID] asc
Declare @strSql Varchar(200),@intTopN int
Set @intTopN = 11
Set @strSql = 'select top 1 [ID],[Name,Title from WebSite where not [ID] in (select top '+Cast(@intTopN As Varchar(10))+' [ID] from WebSite order by [ID] asc) order by [ID] asc'
Print @strSql
Exec(@strSql)
做完之后﹐總感覺應該會有更好的解決方案﹐可又一時想不出來!將其放在這里﹐供各位討論拍磚﹐如有最佳方案也請網友們不吝指教!
我當時直接就給他回
select ID,Name,Title from WebSite where ID= N
最后這位老兄說如果是ID是連續的話就可以,如果有記錄刪除的話,ID不連續了,就不行了 ,至此才明白他的真正意思。剛好要輪到公司的周會時間﹐于是想了一下。給出如下答案給他﹕
-- select top 1 [ID],[Name,Title from WebSite
-- where not [ID] in (select top 11 [ID] from WebSite order by [ID] asc) order by [ID] asc
Declare @strSql Varchar(200),@intTopN int
Set @intTopN = 11
Set @strSql = 'select top 1 [ID],[Name,Title from WebSite where not [ID] in (select top '+Cast(@intTopN As Varchar(10))+' [ID] from WebSite order by [ID] asc) order by [ID] asc'
Print @strSql
Exec(@strSql)
做完之后﹐總感覺應該會有更好的解決方案﹐可又一時想不出來!將其放在這里﹐供各位討論拍磚﹐如有最佳方案也請網友們不吝指教!