存储过程 为 sql server 2000版本,请打开sql server 2000 的查询分析器执行下面的sql 语句。 程序用到的存储过程(仅支持主键排序)
ifexists (select*from dbo.sysobjects where id =object_id(n'[dbo].[sys_quicksortpaging]') andobjectproperty(id, n'isprocedure') =1) dropprocedure[dbo].[sys_quicksortpaging] go set quoted_identifier off go set ansi_nulls on go createprocedure sys_quicksortpaging ( @tablenvarchar(4000), --表名(必须) @primarykeyfieldnvarchar(50), --表的主键字段 @fieldnvarchar (4000)='*', --需要返回字段名(必须) @wherenvarchar(1000)=null, --where 条件(可选) @groupbynvarchar(1000) =null, --分组 @orderbynvarchar(1000)=null, --排序用到的字段() @pagenumberint=1, --要返回的页(第x页) (默认为第一页) @pagesizeint=10, --每页大小(默认为5) @recordcountint output --返回记录总数 ) as set nocount on declare@sorttablenvarchar(100) declare@sortnamenvarchar(100) declare@strsortcolumnnvarchar(200) declare@operatornvarchar(50) declare@typevarchar(100) declare@precint if@orderbyisnullor@orderby='' set@orderby=@primarykeyfield /**//* 获取用于定位的字段*/ ifcharindex('desc',@orderby)>0 begin set@strsortcolumn=replace(@orderby, 'desc', '') set@operator='<=' end else begin ifcharindex('asc', @orderby) =0 set@strsortcolumn=replace(@orderby, 'asc', '') set@operator='>=' end ifcharindex('.', @strsortcolumn) >0 begin set@sorttable=substring(@strsortcolumn, 0, charindex('.',@strsortcolumn)) set@sortname=substring(@strsortcolumn, charindex('.',@strsortcolumn) +1, len(@strsortcolumn)) end else begin set@sorttable=@table set@sortname=@strsortcolumn end select@type=t.name, @prec=c.prec from sysobjects o join syscolumns c on o.id=c.id join systypes t on c.xusertype=t.xusertype where o.name =@sorttableand c.name =@sortname ifcharindex('char', @type) >0 set@type=@type+'('+cast(@precasnvarchar) +')' declare@strstartrownvarchar(50) declare@strpagesizenvarchar(50) declare@strwherenvarchar(1000) declare@strwhereandnvarchar(1000) declare@strgroupbynvarchar(1000) if@pagenumber<1 set@pagenumber=1 set@strpagesize=convert (nvarchar(50), @pagesize) set@strstartrow=convert ( nvarchar(50), (@pagenumber-1)*@pagesize+1) if@whereisnotnulland@where!='' begin set@strwhere=' where '+@where set@strwhereand=' and '+@where end else begin set@strwhere='' set@strwhereand='' end if@groupbyisnotnulland@groupby!='' begin set@strgroupby=' group by '+@groupby end else begin set@strgroupby='' end declare@strsqlnvarchar(4000) set@strsql=' select @recordcount = count (*) from '+@table+@strwhere+''+@strgroupby exec sp_executesql @strsql,n'@recordcount int output',@recordcount output--计算总页数 exec ( ' declare @sort '+@type+' set rowcount '+@strstartrow+' select @sort = '+@strsortcolumn+' from '+@table+@strwhere+''+@strgroupby+' order by '+@orderby+' set rowcount '+@strpagesize+' select '+@field+' from '+@table+' where '+@strsortcolumn+@operator+' @sort '+@strwhereand+''+@strgroupby+' order by '+@orderby ) go set quoted_identifier off go set ansi_nulls on go