无效的For循环控制变量,嵌套的ResultSet SQL

时间:2013-10-18 10:11:13

标签: sql asp-classic vbscript

我正在尝试将初始SQL查询中的resultSet中的记录用作循环控件,并将值填充到后续的SQL语句中。然后,后面的SQL语句resultSet将用于填充各种HTML表。

在尝试实现此目的时,我遇到了这个问题

  

Microsoft VBScript编译错误'800a0410'

     

'for'循环控制变量无效

     

/ps/testcli.asp,line 83

     

对于RS.Fields中的每个objFieldValue

     

----------------------- ^

我最初的想法是,它无法识别客户端RS和RS,但似乎并非如此。下面的行从39 - 97

开始
SQLDel = "DELETE * FROM ClientModuleList"
SQLDel2 = "DELETE FROM PSS.dbo.TestCli"

'------   
SQLGetClientsV1MVSOld = "SELECT ClientID FROM PSS.dbo.CLIENTS WHERE CardpacVersion='Cardlink 1.0' AND OS='MVS'AND Base='Old'AND Active='1'"
SQLGetClientsV1MVSNew = "SELECT ClientID FROM CLIENTS WHERE CardpacVersion='Cardlink 1.0' , OS='MVS', Base='New', Active='1'"
SQLGetClientsV1AS4Old = "SELECT ClientID FROM CLIENTS WHERE CardpacVersion='Cardlink 1.0' , OS='AS4', Base='Old', Active='1'"
SQLGetClientsV2MVSOld = "SELECT ClientID FROM CLIENTS WHERE CardpacVersion='Cardlink 2.0' , OS='MVS', Base='Old', Active='1'"
'------
SQLGetModules01 = "SELECT Patchlib FROM "
SQLGetModules02 = " WHERE Type='B' AND Required='1'"

'-- Connection --
Set connDBO = Server.CreateObject("ADODB.Connection")

'-- ResultSets --
Set clientRS = Server.CreateObject("ADODB.RecordSet")
Set RS = Server.CreateObject("ADODB.RecordSet")

If Request.Form("submit") <> "" Then

'--Delete All Existing Rows --S001--    
    connDBO.open cString
    connDBO.execute(SQLDel2)
    connDBO.close
'--Delete All Existing Rows --E001--    

'--Get all Clients which satisfy (Cardlink 1.0, MVS, Old, Active) into clientsRS --S002--
    clientRS.Open SQLGetClientsV1MVSOld, cString

    While NOT clientRS.EOF
        For Each objFieldValue in clientRS.Fields
            SQLGetModuleFull = "" & SQLGetModule01 & objFieldValue.Value & SQLGetModule02 & ""
            RS.Open SQLGetModuleFull, cString

                Response.Write("<table border='1'>" )
                Response.Write("<tr>" )
                For Each objFieldName in RS.Fields
                    Response.Write("<th>" & objFieldName.Name & "</th>" )
                Next
                Response.Write("</tr>" )
                If NOT RS.EOF Then
                    While NOT RS.EOF
                        Response.Write("<tr>" )
                            For Each objFieldValue in RS.Fields
                                Response.Write("<td>" & objFieldValue.Value & "</td>" )
                            Next
                        Response.Write("</tr>" )
                        RS.MoveNext
                    WEnd        
                    Response.Write("</table>" )
                End If
        Next
    WEnd

    RS.Close
    clientRS.Close

End If

有关为何会发生这种情况的任何提示? 我对ASP Classic中使用的VBScript很新。

1 个答案:

答案 0 :(得分:4)

“嵌套循环时,每个循环必须具有唯一的元素变量。” http://msdn.microsoft.com/en-us/library/vstudio/5ebk1751.aspx

您不能在嵌套的For Each循环中重复使用相同的元素名称两次:

For Each objFieldValue in clientRS.Fields
[...]
    For Each objFieldValue in RS.Fields

将第二个更改为objFieldValue2(或更好的名称)并查看它是否编译。