首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-04-15 
简单分页存储过程修改在SQL2005中,我写了一个简单的分页存储过程,但服务器上用的是SQL2000数据库。在SQL 20

简单分页存储过程修改
在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 '+@conditions +'='+@value
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 '+@orderby+' '+case @orderbyValue when 0 then ' asc' else ' desc' end
end

  set @sql='with table_temp as (select row_number() over(order by '+@columnName+') as rowIndex,* from '+@tableName+')
  select * from table_temp where rowIndex between '+@startIndex+' and '+@endIndex +@where1 +@orderBydesc
   
--select @sql
exec(@sql)
end

[解决办法]
试试将这段:

SQL code
  set @sql='with table_temp as (select row_number() over(order by '+@columnName+') as rowIndex,* from '+@tableName+')  select * from table_temp where rowIndex between '+@startIndex+' and '+@endIndex +@where1 +@orderBydesc    --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 '+@strWhere;endset @sqlstr=N'select * from(select  '+@fidlelist+', ROW_NUMBER() OVER(orderby '+@tmporder+') as row from '+@tblName+@tmpwhere+') tmp where row between '+cast(((@PageIndex-1)*@PageSize+1) as nvarchar)+' and '+cast(@PageIndex*@PageSize as nvarchar);  exec sp_executesql @sqlstrif @IsReCount != 0beginset @sqlstr=N'select count(*) as Total from '+ @tblName+@tmpwhereexec 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 ('+@Where+N')'--如果@PageCount为NULL值,则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,可以给@PageCount赋值)IF @PageCount IS NULLBEGIN    DECLARE @sql nvarchar(4000)    SET @sql=N'SELECT @PageCount=COUNT(*)'        +N' FROM '+@tbname        +N' '+@Where    EXEC sp_executesql @sql,N'@PageCount int OUTPUT',@PageCount OUTPUT    SET @PageCount=(@PageCount+@PageSize-1)/@PageSizeEND--计算分页显示的TOPN值DECLARE @TopN varchar(20),@TopN1 varchar(20)SELECT @TopN=@PageSize,    @TopN1=(@PageCurrent-1)*@PageSize--第一页直接显示IF @PageCurrent=1    EXEC(N'SELECT TOP '+@TopN        +N' '+@FieldShow        +N' FROM '+@tbname        +N' '+@Where        +N' '+@FieldOrder)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'',@s=@FieldKey    WHILE CHARINDEX(N',',@s)>0        SELECT @Field=LEFT(@s,CHARINDEX(N',',@s)-1),            @s=STUFF(@s,1,CHARINDEX(N',',@s),N''),            @Where1=@Where1+N' AND a.'+@Field+N'=b.'+@Field,            @Where2=@Where2+N' AND b.'+@Field+N' IS NULL',            @Where=REPLACE(@Where,@Field,N'a.'+@Field),            @FieldOrder=REPLACE(@FieldOrder,@Field,N'a.'+@Field),            @FieldShow=REPLACE(@FieldShow,@Field,N'a.'+@Field)    SELECT @Where=REPLACE(@Where,@s,N'a.'+@s),        @FieldOrder=REPLACE(@FieldOrder,@s,N'a.'+@s),        @FieldShow=REPLACE(@FieldShow,@s,N'a.'+@s),        @Where1=STUFF(@Where1+N' AND a.'+@s+N'=b.'+@s,1,5,N''),            @Where2=CASE            WHEN @Where='' THEN N'WHERE ('            ELSE @Where+N' AND ('            END+N'b.'+@s+N' IS NULL'+@Where2+N')'    --执行查询    declare @sss nvarchar(max)    set @sss=(N'SELECT TOP '+@TopN        +N' '+@FieldShow        +N' FROM '+@tbname        +N' a LEFT JOIN(SELECT TOP '+@TopN1        +N' '+@FieldKey        +N' FROM '+@tbname        +N' a '+@Where        +N' '+@FieldOrder        +N')b ON '+@Where1        +N' '+@Where2        +N' '+@FieldOrder)EXEC @sssENDDROP PROC spMypexec spMyp 's','s#','2','3','','','',null --当页数为1的时候是不输出最后的@sss的/*结果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) 


[解决办法]

SQL code
MSSQL2000set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER PROC [dbo].[PROCE_PageView2000](@tbname     nvarchar(100),               --要分页显示的表名@FieldKey   nvarchar(1000),      --用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段@PageCurrent int=1,               --要显示的页码@PageSize   int=10,                --每页的大小(记录数)@FieldShow nvarchar(1000)='',      --以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段@FieldOrder nvarchar(1000)='',      --以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC@WhereString    nvarchar(1000)=N'',     --查询条件@RecordCount int OUTPUT             --总记录数)ASSET NOCOUNT ON--检查对象是否有效--IF OBJECT_ID(@tbname) IS NULL--BEGIN--    RAISERROR(N'对象"%s"不存在',1,16,@tbname)--    RETURN--END--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)    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(@WhereString,N'')=N''    SET @WhereString=N''ELSE    SET @WhereString=N'WHERE ('+@WhereString+N')'--如果@RecordCount为NULL值,则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,可以给@RecordCount赋值)IF @RecordCount IS NULLBEGIN    DECLARE @sql nvarchar(4000)    SET @sql=N'SELECT @RecordCount=COUNT(*)'        +N' FROM '+@tbname        +N' '+@WhereString    EXEC sp_executesql @sql,N'@RecordCount int OUTPUT',@RecordCount OUTPUTEND--计算分页显示的TOPN值DECLARE @TopN varchar(20),@TopN1 varchar(20)SELECT @TopN=@PageSize,    @TopN1=(@PageCurrent-1)*@PageSize--第一页直接显示IF @PageCurrent=1    EXEC(N'SELECT TOP '+@TopN        +N' '+@FieldShow        +N' FROM '+@tbname        +N' '+@WhereString        +N' '+@FieldOrder)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'',@s=@FieldKey    WHILE CHARINDEX(N',',@s)>0        SELECT @Field=LEFT(@s,CHARINDEX(N',',@s)-1),            @s=STUFF(@s,1,CHARINDEX(N',',@s),N''),            @Where1=@Where1+N' AND a.'+@Field+N'=b.'+@Field,            @Where2=@Where2+N' AND b.'+@Field+N' IS NULL',            @WhereString=REPLACE(@WhereString,@Field,N'a.'+@Field),            @FieldOrder=REPLACE(@FieldOrder,@Field,N'a.'+@Field),            @FieldShow=REPLACE(@FieldShow,@Field,N'a.'+@Field)    SELECT @WhereString=REPLACE(@WhereString,@s,N'a.'+@s),        @FieldOrder=REPLACE(@FieldOrder,@s,N'a.'+@s),        @FieldShow=REPLACE(@FieldShow,@s,N'a.'+@s),        @Where1=STUFF(@Where1+N' AND a.'+@s+N'=b.'+@s,1,5,N''),            @Where2=CASE            WHEN @WhereString='' THEN N'WHERE ('            ELSE @WhereString+N' AND ('            END+N'b.'+@s+N' IS NULL'+@Where2+N')'    --执行查询    EXEC(N'SELECT TOP '+@TopN        +N' '+@FieldShow        +N' FROM '+@tbname        +N' a LEFT JOIN(SELECT TOP '+@TopN1        +N' '+@FieldKey        +N' FROM '+@tbname        +N' a '+@WhereString        +N' '+@FieldOrder        +N')b ON '+@Where1        +N' '+@Where2        +N' '+@FieldOrder)END MSSQL2005set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER PROCEDURE [dbo].[PROCE_SQL2005PAGECHANGE]( @TableName varchar(50),            --表名 @ReFieldsStr varchar(200) = '*',   --字段名(全部字段为*) @OrderString varchar(200),         --排序字段(必须!支持多字段不用加order by) @WhereString varchar(500) =N'',  --条件语句(不用加where) @PageSize int,                     --每页多少条记录 @PageIndex int = 1 ,               --指定当前为第几页 @TotalRecord int output            --返回总记录数)AS BEGIN        --处理开始点和结束点    Declare @StartRecord int;    Declare @EndRecord int;     Declare @TotalCountSql nvarchar(500);     Declare @SqlString nvarchar(2000);        set @StartRecord = (@PageIndex-1)*@PageSize + 1    set @EndRecord = @StartRecord + @PageSize - 1     SET @TotalCountSql= N'select @TotalRecord = count(*) from ' + @TableName;--总记录数语句    SET @SqlString = N'(select row_number() over (order by '+ @OrderString +') as rowId,'+@ReFieldsStr+' from '+ @TableName;--查询语句    --    IF (@WhereString! = '' or @WhereString!=null)        BEGIN            SET @TotalCountSql=@TotalCountSql + '  where '+ @WhereString;            SET @SqlString =@SqlString+ '  where '+ @WhereString;                    END    --第一次执行得到    --IF(@TotalRecord is null)    --   BEGIN           EXEC sp_executesql @totalCountSql,N'@TotalRecord int out',@TotalRecord output;--返回总记录数    --  END    ----执行主语句    set @SqlString ='select * from ' + @SqlString + ') as t where rowId between ' + ltrim(str(@StartRecord)) + ' and ' +  ltrim(str(@EndRecord));    Exec(@SqlString)    END 


[解决办法]
set @sql='select rowIndex=identity(int,1,1),* into #1 from '+@tablename+' order by '+ @columnname+'
 select * from #1 where rowIndex between '+@startIndex+' and '+@endIndex +@where1 +@orderBydesc

exec(@sql)

热点排行