【问题标题】:SQL will not insert into tableSQL 不会插入到表中
【发布时间】:2015-09-20 21:54:38
【问题描述】:

我提前为这个问题道歉 - 但我没有经典 ASP 的经验,虽然我尝试过,但我就是想不通。

我的任务是调整现有系统(直到找到替代品),它是用 ASP/VBScript 编写的。

我面临的问题是,当我使用 SQL 检索一些数据时,我无法获取将结果更新到表中的脚本。每次运行查询时,结果的数量都会发生变化。

例如;表中的数据计算某人缺席的时间,每次有人缺席时,它在一个字段中记录持续时间,在另一个字段中记录日期,在另一个字段中为记录提供value = 1

这部分有效。

我正在尝试做的是整理每个日期的总缺勤时间并将该数据插入到同一个表中,并带有value = 2

代码:

Set rssum = conn.execute("select sum(total) as total, visitdate from table_1 where repid=" &  session("repid") & " and type=1 group by visitdate  ")

If rssum.eof Then
  While Not rssum.eof
    rstotal=rssum("total")
    rstotal=rssum("visitdate")

    'some code to generate nextid
    Set abrec = conn.execute(insert into table_1 (abid,repid,type,visitdate,total) values(" & nextid & ", " & session("repid") & ",2,'" & visitdate & "'," & total & ") 
  Wend

  rssum.movenext

  rssum.close
  Set rssum = Nothing
End If

当我运行脚本并说我正在计算 2 天的数据时,它会返回 2 个 type=2 条目,但会重复第一个日期的日期。

为什么会这样?

【问题讨论】:

    标签: mysql sql-server-2008 vbscript asp-classic


    【解决方案1】:

    movenext 需要在循环的末尾,但在循环内。否则会无限重复。我很惊讶你没有得到一个无限循环。

        while not rssum.eof
    
          rstotal=rssum("total")
          rstotal=rssum("visitdate")
    
          'some code to generate nextid
    
          set abrec = conn.execute(insert into table_1 (abid,repid,type,visitdate,total) values(" & nextid & ", " & session("repid") & ",2,'" & visitdate & "'," & total & ") 
    
    
          rssum.movenext
          wend
    

    【讨论】:

    • Brillaint... 感谢您的回复。绝对成功了!
    • Jonathan,不客气,如果你喜欢这个答案,请接受。
    【解决方案2】:

    这里有几个问题。

    1.

    if rssum.eof then
    
      while not rssum.eof
    

    想想这个的逻辑,你是说如果一个条件存在那么只做这个当条件不存在时。您可能根本不需要条件语句,但是如果您想要一个仅适用于非空记录集的条件语句,那么这样做的方法是

    if not (rssum.eof and rssum.bof) then
    

    2.

    set abrec = conn.execute(...)
    

    这是一个插入命令而不是一个选择。你没有任何输出来填充记录集,所以输掉set abrec =

    1. 插入查询需要引号

    2. 正如 Ekkehard 指出的,您需要将 rssum("total") 和 rssum("visitdate") 的值分配给单独的变量,不要使用 rstotal 两次

    3. 正如 Kyle JV 在另一个答案中指出的那样,wend 需要在 rssum.movenext 之后,而不是之前。

    把它们放在一起,试试

    set rssum = conn.execute("select sum(total) as total, visitdate from table_1 where repid=" &  session("repid") & " and type=1 group by visitdate  ")
    
    if not (rssum.bof and rssum.eof) then
    
      while not rssum.eof
    
      total=rssum("total")
      visitdate=rssum("visitdate") 
    
      conn.execute("insert into table_1 (abid,repid,type,visitdate,total) values(" & nextid & ", " & session("repid") & ",2,'" & visitdate & "'," & total & ")" 
    
      rssum.movenext
      wend
    
      rssum.close
      set rssum = nothing
    
    end if 
    

    【讨论】:

    • 嗨,谢谢你的cmets,我是凭记忆写的(我不应该有),正如你恰当地强调的那样,有错误,我想知道的是如何创建循环机制
    • 很公平。就我个人而言,我更喜欢使用do while not...loopdo until...loopwhile not...wend,因为它更容易(至少对于说英语的人来说)理解代码在做什么。
    【解决方案3】:
    if rssum.eof then        ' if        EOF
       while not rssum.eof   ' while not EOF
    

    不可能工作并分配给rstotal两次:

    rstotal=rssum("total")
    rstotal=rssum("visitdate")
    

    也没有意义。

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多