begin
  s:='select * from table where 1=1 ';
  if edit1.text<>'' then
   s:=s+' and A1='''+edit1.text+''''
  else 
   ……

解决方案 »

  1.   


    A到1AX都是字符型的吗
    把你生成的SQL语句放到SQL Explore里执行一下,看是什么错误
      

  2.   

    照zx_wang(wzx)所说的做就对了。
      

  3.   

    照zx_wang(wzx)所说的思路做:但要根据查询条件的字段类型判断加引号的个数、是否要重新格式化字串(例如时间)
      

  4.   

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!我的意思是,判断edit的内容,添加where子句。
    如果,所有的edit都为空,那么,最后的的是
    'select * from table' zx_wang(wzx) 所写的:
    s:='select * from table where 1=1 ';
     这个的话就必须要填写一个edit了,就是多了一个
    ’where 1=1 ‘出来了。并非是我要的。!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      

  5.   

    注意你生成的S字符串中SQL语句的正确性,该有空格的就要加上
    空格,该有括号的要有括号,你可能单步运行一下:
    begin
      s:='select * from table ';
      if edit1.text<>'' then
       s:=s+' where A1=('+edit1.text+') and ';
      else if edit2.text<>'' then
       s:=s+' where A1=('+edit1.text+') and ';
    ......
      

  6.   

    你用数组
    这是我提出的问题,尽管美得到解决可是思路是对的。
    http://www.csdn.net/expert/topic/648/648059.xml?temp=.1813928给我加点分,我穷得可怜。
      

  7.   

    理论正确,只是差了一个加不加where 的问题,那就在s:='select * from table'之后加一个判断是否所有Edit是否都为空的语句:
    if (Edit1.GetTextLen<>0) and (Edit2.GetTextLen<>0) and …… then
       s:= s+' where ';
    ……
    ……//追加文本框的内空
      

  8.   


    if (Edit1.GetTextLen<>0) and (Edit2.GetTextLen<>0) and …… then
       s:= s+' where ';
    ……and 应该改成or吧
      

  9.   

    var
      s,ConStr:String;
    begin
      s:='select * from table ';
      ConStr:='';
      if (Edit1.text<>'') then
        ConStr:=ConStr+'and A1='''+Edit1.Text+''' ';
      if (Edit2.Text<>'') then
        ConStr:=ConStr+'and A2='''+Edit2.Text+''' ';
      if (Edit3.Text<>'') then
        ConStr:=ConStr+'and A3='''+Edit3.Text +''' ';
      if  ConStr<>'' then
      begin
        ConStr:=Copy(Constr,4,Length(ConStr)-3);//把第一个and 写成where
        ConStr:='Where'+ ConStr;
        S:=S+ConStr;
      end;
      showmessage(s);//显示出来验证一下
    end;
      

  10.   

    你可以在一个窗体上放三个TEdit试一下。
      

  11.   

    To:1999() 
    其实像zx_wang(wzx) 那样作是非常高明的,你不一定非要在所的Edit为空的时候就不要Where。
    上面我写的程序中把所有的“If Edit.Text<>'' then”换成“If Trim(Edit1.Text)<>''”这样要好些
      

  12.   

    zx_wang(wzx) 同志是不是我的同事,为何和我的方法一模一样
      

  13.   

    to 1999() (  ) 信誉:100  2002-04-15 17:38:00  得分:0  
     
     
      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!我的意思是,判断edit的内容,添加where子句。
    如果,所有的edit都为空,那么,最后的的是
    'select * from table' zx_wang(wzx) 所写的:
    s:='select * from table where 1=1 ';
     这个的话就必须要填写一个edit了,就是多了一个
    ’where 1=1 ‘出来了。并非是我要的。!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
     
    加上一个 'where 1=1' 是个巧方法,不影响结果!!!!!!!!!!!!!!!!!! 还省去好多判断和字符串操作!!!!!!!!!!!!!
      

  14.   

    苯,人家zx_wang(wzx) 给你金子你都不要,什么"这个的话就必须要填写一个Edit了"!!!好好看看吧
      

  15.   

    sorry,刚刚找到资料,发现确实是对的。:)
      

  16.   

    定义一个数组i
    array[i]=[1..10];
    if editi<>'' then
     select * from tablename
        where a1='''+editi.text+''';