51代码网ORACLEMYSQLSQL SERVER其它数据库java/jspasp/asp.netC/C++/VC++APP应用其它语言服务器应用
您现在的位置: 51代码网 >> asp.net >> 文章正文

如何判断接收的参数是否为null

更新时间:2012-3-26:  来源:51代码网

接收四个参数,判断四个参数是否为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)

  • 上一篇文章:
  • 下一篇文章: 没有了
  • 赞助商链接
    推荐文章
  • 此栏目下没有推荐文章
  • {
    设为首页 | 加入收藏 | 友情链接 | 网站地图 | 联系站长 |