简体   繁体   English

无法使用包含声明的存储过程返回recordcount

[英]Can't return recordcount with the stored procedure with the declaration in it

I wrote a stored procedure to return the record but as soon as I declare a variable my asp classic can't do count the total recordset, it always returns -1 我编写了一个存储过程来返回记录,但是一旦我声明一个变量,我的asp classic就无法计算总记录集,它总是返回-1

If anyone has the solution to this problem it would make my job a lot easier and your input would be greatly appreciated. 如果有人可以解决这个问题,那将使我的工作变得容易得多,您的意见将不胜感激。

Stored proceedure code for SQL Server 2000 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 Classic code: 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)
%>

I would try to put the DECLARE after the BEGIN : 我会尽量把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

You need to include Set NoCount On; 您需要包括Set NoCount On; at the top of your stored procedure. 在存储过程的顶部。 The problem is likely that something is causing a list of rows affected to be returned and that is throwing off classic ADO and it thinks that rows affected is the first recordset. 该问题很可能是由于某种原因导致返回了受影响的行的列表,并且抛出了经典的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

Addition 加成

If the problem is getting an accurate value for the RecordCount property on the RecordSet, then the type of Cursor you use makes a difference. 如果问题是在RecordSet上获得RecordCount属性的准确值,则使用的Cursor类型会有所不同。 In your example, you are using a Static cursor by passing "3" to the Open method. 在您的示例中,通过向Open方法传递“ 3”来使用静态游标。 Using a Static cursor, you need to call MoveLast before RecordCount will be accurate. 使用静态游标,您需要先调用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

Btw, another solution is to simply return the row count as a column in the output (or just the count if you are not going to use any of the rows returned ). 顺便说一句,另一种解决方案是简单地将行计数作为列返回到输出中(或者如果不使用返回的任何行,则仅返回计数)。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM