ASP通过SQL语句中的数组循环

时间:2012-05-11 20:51:27

标签: sql arrays loops asp-classic insert

我的表单会有不同数量的“答案”字段。所以我尝试构建我的数组:

        match = "found"
        form_counter = 1
        i=0
        DIM pollanswer()
        do while match = "found"
        pollanswer(i) = lr_request_collection_dict("poll_answer" & form_counter)
            if pollanswer(i) = "" then
            match ="notfound"
            end if
        form_counter=form_counter+1
        i=i+1
        loop

稍后在我的代码中,我正在接受这些值,并希望将它们插入到我的数据库中,但这是我被卡住的地方,因为我需要自己插入第一个值然后循环其余的。

thisQuery = "insert into survey_2_surveyquestionanswers (surveyquestionanswer_surveyquestionid, "&_
            "surveyquestionanswer_answer, surveyquestionanswer_answerlabel, surveyquestionanswer_order) "&_
            "select @@identity, "& SQLQuote(pollanswer(0)) &", "& SQLQuote(pollanswer(0)) &", 1 "&_
            icount = 1
            ecount = 2
            for each arrValue in pollanswer
            "union select @@identity, "& SQLQuote(pollanswer) &", "& SQLQuote(pollanswer) &", "& SQLInt(ecount)
            ecount=ecount+1
            next
            ";"
            set thisRS = dbAccessObj.DirectQuery("live", thisQuery)

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

好吧,你似乎是用“for each”方法两次插入第一个条目。

您可以使用索引表单:

for a_index = 1 to Ubound(pollanswer)
    arrValue = pollanswer(a_index)
    ...
next

这只省略了第一个数组元素。