with query1 do
begin
 sql.add('select sum(amount) from table1');
 sql.add('where substring(workdate,1,6)=:begindate');
 parambyname('begindate').asstring:=edit1.text;
end;由于我对数据表的自段进行了计算,即substring(workdate,1,6)所以无法利用到索引,导致查询速度很慢,在查询分析器里我改成:select sum(amount) from table1
where workdate like '200401%' 这样就利用到了索引,加快了查询速度,请问在DELPHI里我怎么实现这样的转换,怎么把等号左边的计算等价到等号右边对参数的计算上去,谢谢

解决方案 »

  1.   

    sql.add('select sum(amount) from table1');
    sql.add('where workdate like '''+Edit1.Text+'%''');全部是单引号
      

  2.   

    对dasaint的一点补充,觉得应该是:
    sql.add('select sum(amount) from table1');
    sql.add('where workdate like "'+Edit1.Text+'"%');
    没有上机具体操作 先试试看咯 没有关系的!
      

  3.   

    我试了不行,SQL GENERAL ERROR
    上面两位写的应该在查询分析器里就是 select sum(amount) from table
    where workdate like 'Edit1.text'%
    这样明显错误的吧
      

  4.   

    不可能不行,我用得好好的 win2k + Delphi6 + Oracle & Access当执行sql.add('where workdate like '''+Edit1.Text+'%''');时,Delphi会把Edit1.Text的内容取出来,生成sql语句。注意,最后一个单引号在%的后面。不能在查询分析器里写,它根本就不知道Edit1是什么。
      

  5.   

    var SQLText:String;
      ...
      SQLText:='select sum(amount) from table '+
               'where workdate like '''+Edit1.Text+'%''';
      SQL.add(SQLText);  Edit1.Text必须赋值。在查询分析器测试。
      

  6.   

    能解释一下'where workdate like '''+Edit1.Text+'%'''里面各个单引号的含义吗,有点不理解,谢谢
      

  7.   

    1. 在Delphi中,一对单引号之间的是字符串,如 'hello world'
    2. 可是如果在字符串中有单引号出现怎么办?方法是在一对单引号括起来的字符串中连续的两个单引号被解释为一个单引号。如:'There''s a tree.' 中间的字符串就是 There's a tree.
    3. 因此 'where workdate like ''' 表示 where workdate like '
       Edit1.Text 表示Edit1中的内容,比如是 200401
       '%''' 表示 %'
    4. 这样,三项加在一起就成了 where workdate like '200401%'