第一个:
Code
CREATE PROC dbo.PageView
@tbname sysname, --要分页显示的表名
@FieldKey sysname, --用于定位记录的主键(惟一键)字段,只能是单个字段
@PageCurrent int=1, --要显示的页码
@PageSize int=10, --每页的大小(记录数)
@FieldShow nvarchar(1000)='', --以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段
@FieldOrder nvarchar(1000)='', --以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC
--用于指定排序顺序
@Where nvarchar(1000)='', --查询条件
@PageCount int --总页数
AS
DECLARE @sql nvarchar(4000)
SET NOCOUNT ON
IF OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTable')=0
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsView')=0
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTableFunction')=0
BEGIN
RAISERROR(N'"%s"不是表、视图或者表值函数',1,16,@tbname)
RETURN
END
--分页字段检查
IF ISNULL(@FieldKey,N'')=''
BEGIN
RAISERROR(N'分页处理需要主键(或者惟一键)',1,16)
RETURN
END
--其他参数检查及规范
IF ISNULL(@PageCurrent,0)<1 SET @PageCurrent=1
IF ISNULL(@PageSize,0)<1 SET @PageSize=10
IF ISNULL(@FieldShow,N'')=N'' SET @FieldShow=N'*'
IF ISNULL(@FieldOrder,N'')=N''
SET @FieldOrder=N''
ELSE
SET @FieldOrder=N'ORDER BY '+LTRIM(@FieldOrder)
IF ISNULL(@Where,N'')=N''
SET @Where=N''
ELSE
SET @Where=N'WHERE ('+@Where+N')'
--计算分页显示的TOPN值
DECLARE @TopN varchar(20),@TopN1 varchar(20)
SELECT @TopN=@PageSize,
@TopN1=@PageCurrent*@PageSize
--第一页直接显示
IF @PageCurrent=1
EXEC(N'SELECT TOP '+@TopN
+N' '+@FieldShow
+N' FROM '+@tbname
+N' '+@Where
+N' '+@FieldOrder)
ELSE
BEGIN
SELECT @PageCurrent=@TopN1,
@sql=N'SELECT @n=@n-1,@s=CASE WHEN @n<'+@TopN
+N' THEN @s+N'',''+QUOTENAME(RTRIM(CAST('+@FieldKey
+N' as varchar(8000))),N'''''''') ELSE N'''' END FROM '+@tbname
+N' '+@Where
+N' '+@FieldOrder
SET ROWCOUNT @PageCurrent
EXEC sp_executesql @sql,
N'@n int,@s nvarchar(4000) OUTPUT',
@PageCurrent,@sql OUTPUT
SET ROWCOUNT 0
IF @sql=N''
EXEC(N'SELECT TOP 0'
+N' '+@FieldShow
+N' FROM '+@tbname)
ELSE
BEGIN
SET @sql=STUFF(@sql,1,1,N'')
--执行查询
EXEC(N'SELECT TOP '+@TopN
+N' '+@FieldShow
+N' FROM '+@tbname
+N' WHERE '+@FieldKey
+N' IN('+@sql
+N') '+@FieldOrder)
END
END
Code
CREATE PROCEDURE [dbo].[UP_ReadRecordByPage]
(
@table VARCHAR(100),
@fields VARCHAR(500),
@condition NVARCHAR(500),
@orderby NVARCHAR(100),
@page_size INT,
@curr_page INT
)
AS
DECLARE @min_num INT
DECLARE @max_num INT
SET @min_num = @page_size * @curr_page
SET @max_num = @page_size * (@curr_page+1)
IF(@curr_page = 0)
BEGIN
EXEC('
SELECT TOP '+@page_size+' '+@fields+'
FROM '+@table+'
WHERE '+@condition+'
ORDER BY '+@orderby+';
')
END
ELSE
BEGIN
DECLARE @Sortable VARCHAR(20)
SELECT @Sortable = 'Temp'+convert(varchar,datepart(ss,getdate()))+'_'+convert(varchar,datepart(ms,getdate()))
EXEC('
CREATE TABLE #'+@Sortable+'
(
sort_id int IDENTITY (1, 1) NOT NULL ,
key_id int
);
INSERT INTO #'+@Sortable+'(key_id)
SELECT top '+@max_num+' [id]
FROM '+@table+'
WHERE '+@condition+'
ORDER BY '+@orderby+';
SELECT '+@fields+'
FROM #'+@Sortable+' A, '+@table+' B
WHERE A.key_id = B.[id] AND A.sort_id>'+@min_num+'
ORDER BY A.sort_id;
')
END
GO
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
CREATE PROC dbo.PageView
@tbname sysname, --要分页显示的表名
@FieldKey sysname, --用于定位记录的主键(惟一键)字段,只能是单个字段
@PageCurrent int=1, --要显示的页码
@PageSize int=10, --每页的大小(记录数)
@FieldShow nvarchar(1000)='', --以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段
@FieldOrder nvarchar(1000)='', --以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC
--用于指定排序顺序
@Where nvarchar(1000)='', --查询条件
@PageCount int --总页数
AS
DECLARE @sql nvarchar(4000)
SET NOCOUNT ON
IF OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTable')=0
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsView')=0
AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTableFunction')=0
BEGIN
RAISERROR(N'"%s"不是表、视图或者表值函数',1,16,@tbname)
RETURN
END
--分页字段检查
IF ISNULL(@FieldKey,N'')=''
BEGIN
RAISERROR(N'分页处理需要主键(或者惟一键)',1,16)
RETURN
END
--其他参数检查及规范
IF ISNULL(@PageCurrent,0)<1 SET @PageCurrent=1
IF ISNULL(@PageSize,0)<1 SET @PageSize=10
IF ISNULL(@FieldShow,N'')=N'' SET @FieldShow=N'*'
IF ISNULL(@FieldOrder,N'')=N''
SET @FieldOrder=N''
ELSE
SET @FieldOrder=N'ORDER BY '+LTRIM(@FieldOrder)
IF ISNULL(@Where,N'')=N''
SET @Where=N''
ELSE
SET @Where=N'WHERE ('+@Where+N')'
--计算分页显示的TOPN值
DECLARE @TopN varchar(20),@TopN1 varchar(20)
SELECT @TopN=@PageSize,
@TopN1=@PageCurrent*@PageSize
--第一页直接显示
IF @PageCurrent=1
EXEC(N'SELECT TOP '+@TopN
+N' '+@FieldShow
+N' FROM '+@tbname
+N' '+@Where
+N' '+@FieldOrder)
ELSE
BEGIN
SELECT @PageCurrent=@TopN1,
@sql=N'SELECT @n=@n-1,@s=CASE WHEN @n<'+@TopN
+N' THEN @s+N'',''+QUOTENAME(RTRIM(CAST('+@FieldKey
+N' as varchar(8000))),N'''''''') ELSE N'''' END FROM '+@tbname
+N' '+@Where
+N' '+@FieldOrder
SET ROWCOUNT @PageCurrent
EXEC sp_executesql @sql,
N'@n int,@s nvarchar(4000) OUTPUT',
@PageCurrent,@sql OUTPUT
SET ROWCOUNT 0
IF @sql=N''
EXEC(N'SELECT TOP 0'
+N' '+@FieldShow
+N' FROM '+@tbname)
ELSE
BEGIN
SET @sql=STUFF(@sql,1,1,N'')
--执行查询
EXEC(N'SELECT TOP '+@TopN
+N' '+@FieldShow
+N' FROM '+@tbname
+N' WHERE '+@FieldKey
+N' IN('+@sql
+N') '+@FieldOrder)
END
END
第二个:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
CREATE PROCEDURE [dbo].[UP_ReadRecordByPage]
(
@table VARCHAR(100),
@fields VARCHAR(500),
@condition NVARCHAR(500),
@orderby NVARCHAR(100),
@page_size INT,
@curr_page INT
)
AS
DECLARE @min_num INT
DECLARE @max_num INT
SET @min_num = @page_size * @curr_page
SET @max_num = @page_size * (@curr_page+1)
IF(@curr_page = 0)
BEGIN
EXEC('
SELECT TOP '+@page_size+' '+@fields+'
FROM '+@table+'
WHERE '+@condition+'
ORDER BY '+@orderby+';
')
END
ELSE
BEGIN
DECLARE @Sortable VARCHAR(20)
SELECT @Sortable = 'Temp'+convert(varchar,datepart(ss,getdate()))+'_'+convert(varchar,datepart(ms,getdate()))
EXEC('
CREATE TABLE #'+@Sortable+'
(
sort_id int IDENTITY (1, 1) NOT NULL ,
key_id int
);
INSERT INTO #'+@Sortable+'(key_id)
SELECT top '+@max_num+' [id]
FROM '+@table+'
WHERE '+@condition+'
ORDER BY '+@orderby+';
SELECT '+@fields+'
FROM #'+@Sortable+' A, '+@table+' B
WHERE A.key_id = B.[id] AND A.sort_id>'+@min_num+'
ORDER BY A.sort_id;
')
END
GO