1

当我通过 SQL Server 2008 运行 SQL 时,它工作得很好。但是,当我将它添加到经典的 ASP 网页然后从那里调用它时,它似乎中断了。我不确定我错过了什么?

sql = ""
sql = sql & " DECLARE @listStr VARCHAR(MAX)"

sql = sql & " DECLARE @temp TABLE ("
sql = sql & " DistID varchar(30),"
sql = sql & " FName varchar(30),"
sql = sql & " LName varchar(30),"
sql = sql & " RankID int, "
sql = sql & " PSV int,"
sql = sql & " ShipCountry varchar(30),"
sql = sql & " ShipState varchar(30) )"

sql = sql & " INSERT INTO @temp"
sql = sql & " EXEC  [dbo].[MSGGetList]"
sql = sql & " @List = N'" & request("report_type") & "',"
sql = sql & " @DistID = " & Session("DistributorID") & ","
sql = sql & " @BusCtrID = 1,"

' Autoship Filter Params
If request("autoship") <> "" Then
    sql = sql & " @FilterAutoship = '" & request("autoship") & "',"
Else
    sql = sql & " @FilterAutoship = NULL,"
End If

sql = sql & " @ItemID = " & request("item_id") & ","

' Order Filter Params
If request("orders") <> "" Then
    sql = sql & " @Orders = '" & request("orders") & "',"
Else
    sql = sql & " @Orders = NULL,"
End If

If request("minvol") <> "" Then
    sql = sql & " @MinVol = " & request("minvol") & ","
Else
    sql = sql & " @MinVol = NULL,"
End If

If request("minpostamt") <> "" Then
    sql = sql & " @MinPostAmt = " & request("minpostamt") & ","
Else
    sql = sql & " @MinPostAmt = NULL,"
End If

If request("startdate") <> "" Then
    sql = sql & " @DateStart = '" & request("startdate") & "',"
Else
    sql = sql & " @DateStart = NULL,"
End If

If request("enddate") <> "" Then
    sql = sql & " @DateEnd = '" & request("enddate") & "',"
Else
    sql = sql & " @DateEnd = NULL,"
End If

' Location Filter Params
If request("country") <> "" Then
    sql = sql & " @Country = '" & request("country") & "',"
Else
    sql = sql & " @Country = NULL,"
End If

If request("region") <> "" Then
    sql = sql & " @Region = '" & request("region") & "',"
Else
    sql = sql & " @Region = NULL,"
End If

' Rank Filter Params
If request("rankid") <> "" Then
    sql = sql & " @RankID = '" & request("rankid") & "',"
Else
    sql = sql & " @RankID = NULL,"
End If

'Do Not send list
If request("DoNotSend") <> "" Then
    sql = sql & " @Exempt = '" & request("DoNotSend") & "',"
End If

' Volume Filter Params
sql = sql & " @MinPV = " & request("min_pv") & ","
sql = sql & " @MaxPV = " & request("max_pv") & ","
sql = sql & " @MinGV = " & request("min_gv") & ","
sql = sql & " @MaxGV = " & request("max_gv") & ","
sql = sql & " @MinLBVRBV = NULL"
sql = sql & " SELECT @listStr = COALESCE(@listStr+',' ,'') + DistID FROM @temp"
sql = sql & " SELECT @listStr AS ToList"
stop
response.write(sql & "<br />")
Set rsToList = GetRecordSet(sql)


response.Write(rsToList("ToList"))

当我单步执行代码并查看rsToList它时说:

对象关闭时不允许操作

知道我在这里缺少什么吗?

编辑:

Function getRecordset(strSQL)

        If Application("DebugSQL") Then
            Call WriteSQL(strSQL)
        End If

        'Create Database Connection
        Set FunctionDBConn = Server.CreateObject("ADODB.Connection")
        FunctionDBConn.ConnectionTimeout = 180
        FunctionDBConn.Open(Application("DB_ConnectionString"))
        Set adoRS = Server.CreateObject("ADODB.Recordset")

        adoRS.CursorLocation = 3
        adoRS.LockType = 4

        'Create Recordset
        adoRS.Open strSQL, FunctionDBConn
        Set adoRS.ActiveConnection = Nothing
        Set GetRecordset = adoRS

        'Close Database Connection
        FunctionDBConn.Close
        Set FunctionDBConn = Nothing

    End Function
4

4 回答 4

1

我们可以看看你的 GetRecordSet 方法吗?

听起来您在方法中关闭了连接。
您必须等到完成对记录集的处理后才能关闭连接。

考虑将连接传递给方法或使其成为可以在方法外部关闭的页面级变量。

于 2012-07-19T20:37:35.730 回答
1

此错误通常通过使用以下语句开始 SQL 批处理来解决:

SET NOCOUNT ON
于 2012-07-19T21:48:08.747 回答
1

问题是@temp 表。我将功能添加到存储过程中并删除了临时表并且它起作用了。

于 2012-07-26T17:32:20.713 回答
1

有两种解决方案

  • 如果您不想创建存储过程,则使用以下语句开始 sql 批处理

设置 ANSI_WARNINGS 关闭

SET NOCOUNT ON
  • 您可以将sql语句放在存储过程中并调用它
于 2013-11-11T11:33:26.447 回答