-- 获取指定页的数据
create procedure getrecordfrompage
@tblname varchar(255), -- 表名
@fldname varchar(255), -- 字段名
@pagesize int = 10, -- 页尺寸
@pageindex int = 1, -- 页码
@iscount bit = 0, -- 返回记录总数, 非 0 值则返回
@ordertype bit = 0, -- 设置排序类型, 非 0 值则降序
@strwhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
as
declare @strsql varchar(6000) -- 主语句
declare @strtmp varchar(100) -- 临时变量
declare @strorder varchar(400) -- 排序类型
if @ordertype != 0
begin
set @strtmp = "<(select min"
set @strorder = " order by [" + @fldname +"] desc"
end
else
begin
set @strtmp = ">(select max"
set @strorder = " order by [" + @fldname +"] asc"
end
set @strsql = "select top " + str(@pagesize) + " * from ["
+ @tblname + "] where [" + @fldname + "]" + @strtmp + "(["
+ @fldname + "]) from (select top " + str((@pageindex-1)*@pagesize) + " ["
+ @fldname + "] from [" + @tblname + "]" + @strorder + ") as tbltmp)"
+ @strorder
if @strwhere != ''
set @strsql = "select top " + str(@pagesize) + " * from ["
+ @tblname + "] where [" + @fldname + "]" + @strtmp + "(["
+ @fldname + "]) from (select top " + str((@pageindex-1)*@pagesize) + " ["
+ @fldname + "] from [" + @tblname + "] where " + @strwhere + " "
+ @strorder + ") as tbltmp) and " + @strwhere + " " + @strorder
if @pageindex = 1
begin
set @strtmp = ""
if @strwhere != ''
set @strtmp = " where " + @strwhere
set @strsql = "select top " + str(@pagesize) + " * from ["
+ @tblname + "]" + @strtmp + " " + @strorder
end
if @iscount != 0
set @strsql = "select count(*) as total from [" + @tblname + "]"
exec (@strsql)
go
新闻热点
疑难解答