当前位置: 代码迷 >> Sql Server >> SQL2005 分页存储过程转为 SQL2003 分页存储过程解决方法
  详细解决方案

SQL2005 分页存储过程转为 SQL2003 分页存储过程解决方法

热度:138   发布时间:2016-04-27 15:00:50.0
SQL2005 分页存储过程转为 SQL2003 分页存储过程
我这边有一段SQL2005 的分页存储过程 现在要放到SQL2000 上 但是会出错 请问要怎么修改?(或者能够写成跟2005功能一样的也行)
代码:

GO
/****** Object: StoredProcedure [dbo].[UtilPage] Script Date: 10/27/2011 17:43:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[UtilPage]
@datasrc nvarchar(200)
,@orderBy nvarchar(200)
,@fieldlist nvarchar(300) = '*'
,@filter nvarchar(1000) = ''
,@pageNum int = 1
,@pageSize int = NULL
AS
SET NOCOUNT ON
DECLARE
  @STMT nvarchar(4000) -- SQL to execute
  ,@recct int -- total # of records (for GridView paging interface)

IF LTRIM(RTRIM(@filter)) = '' SET @filter = '1 = 1'
IF @pageSize IS NULL BEGIN
  SET @STMT = 'SELECT ' + @fieldlist + 
  'FROM ' + @datasrc +
  'WHERE ' + @filter + 
  'ORDER BY ' + @orderBy
  EXEC (@STMT) -- return requested records 
END ELSE BEGIN
  SET @STMT = 'SELECT @recct = COUNT(*)
  FROM ' + @datasrc + '
  WHERE ' + @filter
  EXEC sp_executeSQL @STMT, @params = [email protected] INT OUTPUT', @recct = @recct OUTPUT
  SELECT @recct AS recct -- return the total # of records

  DECLARE
  @lbound int,
  @ubound int

  SET @pageNum = ABS(@pageNum)
  SET @pageSize = ABS(@pageSize)
  IF @pageNum < 1 SET @pageNum = 1
  IF @pageSize < 1 SET @pageSize = 1
  SET @lbound = ((@pageNum - 1) * @pageSize)
  SET @ubound = @lbound + @pageSize + 1
  IF @lbound >= @recct BEGIN
  SET @ubound = @recct + 1
  SET @lbound = @ubound - (@pageSize + 1) -- return the last page of records if -- no records would be on the
  -- specified page
  END
  SET @STMT = 'SELECT ' + @fieldlist + '
  FROM (
  SELECT ROW_NUMBER() OVER(ORDER BY ' + @orderBy + ') AS row, *
  FROM ' + @datasrc + '
  WHERE ' + @filter + '
  ) AS tbl
  WHERE
  row > ' + CONVERT(varchar(9), @lbound) + ' AND
  row < ' + CONVERT(varchar(9), @ubound)
  EXEC (@STMT) -- return requested records 
  print (@STMT)
END


------解决方案--------------------
SQL code
--参考吧:set 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 ([email protected]+N')'[email protected],则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,[email protected])IF @RecordCount IS NULLBEGIN    DECLARE @sql nvarchar(4000)    SET @sql=N'SELECT @RecordCount=COUNT(*)'        +N' FROM [email protected]        +N' [email protected]    EXEC sp_executesql @sql,[email protected] int OUTPUT',@RecordCount OUTPUTEND--计算分页显示的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',            @WhereString=REPLACE(@WhereString,@Field,[email protected]),            @FieldOrder=REPLACE(@FieldOrder,@Field,[email protected]),            @FieldShow=REPLACE(@FieldShow,@Field,[email protected])    SELECT @WhereString=REPLACE(@WhereString,@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 @WhereString='' THEN N'WHERE ('            ELSE @WhereString+N' AND ('            [email protected]+N' IS [email protected]+N')'    --执行查询    EXEC(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])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)[email protected] + 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,[email protected]+' from '+ @TableName;--查询语句    --    IF (@WhereString! = '' or @WhereString!=null)        BEGIN            SET @[email protected] + '  where '+ @WhereString;            SET @SqlString [email protected]+ '  where '+ @WhereString;                    END    --第一次执行得到    --IF(@TotalRecord is null)    --   BEGIN           EXEC sp_executesql @totalCountSql,[email protected] 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
------解决方案--------------------
SQL code
ALTER PROCEDURE [dbo].[UtilPage]@datasrc nvarchar(200),@orderBy nvarchar(200),@fieldlist nvarchar(300) = '*',@filter nvarchar(1000) = '',@pageNum int = 1,@pageSize int = NULLASSET NOCOUNT ONDECLARE  @STMT nvarchar(4000) -- SQL to execute  ,@recct int -- total # of records (for GridView paging interface)IF LTRIM(RTRIM(@filter)) = '' SET @filter = '1 = 1'IF @pageSize IS NULL BEGIN  SET @STMT = 'SELECT ' + @fieldlist +    'FROM ' + @datasrc +  'WHERE ' + @filter +    'ORDER BY ' + @orderBy  EXEC (@STMT) -- return requested records  END ELSE BEGIN  SET @STMT = 'SELECT @recct = COUNT(*)  FROM ' + @datasrc + '  WHERE ' + @filter  EXEC sp_executeSQL @STMT, @params = [email protected] INT OUTPUT', @recct = @recct OUTPUT  SELECT @recct AS recct -- return the total # of records  DECLARE  @lbound int,  @ubound int  SET @pageNum = ABS(@pageNum)  SET @pageSize = ABS(@pageSize)  IF @pageNum < 1 SET @pageNum = 1  IF @pageSize < 1 SET @pageSize = 1  SET @lbound = ((@pageNum - 1) * @pageSize)  SET @ubound = @lbound + @pageSize + 1  IF @lbound >= @recct BEGIN  SET @ubound = @recct + 1  SET @lbound = @ubound - (@pageSize + 1) -- return the last page of records if -- no records would be on the  -- specified page  END  SET @STMT = '  SELECT identity(int,1,1) AS row, *  into #t  FROM ' + @datasrc + '  WHERE ' + @filter + ' order by [email protected]+   ' SELECT ' + @fieldlist +'   from #t  WHERE  row > ' + CONVERT(varchar(9), @lbound) + ' AND  row < ' + CONVERT(varchar(9), @ubound)  EXEC (@STMT) -- return requested records    print (@STMT)END
  相关解决方案