【问题标题】:ASP Classic Recordset In Stored Procedure -1 Recordcount存储过程中的 ASP 经典记录集 -1 Recordcount
【发布时间】:2019-06-10 20:59:22
【问题描述】:

我正在使用带有 3 个参数的经典 ASP 中的 SQL Server 存储过程。我正在尝试查找记录数,但它返回“-1”。
我看到了一个类似的post,按照它的建议做了(检查游标类型,并在存储过程中添加'set nocount on'),但这些更改并没有影响-1 记录计数。

这是我在经典 ASP 页面中的代码,如下所示。

strInterestName = request("InterestName") 
strActiveDate = request("activedate")
strExpireDate = request("expiredate")


Set objCommandSec = CreateObject("ADODB.Command") 
Set objRS = CreateObject("ADODB.RecordSet") 
objRS.cursorlocation = 3
objRS.cursortype = adOpenStatic 


With objCommandSec  
Set .ActiveConnection = objConnection  
.CommandText = "[01_cms_search_pg_select_news_items_4]"  
.CommandType = 4   
.Parameters.Append .CreateParameter("@InterestName",adVarChar,
adParamInput, 25)              
.Parameters.Append .CreateParameter("@ActiveDate",adDate, adParamInput)  
.Parameters.Append .CreateParameter("@ExpireDate",adDate,
    adParamInput)
.Parameters("@InterestName") = strInterestName
.Parameters("@ActiveDate") = strActiveDate   
.Parameters("@ExpireDate") = strExpireDate

 set objRS =.Execute()   
End With  

这是存储过程的代码,如下:

ALTER PROCEDURE [dbo].[01_cms_search_pg_select_news_items_4]

@InterestName varchar(50), 
@ActiveDate datetime, 
@ExpireDate datetime


AS DECLARE @sql nvarchar(4000)

SELECT @sql = ' SELECT * ' + 
              ' FROM news ' +
              ' WHERE ' +
              ' bulletin_display_indicator = ''true'' '+
              ' AND ' +
              ' website_homepg_display_indicator= ''false'' '



IF @InterestName is not null    
SELECT @sql = @sql + ' AND (InterestName = @InterestName) 

IF @ExpireDate is not null and @ExpireDate IS NOT NULL
SELECT @sql = @sql + ' AND (expiredate between @ActiveDate and @ExpireDate)


SELECT @sql = @sql + '; '

EXEC sp_executesql @sql, N'@InterestName varchar(50), @ActiveDate
DateTime, @ExpireDate DateTime',@InterestName, @ActiveDate,
@ExpireDate

【问题讨论】:

    标签: stored-procedures asp-classic ado recordset


    【解决方案1】:

    我为此苦苦挣扎了一段时间,然后找到了适合我的东西。 它不漂亮,但它可以完成工作。

    让您的存储过程返回 2 个记录集:一个包含您需要的表数据,然后一个包含记录数:

    SELECT <all_you_need> FROM <your_table> WHERE <your_arguments>
    SELECT [myCount] = @@ROWCOUNT
    

    然后,在您的 ASP 文件中:

    dim objRS, dataRS, countRS
    Set objRS = CreateObject("ADODB.RecordSet") 
    Set dataRS = CreateObject("ADODB.RecordSet") 
    Set countRS = CreateObject("ADODB.RecordSet") 
    [your original call to the stored procedure]
    set objRS =.Execute()
    set dataRS = objRS
    set countRS = objRS.nextrecordset()
    

    countRS 现在包含一个记录集,其中包含一个名为“myCount”的单行和单列,您可以查询以获取记录数。 dataRS 包含您的原始数据集。

    注意:如果您在处理数据集之前不需要知道记录数,您可以这样简化:

    dim objRS, countRS
    Set objRS = CreateObject("ADODB.RecordSet") 
    Set countRS = CreateObject("ADODB.RecordSet") 
    [your original call to the stored procedure]
    set objRS =.Execute()
    [process your dataset as you need to]
    set countRS = objRS.nextrecordset()
    

    【讨论】:

    • 谢谢!为了访问记录计数,我会访问 countRS("myCount") 吗?我试过这个,但我得到“在与请求的名称或序号相对应的集合中找不到项目。”
    • 我是否需要将 myCount 设置为存储过程中的变量(例如,设置 @myCount=@@ROWCOUNT)?我也一直在尝试,但得到的是:“过程或函数......需要参数'@myCount',但未提供。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多