Sql2000 版通用分頁儲存過程
阿新 • • 發佈:2019-02-15
CREATEPROCEDURE sys_QuickSortPaging
(
@Tablenvarchar(4000), --表名(必須)
@PrimaryKeyFieldnvarchar(50), --表的主鍵欄位
@Fieldnvarchar (4000)='*', --需要返回欄位名(必須)
@Wherenvarchar(1000)=NULL, --Where 條件(可選)
@GroupBynvarchar(1000) =NULL, --分組
@OrderBy nvarchar(1000)=NULL, --排序用到的欄位()
@PageNumberint=1, --要返回的頁(第X頁) (預設為第一頁)
@PageSizeint=10, --每頁大小(預設為5)
@RecordCountint output --返回記錄總數
)
AS
SET NOCOUNT ON
DECLARE@SortTablenvarchar(100)
DECLARE@SortNamenvarchar (100)
DECLARE@strSortColumnnvarchar(200)
DECLARE@Operatornvarchar(50)
DECLARE@Typevarchar(100)
DECLARE@Precint
IF@OrderByISNULLOR@OrderBy=''
SET@OrderBy=@PrimaryKeyField
/* 獲取用於定位的欄位*/
IFCHARINDEX('DESC',@OrderBy)>0
BEGIN
SET@strSortColumn=REPLACE(@OrderBy, 'DESC', '')
SET@Operator='<='
END
ELSE
BEGIN
IFCHARINDEX('ASC', @OrderBy) =0
SET@strSortColumn=REPLACE(@OrderBy, 'ASC', '')
SET@Operator='>='
END
IFCHARINDEX('.', @strSortColumn) >0
BEGIN
SET@SortTable=SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))
SET@SortName=SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) +1, LEN(@strSortColumn))
END
ELSE
BEGIN
SET@SortTable=@Table
SET@SortName=@strSortColumn
END
SELECT@Type=t.name, @Prec=c.prec
FROM sysobjects o
JOIN syscolumns c on o.id=c.id
JOIN systypes t on c.xusertype=t.xusertype
WHERE o.name =@SortTableAND c.name =@SortName
IFCHARINDEX('char', @Type) >0
SET@Type=@Type+'('+CAST(@PrecASnvarchar) +')'
DECLARE@strStartRownvarchar(50)
DECLARE@strPageSizenvarchar(50)
DECLARE@strWherenvarchar(1000)
DECLARE@strWhereAndnvarchar(1000)
DECLARE@strGroupBynvarchar(1000)
IF@PageNumber<1
SET@PageNumber=1
SET@strPageSize=CONVERT (nvarchar(50), @PageSize)
SET@strStartRow=CONVERT ( nvarchar(50), (@PageNumber-1)*@PageSize+1)
IF@WhereISNOTNULLAND@Where!=''
BEGIN
SET@strWhere=' WHERE '+@Where
SET@strWhereAnd=' AND '+@Where
END
ELSE
BEGIN
SET@strWhere=''
SET@strWhereAnd=''
END
IF@GroupByISNOTNULLAND@GroupBy!=''
BEGIN
SET@strGroupBy=' GROUP BY '+@GroupBy
END
ELSE
BEGIN
SET@strGroupBy=''
END
DECLARE@strSQLnvarchar(4000)
SET@strSql=' SELECT @RecordCount = Count (*) FROM '+@Table+@strWhere+''+@strGroupBy
EXEC sp_executesql @strSql,N'@RecordCount int OUTPUT',@RecordCount OUTPUT--計算總頁數
EXEC
(
'
DECLARE @Sort '+@Type+'
SET ROWCOUNT '+@strStartRow+'
SELECT @Sort = '+@strSortColumn+' FROM '+@Table+@strWhere+''+@strGroupBy+' ORDER BY '+@OrderBy+'
SET ROWCOUNT '+@strPageSize+'
SELECT '+@Field+' FROM '+@Table+' WHERE '+@strSortColumn+@Operator+' @Sort '+@strWhereAnd+''+@strGroupBy+' ORDER BY '+@OrderBy
)
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
(
@Tablenvarchar(4000), --表名(必須)
@PrimaryKeyFieldnvarchar(50), --表的主鍵欄位
@Fieldnvarchar (4000)='*', --需要返回欄位名(必須)
@Wherenvarchar(1000)=NULL, --Where 條件(可選)
@GroupBynvarchar(1000) =NULL, --分組
@OrderBy
@PageNumberint=1, --要返回的頁(第X頁) (預設為第一頁)
@PageSizeint=10, --每頁大小(預設為5)
@RecordCountint output --返回記錄總數
)
AS
SET NOCOUNT ON
DECLARE@SortTablenvarchar(100)
DECLARE@SortNamenvarchar
DECLARE@strSortColumnnvarchar(200)
DECLARE@Operatornvarchar(50)
DECLARE@Typevarchar(100)
DECLARE@Precint
IF@OrderByISNULLOR@OrderBy=''
SET@OrderBy=@PrimaryKeyField
/* 獲取用於定位的欄位*/
IFCHARINDEX('DESC',@OrderBy)>0
BEGIN
SET@Operator='<='
END
ELSE
BEGIN
IFCHARINDEX('ASC', @OrderBy) =0
SET@strSortColumn=REPLACE(@OrderBy, 'ASC', '')
SET@Operator='>='
END
IFCHARINDEX('.', @strSortColumn) >0
BEGIN
SET@SortTable=SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))
SET@SortName=SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) +1, LEN(@strSortColumn))
END
ELSE
BEGIN
SET@SortTable=@Table
SET@SortName=@strSortColumn
END
SELECT@Type=t.name, @Prec=c.prec
FROM sysobjects o
JOIN syscolumns c on o.id=c.id
JOIN systypes t on c.xusertype=t.xusertype
WHERE o.name =@SortTableAND c.name =@SortName
IFCHARINDEX('char', @Type) >0
SET@Type=@Type+'('+CAST(@PrecASnvarchar) +')'
DECLARE@strStartRownvarchar(50)
DECLARE@strPageSizenvarchar(50)
DECLARE@strWherenvarchar(1000)
DECLARE@strWhereAndnvarchar(1000)
DECLARE@strGroupBynvarchar(1000)
IF@PageNumber<1
SET@PageNumber=1
SET@strPageSize=CONVERT (nvarchar(50), @PageSize)
SET@strStartRow=CONVERT ( nvarchar(50), (@PageNumber-1)*@PageSize+1)
IF@WhereISNOTNULLAND@Where!=''
BEGIN
SET@strWhere=' WHERE '+@Where
SET@strWhereAnd=' AND '+@Where
END
ELSE
BEGIN
SET@strWhere=''
SET@strWhereAnd=''
END
IF@GroupByISNOTNULLAND@GroupBy!=''
BEGIN
SET@strGroupBy=' GROUP BY '+@GroupBy
END
ELSE
BEGIN
SET@strGroupBy=''
END
DECLARE@strSQLnvarchar(4000)
SET@strSql=' SELECT @RecordCount = Count (*) FROM '+@Table+@strWhere+''+@strGroupBy
EXEC sp_executesql @strSql,N'@RecordCount int OUTPUT',@RecordCount OUTPUT--計算總頁數
EXEC
(
'
DECLARE @Sort '+@Type+'
SET ROWCOUNT '+@strStartRow+'
SELECT @Sort = '+@strSortColumn+' FROM '+@Table+@strWhere+''+@strGroupBy+' ORDER BY '+@OrderBy+'
SET ROWCOUNT '+@strPageSize+'
SELECT '+@Field+' FROM '+@Table+' WHERE '+@strSortColumn+@Operator+' @Sort '+@strWhereAnd+''+@strGroupBy+' ORDER BY '+@OrderBy
)
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO