sql.add('select * from record where location like :temp1 and build like :temp2 and room like :temp3');
      parambyname('temp1').asstring:='%'+trim(locaEdit.Text)+'%';
      parambyname('temp2').asstring:='%'+trim(buildEdit.Text)+'%';
      parambyname('temp3').asstring:='%'+trim(roomEdit.Text)+'%';如果3个Edit均不输内容,期望查询到的是全库内容,但结果是只有location/build/room三个字段都有值的才能查询到。若3者之中有一个为空(NULL)则不行。问题:DELPHI中的'%'+trim(roomEdit.Text)+'%',当edit.text=''时不能获取SQL SERVER中的NULL值?

解决方案 »

  1.   

    NOBODY HELPS ME??
    UP......
      

  2.   

    用三个 or 字段 is null 连接
      

  3.   

    同样支持weitao999(涛涛)
    写详细一点
    sql.add('select * from record where ((location like :temp1) or (location is null)) and ((build like :temp2) or (build is null)) and ((room like :temp3')or (room is null));
          parambyname('temp1').asstring:='%'+trim(locaEdit.Text)+'%';
          parambyname('temp2').asstring:='%'+trim(buildEdit.Text)+'%';
          parambyname('temp3').asstring:='%'+trim(roomEdit.Text)+'%';
      

  4.   

    不过这样不太好,就是不管怎么样都把NULL值查出来了
    再换个方式把
    sql.add('select * from record where ((location like :temp1) or ((location is null) and (:temp1=''%%''))) and ((build like :temp2) or ((build is null) and (:temp2=''%%'')) and ((room like :temp3)or ((room is null) and (:temp3=''%%''))';这样应该完全可以达到要求了
      

  5.   

    还有别的办法,要下班了,明天给你解决!
    有问题请发信息到我的E-mail:[email protected]
      

  6.   

    这还不容易呀!
    你的sql.text分开来写嘛
    User输入了多少个Edit,你sql.text中就有多少个参数。
    如:if trim(Edit1.text)<>'' then
      sql.text:=
    if Trim(edit2.text)<>'' then
      …………
      

  7.   

    谢谢楼上诸位~!
    我希望的就是niat97222(Freeman)说的第二种情况。
    但你的语句好象括号上有问题吧?调试不过~~~
    我添成以下这样,语法上没问题了,但运行查询后抱错:invalid token:))
    不知错在哪里?
    谢谢各位~! sql.add('select * from record where ((location like :temp1) or ((location IS NULL) and (:temp1=''%%''))) and ((build like :temp2) or ((build IS NULL) and (:temp2=''%%''))) and ((room like :temp3) or ((room IS NULL) and (:temp3=''%%'')))');
      

  8.   

    你的这个问题是空和NULL 的问题。 在数据库中''和NULL 是不同的。