页面查询时出现“查询过于复杂!”
这是网上搜集的资料
一个动态字符串数组 A() A(1)="acdc" A(2)="aghdc" A(3)="a2cdc" A(4)="agh4dc …… A(250)="ac78dc" A(251)="a6ghdc ……
查找数据库中字段“name”等于数组A中任意一项的记录
我的笨办法是用一个循环遍历数组生成SQL语句, select * from DB where name='acdc' or name='aghdc' or …………
在数组A行数小于253的时候,也就是or条件小于253时,SQL语句正常执行, 在数组A行数大于253的时候,SQL语句出错,提示“查询过于复杂!”
请教有什么更高效的办法,并且需要解决“查询过于复杂”这个问题
谢
------------------------------------------------------------------------------------ 可以用循环 FOR I=1 TO 255 RR="select * from DB where name=" & A(I) &"'" SET FF=CURRENTDB.OPENRESCORDSET(RR) IF FF.EOF MSGBOX "NO" ELSE MSGBOX "YES" EXIT FOR END IF NEXT -------------------------------------------------------------------------------------- dim strSql as string strSql="select * from DB where name in('" &join(A,"','") &"')" …… -------------------------------------------------------------------------------------- access对于or的使用有限制。
可以通过如下方式解决:
(1)把数组的所有元素追加到一个表中,然后用
select * from DB where name in (select 列名 from 表)
(2)用代码动态生成数组元素值的串,使得串的格式类似:
dim str as string dim strSQL as string str="'acdc','aghdc','a2cdc'" strSQL="select * from DB where name in (" & str & ")"
--------------------------------------------------------------------------------------
总结:
str="'acdc','aghdc','a2cdc'" 此代码中如果查询的是数字,则不加引号
网站页面参照myshop_course_file.asp |