--在Sql查询分析器中执行一下脚本建立存储过程p_splitpage
--create procedure p_splitpage
if exists(select * from dbo.sysobjects where id = object_id(N'[dbo].[p_splitpage]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_splitpage]
GO
![](/Images/OutliningIndicators/None.gif)
--利用SQL未公开的存储过程实现分页
CREATE PROCEDURE P_SPLITPAGE
(
@SQL NVARCHAR(4000), --要执行的SQL语句
@CURRENTPAGE INT = 2, --要显示的页码
@PAGESIZE INT = 10, --每页的大小
@PAGECOUNT INT = 0 OUT --总页数
)
AS
SET NOCOUNT ON
DECLARE @P1 INT
![](/Images/OutliningIndicators/None.gif)
EXEC SP_CURSOROPEN @P1 OUTPUT,@SQL,@SCROLLOPT = 1,@CCOPT = 1,@ROWCOUNT = @PAGECOUNT OUTPUT
![](/Images/OutliningIndicators/None.gif)
SET @PAGECOUNT = CEILING(1.0 * @PAGECOUNT / @PAGESIZE)
SET @CURRENTPAGE = (@CURRENTPAGE-1) * @PAGESIZE + 1
--SELECT @CURRENTPAGE
EXEC SP_CURSORFETCH @P1,16,@CURRENTPAGE,@PAGESIZE
EXEC SP_CURSORCLOSE @P1
![](/Images/OutliningIndicators/None.gif)
GO
![](/Images/OutliningIndicators/None.gif)
--然后在程序中就可以用以下方式调用,具体的参数请参见上面的脚本
EXEC p_splitpage 'select top 1000 id,name from sysobjects',2,10
ASP中的调用
var db = new DB("Provider=SQLOLEDB.1;Password=mysa;Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=.");
var strSql = "SELECT * FROM Products ORDER BY ProductID DESC";
db.setCmdType(adCmdStoredProc);
db.setCmdText("p_splitpage");
db.addCmdParam(adVarChar,1000,strSql,"@SQL");
db.addCmdParam(adInteger,4,1,"@CURRENTPAGE");
db.addCmdParam(adInteger,4,10,"@PAGESIZE");
db.addCmdParam(adInteger,4,0,"@PAGECOUNT",adParamOutput);
//取得输出参数必须执行无返回值的exeCmd方法
//而不能输出值和记录集同时得到,因为这是ADO的限制!![](/Emoticons/hitwall.gif)
db.exeCmd();
var params = db.getCmdParameters();
![](/Images/OutliningIndicators/None.gif)
for(var i=0;i<params.Count;i++)
Apps.printLn(params(i).Name + "," + params(i).Value);
![](/Images/OutliningIndicators/None.gif)
//取得rs的方法
var rs = db.exeCmdRs();
if(rs != null)
{
rs = rs.NextRecordset();
}
for(;!rs.EOF;rs.MoveNext())
{
Apps.printLn(rs(1));
}
db.clear();
Apps.print(Globals.getUseTime());