create proc p_1
@pagesize int,-- 每页的大小
@pageindex int--当前要查看的页
as
select * ,identity(int,1,1) as sid into # from tb
if @pagesize<1
set @pagesize=1
set rowcount @pagesize
select * from # where sid>(@pageindex-1)*@pagesize
set rowcount 0
go
--功能:通用海量数据库翻页
--设计:王文涛
QQ: 471432567
CREATE PROCEDURE Ture_Page
@PageSize int, --每页的行数
@PageIndex int, --1 代表第一页
@Col varchar(200), --要显示的字段
@Table varchar(200), --所用到的表
@Where varchar(200)='', --所用到的条件
@OKey varchar(50), --排序字段
@Order varchar(20)='ASC' --排序方式
as
declare @cmdstr varchar(2000)
set nocount on
set @cmdstr='select top '
set @cmdstr=@cmdstr+convert(nvarchar,@PageSize)
if @Order='DESC' and @PageIndex>1
set @cmdstr=@cmdstr+' '+@Col+' from '+@Table+' where '+@OKey+'<'
else if @PageIndex=1
set @cmdstr=@cmdstr+' '+@Col+' from '+@Table+' where '+@OKey+'>='
else
set @cmdstr=@cmdstr+' '+@Col+' from '+@Table+' where '+@OKey+'>'
if @PageIndex>1
begin
if @Order='ASC'
set @cmdstr=@cmdstr+'(select max ('+@OKey+') from (select top '
else
set @cmdstr=@cmdstr+'(select min ('+@OKey+') from (select top '
set @cmdstr=@cmdstr+convert(nvarchar,(@PageIndex-1)*@PageSize)
set @cmdstr=@cmdstr+' '+@OKey+' from '+@Table+' order by '+@OKey+' '+@Order+') as t) '
end
else
set @cmdstr=@cmdstr+'0 '
if @Where<>''
set @cmdstr=(@cmdstr+' and '+@Where+' order by '+@OKey+' '+@Order)
else
set @cmdstr=(@cmdstr+'order by '+@OKey+' '+@Order)
exec(@cmdstr)
set nocount off
GO
--那给你一个动态sql语句的。
CREATE procedure select_pagesize( @select_list varchar(1000)='*',--不需要select
@table_name varchar(100),
@where varchar(1000)='',--不需要where
@primary_key varchar(100),--当是表联合时,加表名前缀.
@order_by varchar(200),--需要完整的子句 order by ...
@page_size smallint=20,--每页记录
@page_index int=1,--页索引
@do_count bit=0)--1只统计总数
as
/*
过程名:通用存储过程分页
使用示例:
单表sql调用:exec select_pagesize 'login_id,login_name','tb_login',' login_name like ''%%''','login_id',' order by login_dt desc',20,10
多表sql调用:exec select_pagesize 'a.login_id,a.login_name,b.pro_name','tb_login a,tb_code_province b',' a.pro_id=b.pro_id and a.login_name like ''%%''','a.login_id',' order by a.login_dt desc',20,10
备注:外部程序调用不需要转义单引号
原型结构:select top 20 select_list
from tablename
where z_id not in(select z_id from (select top 100 z_id from tablename order by order_by) temptable)
and ...
order by order_by
*/
declare @sql_str varchar(8000)
declare @record_min int
declare @new_where varchar(1000),@newin_where varchar(1000)
if @where=''--重新为梳理,此过程时性能的考虑,因此不使用 where 1=1 再追加条件。
begin
select @new_where=''
select @newin_where=''
end
else
begin
select @new_where=' and '+@where
select @newin_where=' where '+@where
end
if @do_count=1
select @sql_str='select count(*) from '+@table_name+@newin_where
else
if @page_index=1
if @where=''
select @sql_str='select top '+convert(varchar,@page_size)+ ' '+@select_list+' from '+@table_name+' '+@order_by
else
select @sql_str='select top '+convert(varchar,@page_size)+ ' '+@select_list+' from '+@table_name+' where '+@where+' '+@order_by
else
begin
select @record_min=(@page_index-1)*@page_size
select @sql_str='select top '+convert(varchar,@page_size)+' '+@select_list+' from '+@table_name+' where '+@primary_key+' not in (select '+stuff(@primary_key,1,charindex('.',@primary_key),'')
select @sql_str=@sql_str+' from (select top '+convert(varchar,@record_min)+' '+@primary_key+' from '+@table_name+@newin_where+' '+@order_by+') temptable0000)'
select @sql_str=@sql_str+@new_where+' '+@order_by
end
--print @sql_str
exec(@sql_str)
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_PageView]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_PageView]
GO
/*--利用SQL未公开的存储过程实现分页
方法简单且效率高,已知的问题就是要多返回一个空的记录集
解决的方法是在前台调用时,用 set recordset=recordset.nextrecordset
的方法跳过第一个记录集
此方法由J9988提供,我只是将它改成了方便调用的存储过程
--邹建 2004.05(引用请保留此信息)--*/
/*--调用示例
declare @PageCount int
exec sp_PageView
@sql='select * from sysobjects',
@PageCurrent=2,
@PageCount=@PageCount out
SELECT @PageCount
--*/
CREATE PROC sp_PageView
@sql ntext, --要执行的sql语句
@PageCurrent int=1, --要显示的页码
@PageSize int=10, --每页的大小
@Rows int OUTPUT,--总记录数
@PageCount int OUTPUT --总页数
AS
SET NOCOUNT ON
DECLARE @p1 int
--初始化分页游标
EXEC sp_cursoropen
@cursor=@p1 OUTPUT,
@stmt=@sql,
@scrollopt=1,
@ccopt=1,
@rowcount=@PageCount OUTPUT
SET @Rows=@PageCount
--计算总页数
IF ISNULL(@PageSize,0)<1
SET @PageSize=10
SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
IF ISNULL(@PageCurrent,0)<1 OR ISNULL(@PageCurrent,0)>@PageCount
SET @PageCurrent=1
ELSE
SET @PageCurrent=(@PageCurrent-1)*@PageSize+1
--显示指定页的数据
EXEC sp_cursorfetch @p1,16,@PageCurrent,@PageSize
--关闭分页游标
EXEC sp_cursorclose @p1
GO
create proc p_1
@pageSize int = 10, ----每页显示的记录个数
@pageint = 1, ----要显示那一页的记录
@Countsint = 1 output ----查询到的记录数
as
select *,identity(int,1,1) as 'id' into # from tb
set rowcount @pagesize
select * from # where id>(@page-1)*@pagesize
set @counts=@@rowcount
set rowcount 0
go