use [npu_organization]
go/****** object: storedprocedure [dbo].[up_getpagelist] script date: 08/24/2013 11:22:28 ******/
set ansi_nulls on
goset quoted_identifier on
goalter procedure [dbo].[up_getpagelist]
(@tbname varchar(255), -- 表名
@fields varchar(1000) = '*', -- 需要返回的列
@order varchar(255)='1', -- 排序的欄位名
@pagesize int = 10, -- 頁尺寸
@pageindex int = 1, -- 頁碼
@strwhere varchar(1500) = '', -- 查詢條件 (注意: 不要加 where)
@count int = 0 output, -- 返回記錄總數, 非 0 值則返回
@ordertype bit = 0 -- 設定排序型別, 非 0 值則降序)as
declare @strsql nvarchar(4000) -- 主語句
declare @strtmp varchar(110) -- 臨時變數
declare @strorder varchar(400) -- 排序型別
declare @getvalue varchar(400)
declare @roworder varchar(100)
set @strsql = 'select @count= count(1) from ' + @tbname + ' where 1=1 '+@strwhere
print @strsql
exec sp_executesql @strsql,n'@count int out',@count out
print @count
declare @sql nvarchar(2000)
declare @cou int
begin
if @ordertype = 0
begin
set @strtmp = '>(select max'
set @strorder = ' order by ' + @order +' desc'
set @roworder=' row '
--如果@ordertype不是0,就執行降序,這句很重要!
endelse
begin
set @strtmp = '<(select min'
set @strorder = ' order by ' + @order +' asc'
set @roworder=' row desc '
endif @pageindex = 1
begin
print @fields
set @strsql = 'select top ' + str(@pagesize) +' '+@fields+ ' from ' + @tbname + ' where 1=1 ' + @strwhere + ' ' + @strorder
--如果是第一頁就執行以上**,這樣會加快執行速度
endelse
begin
--以下**賦予了@strsql以真正執行的sql**
set @strsql = 'with temptb as(select top ' + str(@count) +' '+@fields+ ' from '
+ @tbname + ' where 1=1 ' +@strwhere+ @strorder +' )'
set @strsql+='select top '+ str(@pagesize) +' * from temptb where row '+@strtmp+'(row) from (select top '+ str(@pagesize*(@pageindex-1)) +' row from temptb) as row) order by '+@roworder
end
endprint @strsql
exec (@strsql)
分頁儲存過程 分頁儲存過程
分頁儲存過程 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...