简体   繁体   English

在vbscript中的循环上删除重复项

[英]Remove duplicates on a loop in vbscript

I am getting an error on a classic asp page (error '80020009') 我在经典的ASP页面上遇到错误(错误“ 80020009”)

The simplified and cleaned code is the following 简化和清理的代码如下

  Set rs = server.createobject("ADODB.Recordset")

  rs.Fields.Append "eventnum", 3

  rs.Open
  rs.AddNew "eventnum", "26856"
  rs.AddNew "eventnum", "26857"
  rs.AddNew "eventnum", "26857"
  rs.Update


  rs.movefirst
  while not rs.eof

    holdereventnum = rs("eventnum")
    response.write rs("eventnum") & "<br>"

    rs.movenext

    if not rs.eof then
       while holdereventnum=rs("eventnum") and not rs.eof
           rs.movenext
       wend
    end if

   wend

So if the recordset returns 因此,如果记录集返回
23856 23856
26857 26857
26857 26857
it fails, but if returns 它失败,但是如果返回
26857 26857
26857 26857
23856 23856
it works, and I can't figure it out why. 它有效,但我不知道为什么。

The error is reported on the following line: 在以下行中报告该错误:

while holdereventnum=rs("eventnum") and not rs.eof

Any clue is welcome. 任何线索都欢迎。

i assume the error you're getting isn't related to the following line missing the THEN keyword and it was just a typo: 我认为您遇到的错误与缺少THEN关键字的以下行无关,这只是一个错字:

if not rs.eof

instead of trying to remove the duplicates in your asp code, can you update your sql to not select any duplicates by using the DISTINCT keyword in your sql? 可以尝试通过使用SQL中的DISTINCT关键字更新SQL以不选择任何重复项,而不是尝试删除ASP代码中的重复项?

select distinct( eventNum ) from table where....

The error itself is saying that there is no record to be read from, that it's indeed EOF, even with the IF statement check there. 错误本身就是说,即使使用IF语句检查,也没有要读取的记录,它的确是EOF。 I've never had much luck trying to get a loop within a loop using the same recordset to work. 我从未尝试过使用相同的记录集在一个循环中获得一个循环,这让我很幸运。 If you're unable to modify your sql, convert your RS to an array using GetRows, then loop through the array from within your WHILE loop 如果您无法修改SQL,请使用GetRows将RS转换为数组,然后从WHILE循环中遍历该数组

The solution was related to change the way how the loop was used. 该解决方案与更改循环的使用方式有关。

Set rs = server.createobject("ADODB.Recordset")

rs.Fields.Append "eventnum", 3

rs.Open
rs.AddNew "eventnum", "26856"
rs.AddNew "eventnum", "26857"
rs.AddNew "eventnum", "26857"
rs.Update


rs.movefirst
while not rs.eof

  holdereventnum = rs("eventnum")
  response.write rs("eventnum") & "<br>"

  rs.movenext

  if not rs.eof then
    do while holdereventnum=rs("eventnum") and not rs.eof
       rs.movenext
       if rs.eof then exit do end if
    loop
  end if

wend

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM