CREATE procedure main_table_pwqzc
(@pagesize int,
@pageindex int,
@docount bit,
@this_id)
as
if(@docount=1)
begin
select count(id) from luntan where order by t.id
end
GO
存儲過程會根據傳入的參數@docount來確定是不是要返回所有要分頁的記錄總數
特別是這兩行
set rowcount @PageUpperBound
insert into @indextable(nid) select id from luntan where order by reply_time desc
真的是妙不可言!!set rowcount @PageUpperBound當記錄數達到@PageUpperBound時就會停止處理查詢
,select id 只把id列取出放到臨時表裡,select a.* from luntan a,@indextable t where a.id=t.nid
and t.id>@PageLowerBound and t.id< order by t.id
而這句也只從表中取出所需要的記錄,而不是所有的記錄,結合起來,極大的提高了效率!!
妙啊,真的妙!!!!
CREATE PROCEDURE Paging_RowCount
(
@Tables varchar(1000),
@PK varchar(100),
@Sort varchar(200) = NULL,
@PageNumber int = 1,
@PageSize int = 10,
@FIElds varchar(1000) = '*',
@Filter varchar(1000) = NULL,
@Group varchar(1000) = NULL)
AS
/*Default Sorting*/
IF @Sort IS NULL OR @Sort = ''
SET @Sort = @PK
/*Find the @PK type*/
DECLARE @SortTable varchar(100)
DECLARE @SortName varchar(100)
DECLARE @strSortColumn varchar(200)
DECLARE @Operator char(2)
DECLARE @type varchar(100)
DECLARE @prec int
/*Set sorting variables.*/
IF CHARINDEX('DESC',@Sort)>0
BEGIN
SET @strSortColumn = REPLACE(@Sort, 'DESC', '')
SET @Operator = '<='
END
ELSE
BEGIN
IF CHARINDEX('ASC', @Sort) = 0
SET @strSortColumn = REPLACE(@Sort, 'ASC', '')
SET @Operator = '>='
END
IF CHARINDEX('.', @strSortColumn) > 0
BEGIN
SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))
SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1,