开发者

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

开发者 https://www.devze.com 2023-02-14 02:57 出处:网络
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

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

If anyone has the solution to this 开发者_StackOverflowproblem it would make my job a lot easier and your input would be greatly appreciated.

Stored proceedure code for 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:

<%
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:

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; 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.

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. In your example, you are using a Static cursor by passing "3" to the Open method. Using a Static cursor, you need to call MoveLast before RecordCount will be accurate.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消