当前位置: 代码迷 >> Sql Server >> 简单分页存储过程修改解决思路
  详细解决方案

简单分页存储过程修改解决思路

热度:67   发布时间:2016-04-27 13:36:41.0
简单分页存储过程修改
在SQL2005中,我写了一个简单的分页存储过程,但服务器上用的是SQL2000数据库。在SQL 2000数据库中不支持row_number函数。如何把下面存储过程修改能够在SQL2000中运行。

create procedure [dbo].[sp_UnifyPage]
@tableName varchar(50), --表名
@columnName varchar(50), --排序的列名
@startIndex varchar(50), --开始页
@endIndex varchar(50), --结束页
@conditions varchar(500),--分页的条件
@value varchar(50), --分页的条件所赋的值
@orderby varchar(50), --排序
@orderbyValue varchar(50) ----0代表asc,1代表desc
as
begin
  declare @where1 nvarchar(200) --保存分页的条件
  declare @orderBydesc nvarchar(200) --保存排序的条件
declare @sql varchar(2000)--变量,保存构造的sql语句
  --分页的条件
if(@conditions is null or rtrim(@conditions)='' and @value is null or rtrim(@value)='')--如果没有查询条件
begin
set @where1=' '
  set @value=' '
end
else
begin
set @where1=' and [email protected] [email protected]
end
  --排序
if(@orderby is null or rtrim(@orderby)='' and @orderbyValue is null or rtrim(@orderbyValue)='')--如果没有排序条件
begin
set @orderBydesc=' '
  set @orderbyValue=' '
end
else
begin
set @orderBydesc=' order by [email protected]+' '+case @orderbyValue when 0 then ' asc' else ' desc' end
end

  set @sql='with table_temp as (select row_number() over(order by [email protected]+') as rowIndex,* from [email protected]+')
  select * from table_temp where rowIndex between [email protected]+' and [email protected] [email protected] [email protected]
   
--select @sql
exec(@sql)
end

------解决方案--------------------
试试将这段:
SQL code
  set @sql='with table_temp as (select row_number() over(order by [email protected]+') as rowIndex,* from [email protected]+')  select * from table_temp where rowIndex between [email protected]+' and [email protected] [email protected] [email protected]    --select @sqlexec(@sql)
------解决方案--------------------
去看看2000通用的分页吧!一般是要加主键ID,然后用TOP来分页处理的。
------解决方案--------------------
SQL code
分页的存过--来自CSDNCREATE PROCEDURE [dbo].[Basic_Pagination2005]@tblName      nvarchar(200),     --表名@fidlelist    nvarchar(1000),   --要查询字段@fldName      nvarchar(100),    --排序字段@PageSize     int,              --页尺寸@PageIndex    int,              --页码@IsReCount    bit ,             -- 返回记录总数, 非 0 值则返回@OrderType    bit,              -- 设置排序类型, 非 0 值则降序@strWhere nvarchar(1000)        --查询条件ASdeclare @sqlstr nvarchar(4000),@tmpwhere nvarchar(4000),@tmporder nvarchar(100)BEGINif @OrderType != 0beginset @tmporder = @fldName +' desc 'endelsebegin        set @tmporder = @fldName +' asc 'endset @tmpwhere='';if(@strWhere!='')beginset @tmpwhere=' where [email protected];endset @sqlstr=N'select * from(select  [email protected]+', ROW_NUMBER() OVER(orderby [email protected]+') as row from [email protected][email protected]+') tmp where row between '+cast(((@PageIndex-1)[email protected]+1) as nvarchar)+' and '+cast(@[email protected] as nvarchar);  exec sp_executesql @sqlstrif @IsReCount != 0beginset @sqlstr=N'select count(*) as Total from '+ @[email protected]exec sp_executesql @sqlstr    endEND--返回第三页EXECUTE [Basic_Pagination2005] 'SC','S#','GRADE',10,3,200,1,'GRADE IS NOT NULL' --返回第10页EXECUTE [Basic_Pagination2005] 'SC','S#','GRADE',10,10,200,1,'GRADE IS NOT NULL' DROP PROC [Basic_Pagination2005]
------解决方案--------------------
SQL code
--分页存过2CREATE PROC spMyp@tbname     sysname,               --要分页显示的表名@FieldKey   nvarchar(1000),      --用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段@PageCurrent int=1,               --要显示的页码@PageSize   int=10,                --每页的大小(记录数)@FieldShow nvarchar(1000)='',      --以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段@FieldOrder nvarchar(1000)='',      --以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC@Where    nvarchar(1000)='',     --查询条件@PageCount int OUTPUT             --总页数ASSET NOCOUNT ON--检查对象是否有效IF OBJECT_ID(@tbname) IS NULLBEGIN    RAISERROR(N'对象"%s"不存在',1,16,@tbname)    RETURNENDIF OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTable')=0    AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsView')=0    AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTableFunction')=0BEGIN    RAISERROR(N'"%s"不是表、视图或者表值函数',1,16,@tbname)    RETURNEND--分页字段检查IF ISNULL(@FieldKey,N'')=''BEGIN    RAISERROR(N'分页处理需要主键(或者惟一键)',1,16)    RETURNEND--其他参数检查及规范IF ISNULL(@PageCurrent,0)<1 SET @PageCurrent=1IF ISNULL(@PageSize,0)<1 SET @PageSize=10IF 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 ([email protected]+N')'[email protected],则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,[email protected])IF @PageCount IS NULLBEGIN    DECLARE @sql nvarchar(4000)    SET @sql=N'SELECT @PageCount=COUNT(*)'        +N' FROM [email protected]        +N' [email protected]    EXEC sp_executesql @sql,[email protected] int OUTPUT',@PageCount OUTPUT    SET @PageCount=(@[email protected])[email protected]END--计算分页显示的TOPN值DECLARE @TopN varchar(20),@TopN1 varchar(20)SELECT @[email protected],    @TopN1=(@PageCurrent-1)[email protected]--第一页直接显示IF @PageCurrent=1    EXEC(N'SELECT TOP [email protected]        +N' [email protected]        +N' FROM [email protected]        +N' [email protected]        +N' [email protected])ELSEBEGIN    --处理别名    IF @FieldShow=N'*'        SET @FieldShow=N'a.*'    --生成主键(惟一键)处理条件    DECLARE @Where1 nvarchar(4000),@Where2 nvarchar(4000),        @s nvarchar(1000),@Field sysname    SELECT @Where1=N'',@Where2=N'',@[email protected]    WHILE CHARINDEX(N',',@s)>0        SELECT @Field=LEFT(@s,CHARINDEX(N',',@s)-1),            @s=STUFF(@s,1,CHARINDEX(N',',@s),N''),            @[email protected]+N' AND [email protected][email protected],            @[email protected]+N' AND [email protected]+N' IS NULL',            @Where=REPLACE(@Where,@Field,[email protected]),            @FieldOrder=REPLACE(@FieldOrder,@Field,[email protected]),            @FieldShow=REPLACE(@FieldShow,@Field,[email protected])    SELECT @Where=REPLACE(@Where,@s,[email protected]),        @FieldOrder=REPLACE(@FieldOrder,@s,[email protected]),        @FieldShow=REPLACE(@FieldShow,@s,[email protected]),        @Where1=STUFF(@Where1+N' AND [email protected][email protected],1,5,N''),            @Where2=CASE            WHEN @Where='' THEN N'WHERE ('            ELSE @Where+N' AND ('            [email protected]+N' IS [email protected]+N')'    --执行查询    declare @sss nvarchar(max)    set @sss=(N'SELECT TOP [email protected]        +N' [email protected]        +N' FROM [email protected]        +N' a LEFT JOIN(SELECT TOP [email protected]        +N' [email protected]        +N' FROM [email protected]        +N' a [email protected]        +N' [email protected]        +N')b ON [email protected]        +N' [email protected]        +N' [email protected])EXEC @sssENDDROP PROC spMypexec spMyp 's','s#','2','3','','','',null [email protected]/*结果SELECT TOP 3 a.* FROM s a LEFT JOIN(SELECT TOP 3 s# FROM s a  )b ON a.s#=b.s# WHERE (b.s# IS NULL)
  相关解决方案