最大的网站源码资源下载站,
存储过程 为 sql server 2000版本,请打开sql server 2000 的查询分析器执行下面的sql 语句。
程序用到的存储过程(仅支持主键排序)
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[sys_quicksortpaging]') and objectproperty(id, n'isprocedure') = 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 ' +
)
go
set quoted_identifier off
go
set ansi_nulls on
go
新闻热点
疑难解答
图片精选