我有一个SQL查询,它使用Do Until循环从数据库中检索值。循环中SQLCondition的值显示正确,但如果我想在循环外显示值,则它不起作用:
If rReseller > 0 Then
sql2 = "SELECT rl.countryid FROM reseller_locator rl WHERE rl.resellerid = " & rReseller & " AND rl.countryid <> '" & iCountryID & "'"
'response.write sql2
Set rsResellerLocator=Conn.Execute(sql2)
If not rsResellerLocator.EOF Then
do until rsResellerLocator.eof
iRCountryID = rsResellerLocator("countryid")
SQLCondition = iRCountryID & ", "
response.write SQLCondition
rsResellerLocator.movenext
loop
Else
SQLCondition = ""
End If
response.write SQLCondition
End If
循环内部的response.write显示“2,3,4,5”。但是循环外部的response.write显示“5”。
如何将循环内部的值存储到字符串中以便在查询外使用?
答案 0 :(得分:0)
你确定内循环是否正常工作?我认为它没有理由显示&#34; 2,3,4,5&#34; - 您的代码现在的方式,它总是只显示一个数字。试试这个:
替换
SQLCondition = iRCountryID & ", "
使用
SQLCondition = SQLCondition & ", " & iRCountryID
这将保存一系列条件,否则您只是保存最新条件
答案 1 :(得分:0)
也使用此功能将需要的数据保存在字符串中
If rReseller > 0 Then
sql2 = "SELECT rl.countryid FROM reseller_locator rl WHERE rl.resellerid = " & rReseller & " AND rl.countryid <> '" & iCountryID & "'"
'response.write sql2
Set rsResellerLocator=Conn.Execute(sql2)
If not rsResellerLocator.EOF Then
do until rsResellerLocator.eof
iRCountryID = rsResellerLocator("countryid")
SQLCondition = iRCountryID & ", "
SQLConditionAll=""&SQLConditionAll&""&SQLCondition&""
response.write SQLCondition
rsResellerLocator.movenext
loop
Else
SQLCondition = ""
End If
response.write SQLCondition
End If
现在您可以使用Response.write SQLConditionAll 太用字符串显示需要的数据