实现分页的例子-使用存储过程来实现分页
类别: ASP教程
这个 程序只能 达到 10 个 分页
if exists (select * from sysobjects where id = object_id(\'dbo.sp_productPage\') and sysstat & 0xf = 4)
drop procedure dbo.sp_productPage
GO
CREATE PROCEDURE sp_productPage
@intStart TINYINT=1,
@intEnd TINYINT=10
with ENCRYPTION
AS
Declare @strProductID VARCHAR(8),@strProductName VARCHAR(20),
@strSQL1 VARCHAR(100),
@strSQL2 VARCHAR(100),
@strSQL3 VARCHAR(100),
@strSQL4 VARCHAR(100),
@strSQL5 VARCHAR(100),
@strSQL6 VARCHAR(100),
@strSQL7 VARCHAR(100),
@strSQL8 VARCHAR(100),
@strSQL9 VARCHAR(100),
@strSQL10 VARCHAR(100),
@intCCount TINYINT,
@intCount TINYINT,
@i TINYINT
select @i=1
Declare cur_Product SCROLL CURSOR For
Select ProductID,ProductName from KF_Product order by ProductID
Select @intCCount=count(productId) From KF_Product
open cur_Product
Fetch ABSOLUTE @intStart From cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Select @intCount=@intStart
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL1=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount) +\' Union \'
End
else
Begin
Select @strSQL1=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL2=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL2=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL3=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL3=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL4=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL4=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL5=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL5=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL6=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL6=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL7=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL7=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL8=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL8=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL9=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL9=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL10=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
End
else
Begin
Select @strSQL10=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
EndPro:
close cur_Product
DEALLOCATE cur_Product
print @strSQL1
print @strSQL2
print @strSQL3
exec(@strSQL1 + @strSQL2+ @strSQL3+@strSQL4+@strSQL5+@strSQL6+@strSQL7+@strSQL8+@strSQL9+@strSQL10)
GO
存储 过程创建 成功后,就可以在ASP 中 做 如下 调用
strSQL="sp_productPage 1,10
rst.open strSQL,conn,3,1
就可以了
if exists (select * from sysobjects where id = object_id(\'dbo.sp_productPage\') and sysstat & 0xf = 4)
drop procedure dbo.sp_productPage
GO
CREATE PROCEDURE sp_productPage
@intStart TINYINT=1,
@intEnd TINYINT=10
with ENCRYPTION
AS
Declare @strProductID VARCHAR(8),@strProductName VARCHAR(20),
@strSQL1 VARCHAR(100),
@strSQL2 VARCHAR(100),
@strSQL3 VARCHAR(100),
@strSQL4 VARCHAR(100),
@strSQL5 VARCHAR(100),
@strSQL6 VARCHAR(100),
@strSQL7 VARCHAR(100),
@strSQL8 VARCHAR(100),
@strSQL9 VARCHAR(100),
@strSQL10 VARCHAR(100),
@intCCount TINYINT,
@intCount TINYINT,
@i TINYINT
select @i=1
Declare cur_Product SCROLL CURSOR For
Select ProductID,ProductName from KF_Product order by ProductID
Select @intCCount=count(productId) From KF_Product
open cur_Product
Fetch ABSOLUTE @intStart From cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Select @intCount=@intStart
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL1=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount) +\' Union \'
End
else
Begin
Select @strSQL1=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL2=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL2=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL3=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL3=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL4=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL4=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL5=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL5=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL6=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL6=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL7=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL7=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL8=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL8=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL9=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)+\' Union \'
End
else
Begin
Select @strSQL9=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
Fetch cur_Product Into @strProductID,@strProductName
if @@FETCH_STATUS=0
Begin
Select @intCount=@intCount+1
Select @strSQL10=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
End
else
Begin
Select @strSQL10=\'Select productId=\'\'\' + @strProductID + \'\'\',productName=\'\'\' + @strProductName+ \'\'\',ProductCount=\' + convert(VARCHAR,@intcCount)+\',ProductSumCount=\'+ convert(VARCHAR,@intCCount)
Goto EndPro
End
EndPro:
close cur_Product
DEALLOCATE cur_Product
print @strSQL1
print @strSQL2
print @strSQL3
exec(@strSQL1 + @strSQL2+ @strSQL3+@strSQL4+@strSQL5+@strSQL6+@strSQL7+@strSQL8+@strSQL9+@strSQL10)
GO
存储 过程创建 成功后,就可以在ASP 中 做 如下 调用
strSQL="sp_productPage 1,10
rst.open strSQL,conn,3,1
就可以了
- 上一篇: 用ASP访问数据库的几种常见方式
- 下一篇: ASP调用SQL SERVER存储程序
-= 资 源 教 程 =-
文 章 搜 索