50分求教:空字符查询问题~~

anyyan 2003-02-26 10:10:53
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值?


...全文
22 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
anh 2003-02-28
  • 打赏
  • 举报
回复
你的这个问题是空和NULL 的问题。 在数据库中''和NULL 是不同的。
anyyan 2003-02-28
  • 打赏
  • 举报
回复
谢谢楼上诸位~!
我希望的就是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=''%%'')))');
beata88 2003-02-26
  • 打赏
  • 举报
回复
这还不容易呀!
你的sql.text分开来写嘛
User输入了多少个Edit,你sql.text中就有多少个参数。
如:

if trim(Edit1.text)<>'' then
sql.text:=
if Trim(edit2.text)<>'' then
…………
ma007 2003-02-26
  • 打赏
  • 举报
回复
mark
qiume 2003-02-26
  • 打赏
  • 举报
回复
I am learning
307 2003-02-26
  • 打赏
  • 举报
回复
还有别的办法,要下班了,明天给你解决!
有问题请发信息到我的E-mail:guangbowang@163.com
agui 2003-02-26
  • 打赏
  • 举报
回复
同意楼上,最简单了...
niat97222 2003-02-26
  • 打赏
  • 举报
回复
不过这样不太好,就是不管怎么样都把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=''%%''))';

这样应该完全可以达到要求了
niat97222 2003-02-26
  • 打赏
  • 举报
回复
同样支持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)+'%';
hjb8000 2003-02-26
  • 打赏
  • 举报
回复
gz
哈密尔 2003-02-26
  • 打赏
  • 举报
回复
支持weitao999(涛涛)
weitao999 2003-02-26
  • 打赏
  • 举报
回复
用三个 or 字段 is null 连接
anyyan 2003-02-26
  • 打赏
  • 举报
回复
NOBODY HELPS ME??
UP......

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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