判断接收的参数是否为null

ambulance120 2012-03-25 11:22:05

接收四个参数,判断四个参数是否为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
...全文
177 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycqvb 2012-03-26
  • 打赏
  • 举报
回复
你这个是多条件组合查询吧。

不应该用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查询即可。
hookee 2012-03-26
  • 打赏
  • 举报
回复

sybm = Trim(request("sybm"))
sydd = Trim(request("sydd"))
jdyf = Trim(request("jdyf"))
syr = Trim(request("syr"))

If sybm = "" Then sybm = Null
If sydd = "" Then sydd = Null
If jdyf = "" Then jdyf = Null
If syr = "" Then syr = Null

...

kris2010 2012-03-26
  • 打赏
  • 举报
回复
sql 有isnull 函数, isnull(expression, replacement)
shugeer 2012-03-26
  • 打赏
  • 举报
回复
asp的空为"",sql自动生成的空为null
tyhjx 2012-03-26
  • 打赏
  • 举报
回复
把得到的SQL语句,输出,看看是否想要的结果,
Dogfish 2012-03-26
  • 打赏
  • 举报
回复
统一trim一下。比较是否为""

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧