请问如何实现多个(5个以上)动态变量的SQL查询
是这样的,我的数据库有比较多的字段,为实现同时多条件的动态查询,我做了多个combobox用来当做选择查询的条件,如果3-4个还好,可以用 if... then 解决,combobox多了就麻烦了,请问如何解决。 问题点数:20、回复次数:13Top
1 楼Leftie(左手,为人民币服务)回复于 2003-06-04 08:27:52 得分 0
dim tmp1 as string
dim tmp2 as string
...
dim strsql as string
if trim(combobox1.text)="" then
tmp1=""
else
tmp1=" and 字段1='"& combobox1.text &"'"
end if
if trim(combobox2.text)="" then
tmp2=""
else
tmp2=" and 字段2='"& combobox2.text &"'"
end if
...
'构造SQL语句
strsql=" select * from tablename where 1=1 " & tmp1 & tmp2
if rs.state=adstateopen then rs.close
rs.open strsql,cn,adopenkeyset,adlockreadonly
Top
2 楼xinjiana(桃木剑)回复于 2003-06-04 08:29:33 得分 0
dim sqlstr as string
sqlstr="select * from table where 1=1"
if combo1.text<>"" then
sqlstr=sqlstr+" and columnname='"&combo1.text&"'"
endif
if combo2.text<>"" then
sqlstr=sqlstr+" and columnname='"&combo2.text&"'"
endif
...............................
要不就是用控件数组
case 1
sqlstr=sqlstr+" and columnname='"&combo1.text&"'"
case 2
sqlstr=sqlstr+" and columnname='"&combo2.text&"'"Top
3 楼gemgama(gemgama)回复于 2003-06-04 12:23:24 得分 0
谢谢,不过我的意思不是全部的条件我都要选,而是选个别的(几个以上的),主要问题就是SQL里有一个"AND" 比较难处理,请教请教.Top
4 楼spland(spland)回复于 2003-06-04 12:42:38 得分 0
DIM bAndNeed AS BOOLEAN
If Trim(A.Text) <> "" Then
StrSql = IIf(bAndNeed = True, StrSql & " AND ", StrSql & " WHERE ")
StrSql = StrSql & " s_kigen_cd ='" & A.TEXT & "'"
bAndNeed = True
End If
Top
5 楼AechoJohn(江江)回复于 2003-06-04 12:45:29 得分 0
Leftie(左手) 的Top
6 楼gemgama(gemgama)回复于 2003-06-04 12:53:33 得分 0
还是不行,如果我只选最后一个条件就出现SQL的语法错误,SQL中多了一个"And".Top
7 楼zjcxc(邹建)回复于 2003-06-04 12:54:50 得分 0
用控件控件数组处理吧!
你参考一下我在这个贴子中的回复,应该对你的问题有帮助
http://expert.csdn.net/Expert/topic/1653/1653028.xml?temp=.7921106Top
8 楼gemgama(gemgama)回复于 2003-06-04 19:35:48 得分 0
谢谢上边的师傅,但我不是说要全部的条件都要!比如说我只要第一、第四呢?这样不就多了一个“and" 了吗?(我的条件是装在datacombobox里的,有好多个),查全部的条件当然没问题,但如果要挑选个别的就不知道如何写了。请教各位高手。Top
9 楼myhot(大头河马)回复于 2003-06-04 19:45:26 得分 0
先置一临时变量dim StrSql as string
StrSql = StrSql & IIf(StrSql="", & " where ", " and ") & 条件
Top
10 楼zjcxc(邹建)回复于 2003-06-04 20:37:37 得分 20
你仔细看一下我在
http://expert.csdn.net/Expert/topic/1653/1653028.xml?temp=.7921106
发表的东西,它是不会存在你所说的多一个and的问题的Top
11 楼bigbigfans(小骁)回复于 2003-06-04 20:39:02 得分 0
为什么不这样写
if trim(combobox1.text)="" then
tmp1="1=1"
else
tmp1=" and 字段1='"& combobox1.text &"'"
end if
if trim(combobox2.text)="" then
tmp2="1-1"
else
tmp2=" and 字段2='"& combobox2.text &"'"
end if
...
如果条件某个条件没选就变成了... And 1=1 and ... ,问题就过了Top
12 楼zjsm96441125(流星物语)回复于 2003-06-04 23:56:47 得分 0
sSql= "Select * From Table Where cKey='??'
if 条件1 then sSql=sSql + " And 字段1='" & combo1.text & "'"
if 条件2 then sSql=sSql + " And 字段2='" & combo1.text & "'"
if 条件3 then sSql=sSql + " And 字段3='" & combo1.text & "'"
if 条件4 then sSql=sSql + " And 字段4='" & combo1.text & "'"
关键是 Where cKey='??' 必须要添一个字段 cKey='??' 比如说 某个关键 cKey is Not Null 不过这样可能会影响速度
完全满足你的要求
Top
13 楼bosket(shaw)回复于 2003-06-05 11:23:37 得分 0
sSql= "Select * From Table Where 1=1"
if 条件1 then sSql=sSql + " And 字段1='" & combo1.text & "'"
if 条件2 then sSql=sSql + " And 字段2='" & combo1.text & "'"
if 条件3 then sSql=sSql + " And 字段3='" & combo1.text & "'"
if 条件4 then sSql=sSql + " And 字段4='" & combo1.text & "'"
Top




