[英]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.