2012-07-19 43 views
1

當我通過SQL Server 2008運行SQL時,它工作得很好。但是,當我將它添加到經典的ASP網頁,然後從那裏調用它似乎打破。我不確定我缺少什麼?SQL Server記錄集臨時表過早關閉?

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 
+0

把它換成一個事務嗎?只有一條評論。 – Paparazzi 2012-07-19 20:50:39

+0

@Blam我從來沒有使用過交易,這是否會爲我提供更詳細的想法? – 2012-07-19 20:52:35

+0

我不確定是什麼導致了這個問題。交易可能會讓它消失。 http://msdn.microsoft.com/en-us/library/2k2hy99x(v=vs.100).aspx只有註釋 – Paparazzi 2012-07-19 21:42:44

回答

1

問題是@temp表。我將功能添加到存儲過程並刪除臨時表,它工作。

1

我們能看到你的GetRecordSet方法?

它聽起來像你關閉方法中的連接。
在關閉連接之前,您必須等待處理完記錄集。

考慮將連接傳遞給方法或使其成爲可在方法外部關閉的頁面級變量。

+0

我可以提供它,但GetRecordSet方法適用於100多個其他地方。現在編輯OP。 – 2012-07-19 20:39:09

+0

增加了GRS功能。 – 2012-07-19 20:40:40

+0

我鼓勵你看看這個功能http://www.w3schools.com/ado/showasp.asp?filename=demo_ado_getrows – bluelightning1 2012-07-19 20:47:24

1

此錯誤是由usualy開始SQL批處理的發言解析:

SET NOCOUNT ON 
1

有兩個解決方案

  • ,如果你不希望創建存儲過程,然後 開始SQL批處理與下面的語句

SET ANSI_WARNINGS OFF

SET NOCOUNT ON 
  • 你可能會把sql語句放在存儲過程中並調用它