![](/img/trans.png)
[英]VBA RecordSet.RecordCount = -1 when using stored procedure and command
[英]Can't return recordcount with the stored procedure with the declaration in it
我编写了一个存储过程来返回记录,但是一旦我声明一个变量,我的asp classic就无法计算总记录集,它总是返回-1
如果有人可以解决这个问题,那将使我的工作变得容易得多,您的意见将不胜感激。
SQL Server 2000的存储过程代码
CREATE PROCEDURE sp_SalesTaxV3(
@ship_zip varchar(20)
)
AS
--problem if in enable the next 2 lines
DECLARE @tax_rate INT
set @tax_rate =0
--disable the above 2 line the asp will able to count.
--end problem
BEGIN
SELECT * FROM tax_rate where zip =''+@ship_zip+''
END
ASP经典代码:
<%
set strGetTaxZips = Server.CreateObject("ADODB.RecordSet")
strSQLGetTaxZips = "EXECUTE sp_SalesTaxV3 '"&user_zip &"'"
Response.Write(strSQLGetTaxZips)
strGetTaxZips.Open strSQLGetTaxZips,tax_db,3
Response.Write("<BR>recordcount" &strGetTaxZips.recordcount)
%>
我会尽量把DECLARE
后 BEGIN
:
CREATE PROCEDURE sp_SalesTaxV3(@ship_zip varchar(20))
AS BEGIN
DECLARE @tax_rate INT
SET @tax_rate = 0
SELECT * FROM tax_rate WHERE zip = ''+@ship_zip+''
END
您需要包括Set NoCount On;
在存储过程的顶部。 该问题很可能是由于某种原因导致返回了受影响的行的列表,并且抛出了经典的ADO,并且它认为受影响的行是第一个记录集。
CREATE PROCEDURE sp_SalesTaxV3 ( @ship_zip varchar(20) )
AS
Begin
Declare @tax_rate int
Set NoCount On
Set @tax_rate = 0
Select *
From tax_rate
Where zip = @ship_zip
End
加成
如果问题是在RecordSet上获得RecordCount属性的准确值,则使用的Cursor类型会有所不同。 在您的示例中,通过向Open方法传递“ 3”来使用静态游标。 使用静态游标,您需要先调用MoveLast,然后RecordCount才正确。
Const adOpenForwardOnly = 0
Const adOpenStatic = 3
Dim sql
Dim rs
' btw, you should validate your inputs here
sql = "Exec sp_SalesTaxV3 '" & user_zip & "'"
Set rs = Server.CreateObject("ADODB.RecordSet")
rs.Open sql, tax_db, adOpenStatic
rs.MoveLast
rs.MoveFirst 'not needed if you are not going to cycle through the rows
'Now RecordSet will be accurate
Response.Write( "<br />RecordCount: " & rs.RecordCount )
Set rs = Nothing
顺便说一句,另一种解决方案是简单地将行计数作为列返回到输出中(或者如果不使用返回的任何行,则仅返回计数)。
CREATE PROCEDURE sp_SalesTaxV3 ( @ship_zip varchar(20) )
AS
Begin
Declare @tax_rate int
Set NoCount On
Set @tax_rate = 0
Select *
, (Select Count(*)
From tax_rate As T1
Where zip = @ship_zip ) as RowCount
From tax_rate
Where zip = @ship_zip
End
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.