如何在循环中执行多个SQL查询
How to execute multiple SQL queries in a loop cycle
我被困在如何完成标题中提到的任务上,你看,几天,也许几周我问过如何创建某种搜索方法来包括整个月,嗯,这是相关的对于我再次陷入的那个任务 -.-'
我遇到了搜索方法,但我不知道如何要求数据库带来与搜索匹配的所有行,例如,假设我输入了 11 月的当前月份的搜索今年 2019 年,我插入了 5 行,我需要搜索 return 今年这个月插入的这五行。
这是我目前正在尝试但没有结果的方法
下面提到的变量:'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
我也尝试过循环方法 with, do, do while, for, if, do while not, loop until, while loop 等等,得到了相同的结果。
好了,我不知道我在代码中提到的任何错误的原因,任何类型的帮助都会很棒,请询问您是否需要,提前致谢
您可以简单地查询所有记录 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"
据我了解,由于您将日期转换为数字,所以数据库中的日期不是日期格式?如果您的数据库连接已经打开,这就是我的设置方式。
参数化查询的已编辑函数。另外添加了一个函数,使参数化调用更容易。
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
我被困在如何完成标题中提到的任务上,你看,几天,也许几周我问过如何创建某种搜索方法来包括整个月,嗯,这是相关的对于我再次陷入的那个任务 -.-'
我遇到了搜索方法,但我不知道如何要求数据库带来与搜索匹配的所有行,例如,假设我输入了 11 月的当前月份的搜索今年 2019 年,我插入了 5 行,我需要搜索 return 今年这个月插入的这五行。
这是我目前正在尝试但没有结果的方法
下面提到的变量:'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
我也尝试过循环方法 with, do, do while, for, if, do while not, loop until, while loop 等等,得到了相同的结果。
好了,我不知道我在代码中提到的任何错误的原因,任何类型的帮助都会很棒,请询问您是否需要,提前致谢
您可以简单地查询所有记录 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"
据我了解,由于您将日期转换为数字,所以数据库中的日期不是日期格式?如果您的数据库连接已经打开,这就是我的设置方式。
参数化查询的已编辑函数。另外添加了一个函数,使参数化调用更容易。
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