做論壇帖子列表時用到了aspnetpager控制項,這是乙個純分頁用的控制項,與資料毫無關係。在分頁時需要自己寫儲存過程。一開始用select top 10 * from a where id not in (select top 10 id from a )來做,有個問題:sqlsqrver2000中,top後面不能跟變數,這直接導致這種方法失效,而且這樣做也很浪費資源。
於是,換了個做法:
alter procedure dbo.storedprocedure1
(@startindex int,
//每頁起始id
@endindex int,
//每頁完畢id
@forum_id int,
//版塊id
@state int
//帖子狀態 )
as
set nocount on
declare @indextable table(id int identity(1,1),nid int)
//定義乙個臨時表,用來儲存查詢全部結果的順序號,從1開始一直到記錄總數
set rowcount @endindex
insert into @indextable(nid)
//將查詢結果寫入新錶,並自動生成順序號
select id from topic
where (topic.forum_id = @forum_id)
and (topic.state != @state)
order by topic.issue_date desc
//這裡的排序方式必須和下面的排序一致
select topic.*, userinfo.user_name from topic
//按每頁(endindex-startindex)條,顯示查詢符合條件主題的帖子
inner join userinfo on topic.user_id = userinfo.id
inner join @indextable t on topic.id=t.nid
where (topic.forum_id = @forum_id)
and (topic.state != @state)
//查詢除了置頂帖(3)以外的帖子,並按發表時間降序排列
and (t.id between @startindex and @endindex) order by topic.issue_date desc
set nocount off
return
sql 儲存過程分頁
create proc myx prpagerecordset querystr nvarchar 1000 keyfield nvarchar 200 pagesize int,pagenumber int as begin declare sqltext as nvarchar 4000 dec...
SQL 儲存過程 分頁
1.俄羅斯儲存過程 的改良版 create procedure pagination1 pagesize int,頁面大小,如每頁儲存20條記錄 pageindex int 當前頁碼 as set nocount on begin declare indextable table id int id...
SQL 分頁儲存過程
create procedure splitpage sql nvarchar 4000 不帶排序語句的sql語句 page int,頁碼 recsperpage int,每頁容納的記錄數 id varchar 255 需要排序的不重複的id號 sort varchar 255 排序欄位及規則 as...