【问题标题】:How to execute multiple SQL queries in a loop cycle如何在一个循环循环中执行多个 SQL 查询
【发布时间】:2019-11-29 15:09:52
【问题描述】:

我被困在如何完成标题中提到的任务,你看,几天,也许几周我问如何创建某种搜索方法来包括整个月,这与此有关我又被困的任务-.-'

我遇到了搜索方法,但我不知道如何要求数据库带来与搜索匹配的所有行,例如,假设我输入了对当年 11 月当月的搜索2019 年,我插入了 5 行,我需要搜索返回当年这个月插入的这 5 行。

这是我目前正在尝试的,但没有结果

下面提到的变量:'num_month'和'num_year'是通过搜索组合框插入到另一个页面中,'cn_body'这个东西是存储在别处的连接字符串页面

'CREATION OF COUNTER FOR MONTHS OF 30 DAYS
    '------------------------------------------------------------------------------------
Set rs_Results = Server.CreateObject("ADODB.Recordset") -(creation of recordset for opening sql query)

    If month_num = 4 Or month_num = 6 month_num = 9 Or month_num = 11 Then -(checks the specific months with 30 days)

    For day_counter=1 To 30 -(creates a counter for days from 1 to 30 since is the case for months 

of 30 days only)

        search_date = cDate(day_counter &"-"& month_num &"-"& year_num) -(inserts the day counter along with the
 month and year counter separating them by "-" making it a valid date after the conversion)

        converted_date= Clng(search_date) -(converts the date of the variable 'search_date' to numbers)
    strSQL_CIP_Date="SELECT * FROM data_storage WHERE creation_date=" & converted_date 

    'cn_body.execute (strSQL_CIP_Date) -(when using this method on the portion of page that shows the results it throws 
an error which is: 'ADODB.Recordset error '800a0e78' Operation not allowed if the object is closed.', in this case the 
query is executed the times it should, but the results aren't shown because of the mentioned error)

        rs_Results.open strSQL_CIP_Date,cn_body,1,1 

(cn_body is the string connection which is stored into another page, 
    what i'm doing here is opening the sql query using the recordset which is the method i used for other queries without bigger issues,
    but for some reason here it is not working, only runs 2 times then it appears this error: 
    'ADODB.Recordset error '800a0e79'

'Operation not allowed if the object is open')

        response.write day_counter & " " & strSQL_CIP_Date & "<br><br><br><br>" 
-(prints the query with the converted date to ask to the database)
    Next
End If

我也尝试了循环方法,do,do while,for,if,do while not,loop until,while循环等等,并且得到了相同的结果。

所以你有它,我不知道我在代码中提到的任何错误的原因,任何形式的帮助都会很棒,请问你觉得需要什么,提前谢谢

【问题讨论】:

  • 等等……你现在在做什么?!?
  • 嗨@Lankymart 目前我正在尝试在 For 循环中执行查询,我需要的是在每个月的每一天执行查询并检索数据库中存在的查询
  • 我知道你在做什么,我只是震惊了。
  • 为什么?是不是太乱了?

标签: sql vbscript asp-classic


【解决方案1】:

您可以简单地查询所有记录 BETWEEN 两个日期,而不是在循环中为每个日期运行查询:

strSQL_CIP_Date = "SELECT * FROM data_storage WHERE creation_date BETWEEN '" & strStartDate & "' AND '" & strEndDate & "'"

根据您的数据库提供商,您可能需要在查询中使用 # 而不是 ' 作为日期分隔符:

strSQL_CIP_Date = "SELECT * FROM data_storage WHERE creation_date BETWEEN #" & strStartDate & "# AND #" & strEndDate & "#"

您也可以在查询中只使用月份:

strSQL_CIP_Date = "SELECT * FROM data_storage WHERE MONTH(creation_date) = 11"

【讨论】:

  • 不应该鼓励推荐一种不涉及保护的方法,例如使用参数化查询等。这两个答案都不足。
  • 感谢@Lankymart,您的第一个示例完成了这项工作,这比我想象的要容易得多,非常感谢!
  • @black-soul 这基本上与您在之前的问题中得到的答案相同。不知道你是如何在这几周内从那里到达这里的! stackoverflow.com/a/58881543/339440
【解决方案2】:

据我所知,由于您将日期转换为数字,因此数据库中的日期不是日期格式吗?如果您的数据库连接已经打开,这就是我的设置方式。

参数化查询的编辑函数。另外添加了一个函数,使参数化调用更容易。

Set Cn = Server.CreateObject("ADODB.Connection")
Cn.Open CONNECTION_INFO

function dbPara(sql, params)
    dim cmd
    set cmd = Server.CreateObject("ADODB.Command")
    with cmd
        .CommandText = sql
        set .ActiveConnection = cn
    end with

    if NOT isEmpty(params) then
        set rs = cmd.execute(, params)
    else
        set rs = cmd.execute()
    end if

    set dbPara = rs
end function

strSQL_CIP_Date = "SELECT * FROM data_storage WHERE creation_date BETWEEN ? AND ?"
set strRS = dbpara(strSQL_CIP_Date,array(converted_date, converted_date+29))
do until strRS.eof
   'insert code for each entry here
   strRS.movenext
loop

【讨论】:

  • 不应该鼓励推荐一种不涉及保护的方法,例如使用参数化查询等。这两个答案都不足。
  • @lankymart 您是绝对正确的,人们需要自行决定使用这些答案。我只是想帮助指出正确的方向。随意添加您认为更合适的答案。比说出当前答案有什么问题更有帮助。 :) 了解应该如何回答问题对于 OP 和像我这样的人来说都是很好的建议。
  • Stack Overflow 已经有很多在 ADODB 中使用参数化查询的示例。
  • @Lankymart 这看起来更好吗?如果您发现它有任何问题,请告诉我。我为参数化查询添加了一个函数,使它们更易于使用。
  • 您知道您不必创建ADODB.Connection,如果您传递ActivieConnection 一个有效的连接字符串,它将为您实例化并打开连接,并在释放命令时处理它。所以.ActiveConnection = CONNECTION_INFO 也可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
  • 1970-01-01
  • 2014-12-09
相关资源
最近更新 更多