如何在循环周期中执行多个SQL查询

问题描述 投票:0回答:1

我被困在标题上提到的任务的处理上,几天,甚至几周,我问过如何创建某种搜索方法以包含整个月,这与该任务有关我再次陷入困境-.-'

我遇到了搜索方法,但是我不知道如何让数据库带出所有与搜索匹配的行,例如,假设我输入了对当前年份2019年11月的当前月份的搜索,并且我插入了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

我还尝试过使用,执行,执行while,for,if,while进行循环的方法,循环执行,直到while循环等等,并且结果相同。

所以您有它,我不知道我在代码中提到的任何错误的原因,任何形式的帮助都将非常有用,请问您有什么需要,谢谢,谢谢

sql vbscript asp-classic
1个回答
0
投票

您可以简单地查询两个日期的所有记录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"
© www.soinside.com 2019 - 2024. All rights reserved.