sql server 2005分页存储过程和sql server 2000分页存储过程,sql 2005的分页存储过程分3个版本,一个是没有优化过的,一个是优化过的,最后一个支持jion的,sql2000的分页存储过程,也可以运行在sql2005上,但是性能没有sql2005的版本好。
Code
1USE [svnhost]
2GO
3/**//****** 对象: StoredProcedure [dbo].[up_Page2005] 脚本日期: 05/21/2008 11:27:05 ******/
4SET ANSI_NULLS ON
5GO
6SET QUOTED_IDENTIFIER ON
7GO
8CREATE proc [dbo].[up_Page2005]
9@TableName varchar(50), --表名
10@Fields varchar(5000) = '*', --字段名(全部字段为*)
11@OrderField varchar(5000), --排序字段(必须!支持多字段)
12@sqlWhere varchar(5000) = Null,--条件语句(不用加where)
13@pageSize int, --每页多少条记录
14@pageIndex int = 1 , --指定当前为第几页
15@TotalPage int output --返回总页数
16as
17begin
18 Begin Tran --开始事务
19 Declare @sql nvarchar(4000);
20 Declare @totalRecord int;
21 --计算总记录数
22
23 if (@SqlWhere='' or @sqlWhere=NULL)
24 set @sql = 'select @totalRecord = count(*) from ' + @TableName
25 else
26 set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' with(nolock) where ' + @sqlWhere
27 EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
28
29 --计算总页数
30
31 select @TotalPage=CEILING((@totalRecord+0.0)/@PageSize)
32
33 if (@SqlWhere='' or @sqlWhere=NULL)
34 set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName
35 else
36 set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName + ' with(nolock) where ' + @SqlWhere
37
38 --处理页数超出范围情况
39 if @PageIndex<=0
40 Set @pageIndex = 1
41
42 if @pageIndex>@TotalPage
43 Set @pageIndex = @TotalPage
44
45 --处理开始点和结束点
46 Declare @StartRecord int
47 Declare @EndRecord int
48
49 set @StartRecord = (@pageIndex-1)*@PageSize + 1
50 set @EndRecord = @StartRecord + @pageSize - 1
51
52 --继续合成sql语句
53 set @Sql = @Sql + ') as t where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
54 print @sql
55 Exec(@Sql)
56 ---------------------------------------------------
57 If @@Error <> 0
58 Begin
59 RollBack Tran
60 Return -1
61 End
62 Else
63 Begin
64 Commit Tran
65 Return @totalRecord ---返回记录总数
66 End
67end
68
1USE [svnhost]
2GO
3/**//****** 对象: StoredProcedure [dbo].[up_Page2005] 脚本日期: 05/21/2008 11:27:05 ******/
4SET ANSI_NULLS ON
5GO
6SET QUOTED_IDENTIFIER ON
7GO
8CREATE proc [dbo].[up_Page2005]
9@TableName varchar(50), --表名
10@Fields varchar(5000) = '*', --字段名(全部字段为*)
11@OrderField varchar(5000), --排序字段(必须!支持多字段)
12@sqlWhere varchar(5000) = Null,--条件语句(不用加where)
13@pageSize int, --每页多少条记录
14@pageIndex int = 1 , --指定当前为第几页
15@TotalPage int output --返回总页数
16as
17begin
18 Begin Tran --开始事务
19 Declare @sql nvarchar(4000);
20 Declare @totalRecord int;
21 --计算总记录数
22
23 if (@SqlWhere='' or @sqlWhere=NULL)
24 set @sql = 'select @totalRecord = count(*) from ' + @TableName
25 else
26 set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' with(nolock) where ' + @sqlWhere
27 EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
28
29 --计算总页数
30
31 select @TotalPage=CEILING((@totalRecord+0.0)/@PageSize)
32
33 if (@SqlWhere='' or @sqlWhere=NULL)
34 set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName
35 else
36 set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName + ' with(nolock) where ' + @SqlWhere
37
38 --处理页数超出范围情况
39 if @PageIndex<=0
40 Set @pageIndex = 1
41
42 if @pageIndex>@TotalPage
43 Set @pageIndex = @TotalPage
44
45 --处理开始点和结束点
46 Declare @StartRecord int
47 Declare @EndRecord int
48
49 set @StartRecord = (@pageIndex-1)*@PageSize + 1
50 set @EndRecord = @StartRecord + @pageSize - 1
51
52 --继续合成sql语句
53 set @Sql = @Sql + ') as t where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
54 print @sql
55 Exec(@Sql)
56 ---------------------------------------------------
57 If @@Error <> 0
58 Begin
59 RollBack Tran
60 Return -1
61 End
62 Else
63 Begin
64 Commit Tran
65 Return @totalRecord ---返回记录总数
66 End
67end
68
Code
1/**//****** 对象: StoredProcedure [dbo].[up_Page2005V2] 脚本日期: 05/21/2008 11:27:15 ******/
2SET ANSI_NULLS ON
3GO
4SET QUOTED_IDENTIFIER ON
5GO
6-- =============================================
7-- Author: <Author,,Name>
8-- Create date: <Create Date,,>
9-- Description: <Description,,>
10-- =============================================
11CREATE PROCEDURE [dbo].[up_Page2005V2]
12@TableName varchar(50), --表名
13@Fields varchar(5000) = '*', --字段名(全部字段为*)
14@OrderField varchar(5000), --排序字段(必须!支持多字段)
15@sqlWhere varchar(5000) = Null,--条件语句(不用加where)
16@pageSize int, --每页多少条记录
17@pageIndex int = 1 , --指定当前为第几页
18@totalRecord int = 0,
19@TotalPage int output --返回总页数
20AS
21BEGIN
22
23 Begin Tran --开始事务
24 Declare @sql nvarchar(4000);
25 if @totalRecord<=0 begin
26 --计算总记录数
27 if (@SqlWhere='' or @sqlWhere=NULL)
28 set @sql = 'select @totalRecord = count(*) from ' + @TableName
29 else
30 set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' with(nolock) where ' + @sqlWhere
31 EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
32 end
33
34 --计算总页数
35 select @TotalPage=CEILING((@totalRecord+0.0)/@PageSize)
36
37 if (@SqlWhere='' or @sqlWhere=NULL)
38 set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName
39 else
40 set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName + ' with(nolock) where ' + @SqlWhere
41
42 --处理页数超出范围情况
43 if @PageIndex<=0
44 Set @pageIndex = 1
45
46 if @pageIndex>@TotalPage
47 Set @pageIndex = @TotalPage
48
49 --处理开始点和结束点
50
51 Declare @StartRecord int
52 Declare @EndRecord int
53
54 set @StartRecord = (@pageIndex-1)*@PageSize + 1
55 set @EndRecord = @StartRecord + @pageSize - 1
56
57 --继续合成sql语句
58 set @Sql = @Sql + ') as t where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
59 print @sql
60 Exec(@Sql)
61 ---------------------------------------------------
62 If @@Error <> 0
63 Begin
64 RollBack Tran
65 Return -1
66 End
67 Else
68 Begin
69 Commit Tran
70 Return @totalRecord ---返回记录总数
71 End
72END
73
74GO
1/**//****** 对象: StoredProcedure [dbo].[up_Page2005V2] 脚本日期: 05/21/2008 11:27:15 ******/
2SET ANSI_NULLS ON
3GO
4SET QUOTED_IDENTIFIER ON
5GO
6-- =============================================
7-- Author: <Author,,Name>
8-- Create date: <Create Date,,>
9-- Description: <Description,,>
10-- =============================================
11CREATE PROCEDURE [dbo].[up_Page2005V2]
12@TableName varchar(50), --表名
13@Fields varchar(5000) = '*', --字段名(全部字段为*)
14@OrderField varchar(5000), --排序字段(必须!支持多字段)
15@sqlWhere varchar(5000) = Null,--条件语句(不用加where)
16@pageSize int, --每页多少条记录
17@pageIndex int = 1 , --指定当前为第几页
18@totalRecord int = 0,
19@TotalPage int output --返回总页数
20AS
21BEGIN
22
23 Begin Tran --开始事务
24 Declare @sql nvarchar(4000);
25 if @totalRecord<=0 begin
26 --计算总记录数
27 if (@SqlWhere='' or @sqlWhere=NULL)
28 set @sql = 'select @totalRecord = count(*) from ' + @TableName
29 else
30 set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' with(nolock) where ' + @sqlWhere
31 EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
32 end
33
34 --计算总页数
35 select @TotalPage=CEILING((@totalRecord+0.0)/@PageSize)
36
37 if (@SqlWhere='' or @sqlWhere=NULL)
38 set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName
39 else
40 set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName + ' with(nolock) where ' + @SqlWhere
41
42 --处理页数超出范围情况
43 if @PageIndex<=0
44 Set @pageIndex = 1
45
46 if @pageIndex>@TotalPage
47 Set @pageIndex = @TotalPage
48
49 --处理开始点和结束点
50
51 Declare @StartRecord int
52 Declare @EndRecord int
53
54 set @StartRecord = (@pageIndex-1)*@PageSize + 1
55 set @EndRecord = @StartRecord + @pageSize - 1
56
57 --继续合成sql语句
58 set @Sql = @Sql + ') as t where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
59 print @sql
60 Exec(@Sql)
61 ---------------------------------------------------
62 If @@Error <> 0
63 Begin
64 RollBack Tran
65 Return -1
66 End
67 Else
68 Begin
69 Commit Tran
70 Return @totalRecord ---返回记录总数
71 End
72END
73
74GO
Code
1 /****** 对象: StoredProcedure [dbo].[up_Page2005V2_Join] 脚本日期: 05/21/2008 11:27:30 ******/
2 SET ANSI_NULLS ON
3 GO
4 SET QUOTED_IDENTIFIER ON
5 GO
6 -- =============================================
7 -- Author: <Author,,Name>
8 -- Create date: <Create Date,,>
9 -- Description: <Description,,>
10 -- =============================================
11 CREATE PROCEDURE [dbo].[up_Page2005V2_Join]
12 @TableName varchar(150), --表名
13 @Fields varchar(5000) = '*', --字段名(全部字段为*)
14 @OrderField varchar(5000), --排序字段(必须!支持多字段)
15 @sqlWhere varchar(5000) = Null,--条件语句(不用加where)
16 @pageSize int, --每页多少条记录
17 @pageIndex int = 1 , --指定当前为第几页
18 @totalRecord int = 0,
19 @TotalPage int output --返回总页数
20 AS
21
22 BEGIN
23 Begin Tran --开始事务
24 Declare @sql nvarchar(4000);
25
26 if @totalRecord<=0 begin
27
28 --计算总记录数
29 if (@SqlWhere='' or @sqlWhere=NULL)
30 set @sql = 'select @totalRecord = count(*) from ' + @TableName
31 else
32 set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' where ' + @sqlWhere
33 EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
34 end
35
36 --计算总页数
37 select @TotalPage=CEILING((@totalRecord+0.0)/@PageSize)
38 if (@SqlWhere='' or @sqlWhere=NULL)
39 set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName
40 else
41 set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName + ' where ' + @SqlWhere
42
43 --处理页数超出范围情况
44 if @PageIndex<=0
45 Set @pageIndex = 1
46
47 if @pageIndex>@TotalPage
48 Set @pageIndex = @TotalPage
49
50 --处理开始点和结束点
51 Declare @StartRecord int
52 Declare @EndRecord int
53
54 set @StartRecord = (@pageIndex-1)*@PageSize + 1
55 set @EndRecord = @StartRecord + @pageSize - 1
56
57 --继续合成sql语句
58 set @Sql = @Sql + ') as t where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
59 print @sql
60
61 Exec(@Sql)
62 ---------------------------------------------------
63 If @@Error <> 0
64 Begin
65 RollBack Tran
66 Return -1
67 End
68 Else
69 Begin
70 Commit Tran
71 Return @totalRecord ---返回记录总数
72 End
73 END
74
1 /****** 对象: StoredProcedure [dbo].[up_Page2005V2_Join] 脚本日期: 05/21/2008 11:27:30 ******/
2 SET ANSI_NULLS ON
3 GO
4 SET QUOTED_IDENTIFIER ON
5 GO
6 -- =============================================
7 -- Author: <Author,,Name>
8 -- Create date: <Create Date,,>
9 -- Description: <Description,,>
10 -- =============================================
11 CREATE PROCEDURE [dbo].[up_Page2005V2_Join]
12 @TableName varchar(150), --表名
13 @Fields varchar(5000) = '*', --字段名(全部字段为*)
14 @OrderField varchar(5000), --排序字段(必须!支持多字段)
15 @sqlWhere varchar(5000) = Null,--条件语句(不用加where)
16 @pageSize int, --每页多少条记录
17 @pageIndex int = 1 , --指定当前为第几页
18 @totalRecord int = 0,
19 @TotalPage int output --返回总页数
20 AS
21
22 BEGIN
23 Begin Tran --开始事务
24 Declare @sql nvarchar(4000);
25
26 if @totalRecord<=0 begin
27
28 --计算总记录数
29 if (@SqlWhere='' or @sqlWhere=NULL)
30 set @sql = 'select @totalRecord = count(*) from ' + @TableName
31 else
32 set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' where ' + @sqlWhere
33 EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
34 end
35
36 --计算总页数
37 select @TotalPage=CEILING((@totalRecord+0.0)/@PageSize)
38 if (@SqlWhere='' or @sqlWhere=NULL)
39 set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName
40 else
41 set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName + ' where ' + @SqlWhere
42
43 --处理页数超出范围情况
44 if @PageIndex<=0
45 Set @pageIndex = 1
46
47 if @pageIndex>@TotalPage
48 Set @pageIndex = @TotalPage
49
50 --处理开始点和结束点
51 Declare @StartRecord int
52 Declare @EndRecord int
53
54 set @StartRecord = (@pageIndex-1)*@PageSize + 1
55 set @EndRecord = @StartRecord + @pageSize - 1
56
57 --继续合成sql语句
58 set @Sql = @Sql + ') as t where rowId between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
59 print @sql
60
61 Exec(@Sql)
62 ---------------------------------------------------
63 If @@Error <> 0
64 Begin
65 RollBack Tran
66 Return -1
67 End
68 Else
69 Begin
70 Commit Tran
71 Return @totalRecord ---返回记录总数
72 End
73 END
74
Code
1 USE [game]
2 GO
3 /****** 对象: StoredProcedure [dbo].[page] 脚本日期: 05/21/2008 11:37:12 ******/
4 SET ANSI_NULLS ON
5 GO
6 SET QUOTED_IDENTIFIER ON
7 GO
8 CREATE proc [dbo].[page]
9 @RecordCount int output,
10 @ReturnCount bit,
11 @QueryStr nvarchar(1000)='table1',--表名、视图名、查询语句
12 @PageSize int=20, --每页的大小(行数)
13 @PageCurrent int=2, --要显示的页 从0开始
14 @FdShow nvarchar (2000)='*', --要显示的字段列表
15 @IdentityStr nvarchar (100)='id', --主键
16 @WhereStr nvarchar (2000)='1=1',
17 @FdOrder nvarchar(100)='desc' --排序 只能取desc或者asc
18 as
19
20 set nocount on
21 declare
22 @sql nvarchar(2000)
23
24 if @WhereStr = '' begin
25
26 set @WhereStr = '1=1'
27
28 end
29
30 if @ReturnCount=1 begin
31 declare @tsql nvarchar(200)
32 set @tsql=N'select @RecordCount = count(*) from ' + @QueryStr + ' where ' + @WhereStr
33 exec sp_executesql @tsql,N'@RecordCount int output',@RecordCount output
34 end
35
36 if @PageCurrent = 0 begin
37 set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' ' + @FdOrder
38 end
39
40 else begin
41 if upper(@FdOrder) = 'DESC' begin
42
43 set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '< ( select min(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' desc) as t) order by ' + @IdentityStr + ' desc'
44
45 end
46
47 else begin
48
49 set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '> ( select max(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' asc) as t) order by ' + @IdentityStr + ' asc'
50
51 end
52
53 end
54
55 --print @sql
56
57 execute(@sql)
58
59 --select @t = datediff(ms,@t1,getdate())---------------------
60
1 USE [game]
2 GO
3 /****** 对象: StoredProcedure [dbo].[page] 脚本日期: 05/21/2008 11:37:12 ******/
4 SET ANSI_NULLS ON
5 GO
6 SET QUOTED_IDENTIFIER ON
7 GO
8 CREATE proc [dbo].[page]
9 @RecordCount int output,
10 @ReturnCount bit,
11 @QueryStr nvarchar(1000)='table1',--表名、视图名、查询语句
12 @PageSize int=20, --每页的大小(行数)
13 @PageCurrent int=2, --要显示的页 从0开始
14 @FdShow nvarchar (2000)='*', --要显示的字段列表
15 @IdentityStr nvarchar (100)='id', --主键
16 @WhereStr nvarchar (2000)='1=1',
17 @FdOrder nvarchar(100)='desc' --排序 只能取desc或者asc
18 as
19
20 set nocount on
21 declare
22 @sql nvarchar(2000)
23
24 if @WhereStr = '' begin
25
26 set @WhereStr = '1=1'
27
28 end
29
30 if @ReturnCount=1 begin
31 declare @tsql nvarchar(200)
32 set @tsql=N'select @RecordCount = count(*) from ' + @QueryStr + ' where ' + @WhereStr
33 exec sp_executesql @tsql,N'@RecordCount int output',@RecordCount output
34 end
35
36 if @PageCurrent = 0 begin
37 set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' ' + @FdOrder
38 end
39
40 else begin
41 if upper(@FdOrder) = 'DESC' begin
42
43 set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '< ( select min(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' desc) as t) order by ' + @IdentityStr + ' desc'
44
45 end
46
47 else begin
48
49 set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' ' + @FdShow + ' from ' + @QueryStr + ' where ' + @WhereStr + ' and ' + @IdentityStr + '> ( select max(' + @IdentityStr + ') from (select top ' + cast(@PageSize*@PageCurrent as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' where ' + @WhereStr + ' order by ' + @IdentityStr + ' asc) as t) order by ' + @IdentityStr + ' asc'
50
51 end
52
53 end
54
55 --print @sql
56
57 execute(@sql)
58
59 --select @t = datediff(ms,@t1,getdate())---------------------
60