存储过程 为 sql server 2000版本,请打开sql server 2000 的查询分析器执行下面的sql 语句。
程序用到的存储过程(仅支持主键排序)
if exists (select * from dbo.sysobjects where id = object_id(n[dbo].[sys_quicksortpaging]) and objectproperty(id, nisprocedure) = 1)
drop procedure [dbo].[sys_quicksortpaging]
go
set quoted_identifier off
go
set ansi_nulls on
go
create procedure sys_quicksortpaging
(
@table nvarchar(4000), --表名(必须)
@primarykeyfield nvarchar(50), --表的主键字段
@field nvarchar (4000)=*, --需要返回字段名(必须)
@where nvarchar(1000)=null, --where 条件(可选)
@groupby nvarchar(1000) = null, --分组
@orderby nvarchar(1000)=null, --排序用到的字段()
@pagenumber int = 1, --要返回的页(第x页) (默认为第一页)
@pagesize int = 10, --每页大小(默认为5)
@recordcount int output --返回记录总数
)
as
set nocount on
declare @sorttable nvarchar(100)
declare @sortname nvarchar(100)
declare @strsortcolumn nvarchar(200)
declare @operator nvarchar(50)
declare @type varchar(100)
declare @prec int
if @orderby is null or @orderby =
set @orderby = @primarykeyfield
/* 获取用于定位的字段*/
if charindex(desc,@orderby)>0
begin
set @strsortcolumn = replace(@orderby, desc, )
set @operator = <=
end
else
begin
if charindex(asc, @orderby) = 0
set @strsortcolumn = replace(@orderby, asc, )
set @operator = >=
end
if charindex(., @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 = @sorttable and c.name = @sortname
if charindex(char, @type) > 0
set @type = @type + ( + cast(@prec as nvarchar) + )
declare @strstartrow nvarchar(50)
declare @strpagesize nvarchar(50)
declare @strwhere nvarchar(1000)
declare @strwhereand nvarchar(1000)
declare @strgroupby nvarchar(1000)
if @pagenumber < 1
set @pagenumber = 1
set @strpagesize = convert (nvarchar(50), @pagesize)
set @strstartrow = convert ( nvarchar(50), (@pagenumber - 1)*@pagesize + 1)
if @where is not null and @where !=
begin
set @strwhere = where + @where
set @strwhereand= and + @where
end
else
begin
set @strwhere =
set @strwhereand=
end
if @groupby is not null and @groupby !=
begin
set @strgroupby = group by + @groupby
end
else
begin
set @strgroupby =
end
declare @strsql nvarchar(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 支持任意字段排序的存储过程
if exists (select * from dbo.sysobjects where id = object_id(n[dbo].[sys_sortdatapager]) and objectproperty(id, nisprocedure) = 1)
drop procedure [dbo].[sys_sortdatapager]
go
set quoted_identifier on
go
set ansi_nulls on
go
create procedure sys_sortdatapager (
@table nvarchar(4000), --表名(必须)
@primarykeyfield nvarchar(50), --表的主键字段
@field nvarchar (4000)=*, --需要返回字段名(必须)
@where nvarchar(1000)=null, --where 条件(可选)
@groupby nvarchar(1000) = null, --分组
@orderby nvarchar(1000)=null, --排序用到的字段()
@pagenumber int = 1, --要返回的页(第x页) (默认为第一页)
@pagesize int = 10, --每页大小(默认为5)
@recordcount int out --返回记录总数
)
as
/*find the @primarykeyfield type*/
declare @pktable varchar(1000)
declare @pkname varchar(1000)
declare @type varchar(1000)
declare @prec int
if charindex(., @primarykeyfield) > 0
begin
set @pktable = substring(@primarykeyfield, 0, charindex(.,@primarykeyfield))
set @pkname = substring(@primarykeyfield, charindex(.,@primarykeyfield) + 1, len(@primarykeyfield))
end
else
begin
set @pktable = @table
set @pkname = @primarykeyfield
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 = @pktable and c.name = @pkname
if charindex(char, @type) > 0
set @type = @type + ( + cast(@prec as varchar) + )
declare @strpagesize varchar(50)
declare @strstartrow varchar(50)
declare @strwhere varchar(1000)
declare @strgroupby varchar(1000)
/*default sorting*/
if @orderby is null or @orderby =
set @orderby = @primarykeyfield
/*default page number*/
if @pagenumber < 1
set @pagenumber = 1
/*set paging variables.*/
set @strpagesize = cast(@pagesize as varchar(50))
set @strstartrow = cast(((@pagenumber - 1)*@pagesize + 1) as varchar(50))
/*set filter & group variables.*/
if @where is not null and @where !=
set @strwhere = where + @where +
else
set @strwhere =
if @groupby is not null and @groupby !=
set @strgroupby = group by + @groupby +
else
set @strgroupby =
/*execute dynamic query*/
declare @strsql nvarchar(4000)
set @strsql= select @recordcount = count (*) from + @table + @strwhere + + @strgroupby
exec sp_executesql @strsql,n@recordcount int output,@recordcount output--计算总页数
exec(
declare @pagesize int
set @pagesize = + @strpagesize +
declare @primarykeyfield + @type +
declare @tblpk table (
pk + @type + not null primary key
)
declare pagingcursor cursor dynamic read_only for
select + @primarykeyfield + from + @table + @strwhere + + @strgroupby + order by + @orderby +
open pagingcursor
fetch relative + @strstartrow + from pagingcursor into @primarykeyfield
set nocount on
while @pagesize > 0 and @@fetch_status = 0
begin
insert @tblpk (pk) values (@primarykeyfield)
fetch next from pagingcursor into @primarykeyfield
set @pagesize = @pagesize - 1
end
close pagingcursor
deallocate pagingcursor
select + @field + from + @table + join @tblpk tblpk on + @primarykeyfield + = tblpk.pk + @strwhere + + @strgroupby + order by + @orderby
)
go
set quoted_identifier off
go
set ansi_nulls on
go创建一个用于测试的表
create table [dbo].[employees] (
[employeesid] [numeric](18, 0) 