sqlserver2000 存儲過程分頁 -只有一張表

從網上找的,採用了遊標進行了存儲過程分頁,可是有一個問題,就是有兩張表,第一張表是空的,而第二張表纔是真正的有數據的。sql

如今修改了該存儲過程,使其只生成一張表。c#

 

create procedure [dbo].[Pr_QueryByPage]
@sqlstr nvarchar(4000), --查詢sql
@currentpage int, --第頁記錄條數
@pagesize int, --每頁顯示記錄
@tablename varchar(100) --表名
as
set nocount on
declare @P1 int, --P1是遊標的ID
@rowcount int, --
@sql varchar(100) --動態sql語句
--> 笨方法,用臨時表將空結果集接收了
set @sql = 'select top 0 * into ##tmpTable from '+@tablename
exec(@sql)
insert into ##tmpTable exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
--select ceiling(1.0*@rowcount/@pagesize) as TotalPage,@rowcount as [RowCount]
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
Drop Table ##tmpTable
set nocount off
GOfetch

 

傳入4個參數,sql語句,第幾頁,每頁多少條數據,表名。string

在查詢分析器中可使用如下sql語句:table

exec Pr_QueryByPage 'select * from table1  order by id',1,10,'table1'select

 

c#調用:sql語句

//執行分頁的存儲過程,注意:status=''0''是先後各兩個單引號,不是雙引號。
string sql = "exec Pr_QueryByPage 'select * from table1 a where status=''0'' ";
if ( baodanhao != "")
{
sql += " and a.baodanhao like ''%" + baodanhao + "%''";
}
sql += " order by id',"+page+","+pagesize+",'table1' ";
oledbHelper = new OleDBHelper();
return oledbHelper.ExecuteQueryFy(sql);分頁

相關文章
相關標籤/搜索