use [bbs]
go/****** object: storedprocedure [dbo].[page] script date: 12/18/2010 13:45:41 ******/
set ansi_nulls on
goset quoted_identifier on
go*@curentpage 當前頁
*@pagesize 每頁記錄數
*@tablename 表名
*@key 主鍵(自動排序)
*@where 查詢條件
1)空為 null
2)有查詢條件不要帶where
*@order '0'表示 desc '1'是asc
*@pagecount 總頁數
alter procedure [dbo].[page]
@currentpage int,@pagesize int,
@tablename varchar(30),@key varchar(30),
@where varchar(50),@order varchar(1),
@pagecount int ,@str varchar(450) output
asbegin
---------------執行的sql語句------------
declare @sql nvarchar(400),@ordreby nvarchar(200)
declare @tempsql1 varchar(200),@tempsql2 varchar(200)
---------------記錄總數-----------------
declare @count int
---------------臨時變數------------------------
declare @temp1 int,@temp2 int
set @tablename=' '+@tablename+' '
set @key=' '+@key+' '
if @order='0'
set @ordreby=' order by '+@key+'desc'
else
set @ordreby=' order by '+@key
if @where='null'
set @sql='select @count = count(*) from '+ @tablename
else
set @sql='select @count = count(*) from '+ @tablename+' where '+@where
------------@count 付值(宣告變數@count 在說明是output 內型)---------------------------
exec sp_executesql @sql,n'@count int out',@count out
------------求總頁數------------------------------
if (@count%@pagesize)=0
set @pagecount=@count/@pagesize
else
set @pagecount=@count/@pagesize+1
-----------判斷顯示當前頁是否異常------------------
if @currentpage>@pagecount
set @currentpage=@pagecount
if @currentpage<1
set @currentpage=1
----------記錄數小於頁面顯示記錄數-----------------
if(@currentpage=1)
begin
if @where='null'
set @where=' '
else
set @where=' where '+@where
set @sql = 'select top'+ str(@pagesize)+' * from '+@tablename+@where+@ordreby
endelse
begin
/* ---------------desc----------------------
*@temp1表示前面的記錄
*@temp2表示後面的記錄
*假設一共77個記錄,每次取10個。取67~58(第2頁),去掉前面的57(1~57)個和後面的10個(77~66)
if @order=0
begin
set @temp1 = @count-@currentpage*@pagesize
if @temp1<0
set @temp1=0
set @temp2 = (@currentpage - 1)*@pagesize
if @where='null'
begin
set @tempsql1='select top ' + str(@temp1)+' '+@key+' from ' + @tablename+' order by ' +@key
set @tempsql2='select top ' + str(@temp2)+' '+@key+' from ' + @tablename + @ordreby
endelse
begin
set @tempsql1='select top ' + str(@temp1)+' '+@key+' from ' + @tablename+' where '+@where+' order by ' +@key
set @tempsql2='select top ' + str(@temp2)+' '+@key+' from ' + @tablename+' where '+@where+@ordreby
end
set @sql=' select top ' + str(@pagesize) + ' * from ' + @tablename + ' where '+@key+ ' not in '
set @sql= @sql+' ( '+ @tempsql1 +' ) and '
set @sql= @sql+@key+ ' not in ( '+@tempsql2 +' ) '
if @where='null'
set @sql= @sql+@ordreby
else
set @sql= @sql+' and '+@where+@ordreby
end
/* ----------------asc---------------------
* @temp 表示前面顯示的記錄總數
* 去掉 @temp 在取出 pagesize 個即可
else
begin
set @temp1=(@currentpage-1)*@pagesize
if @where='null'
set @tempsql1='select top '+ str(@temp1)+' '+@key+' from ' + @tablename + @ordreby
else
set @tempsql1='select top '+ str(@temp1)+' '+@key+' from ' + @tablename ++' where '+@where+@ordreby
set @sql=' select top ' + str(@pagesize) + ' * from ' + @tablename + ' where '+@key+ ' not in '
set @sql=@sql+' ( '+@tempsql1+' ) '
if @where='null'
set @sql= @sql+@ordreby
else
set @sql= @sql+' and '+@where+@ordreby
endend
set @str=@sql
--exec sp_executesql @sql
end
分頁儲存過程 分頁儲存過程
分頁儲存過程 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...