接收四个参数,判断四个参数是否为null,根据判断结果,查询数据库,但是没有按照设想执行,这是什么原因??
sybm=request("sybm")
sydd=request("sydd")
jdyf=request("jdyf")
syr=request("syr")
set rs_cjtzd =server.createobject("ADODB.RecordSet")
if (IsNull(sybm) and IsNull(sydd) and IsNull(syr)) then
sql_cjtzd="select * from jdjzjh where sybm is null and sydd is null and syr is null and jdyf='"&jdyf&"'"
end if
if ((Not IsNull(sybm)) and IsNull(sydd) and IsNull(syr)) then
sql_cjtzd="select * from jdjzjh where sybm='"&sybm&"' and sydd is null and syr is null and jdyf='"&jdyf&"'"
end if
if ((Not IsNull(sybm)) and (Not IsNull(sydd)) and IsNull(syr)) then
sql_cjtzd="select * from jdjzjh where sybm='"&sybm&"' and sydd='"&sydd&"' and syr is null and jdyf='"&jdyf&"'"
end if
if ((Not IsNull(sybm)) and (Not IsNull(sydd)) and (Not IsNull(syr))) then
sql_cjtzd="select * from jdjzjh where sybm='"&sybm&"' and sydd='"&sydd&"' and syr='"&syr&"' and jdyf='"&jdyf&"'"
end if
rs_cjtzd.open sql_cjtzd,conn,1,1
这个是多条件组合查询吧。
不应该用IF来判断条件,太复杂,容易出错。
你可以考虑用拼接的方式
dim sql
sql = "select * from 表 where 1=1"
if sybm<>"" then
sql = sql &" and 字段名1="&sybm&""
end if
if sydd<>"" then
sql = sql &" and 字段名2="&sydd&""
end if
if jdyf<>"" then
sql = sql &" and 字段名3="&jdyf&""
end if
if syr<>"" then
sql = sql &" and 字段名3="&syr&""
end if
或者更多.....................
最后执行 rs.open sql,conn,1,1
字符的话加上单引号,模糊查询的话换成 like 反正条件随便改。
sql 有isnull 函数, isnull(expression, replacement)