分頁儲存過程

2021-09-05 14:25:24 字數 1933 閱讀 2587

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)

asdeclare  @strsql   varchar(2000)     -- 主語句

declare @strtmp   varchar(1000)     -- 臨時變數

declare @strorder varchar(1000)       -- 排序型別

if @ordertype != 0

begin

set @strtmp = "<(select min"

set @strorder = " order by [" + @fldname +"] desc"

endelse

begin

set @strtmp = ">(select max"

set @strorder = " order by [" + @fldname +"] asc"

endset @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

endif @iscount != 0

set @strsql = "select count(*) as total from [" + @tblname + "]"

exec (@strsql)

go

分頁儲存過程 分頁儲存過程

分頁儲存過程 alter proc dbo p pageshow pagesize int,每頁大小 currentpage int out,當前頁 housename nvarchar 50 房產名稱 totalcount int out,總記錄數 totalpage int out 總頁數 as...

分頁儲存過程

create proc p sobigo percentpage tblname varchar 255 t category 表名 strgetfields varchar 1000 需要返回的列 fldname varchar 255 排序的欄位名 pagesize int 10,頁尺寸 pag...

分頁儲存過程

create procedure pro select pageindex int,pagesize int as select student.sno,student.sname,student.s grade.math,grade.physics,grade.huaxue,grade.chine...