下面的代码意思实现动态SQL查询,划横线的地方表示要按时间进行查询,编译能通的过,可是在执行程序时提示“用于函数参数的个数不对在表达式item_code not in(select item_code from prod_master) and decl_date >=date('2004-09-14') and decl_date<=date('2004-09-16')'中”
数据库用的是ACCESS2000。
哪位大虾能帮帮我,告诉这是什么原因?我该怎么写那段代码。
procedure TProdQueryForm.BitBtn1Click(Sender: TObject);
var
  sqlstr,sqlwhere:string;
begin
  sqlstr:='select * from item_master  where item_code not in '+
          '(select item_code from prod_master) ';
  with DataM.PubADOQ do
  begin
    close;
    sql.Clear;
    if ItemNameCheckB.Checked then
      sqlwhere:=sqlwhere+' and name like ''%'+trim(NameEdit.Text)+'%'' ';
    if PlanCheckB.Checked then
      sqlwhere:=sqlwhere+' and task_type='''+trim(PlanComboB.Text)+''' ';
    if FieldCheckB.Checked then
      sqlwhere:=sqlwhere+' and item_field='''+trim(FieldComboB.Text)+''' ';
    if DateCheckB.Checked then
      sqlwhere:=sqlwhere+' and decl_date >= '+ 'date('+''''+datetostr(BeginDateTimeP.Date)+''''+') '+
            ' and decl_date<='+ 'date('+''''+datetostr(EndDateTimeP.Date)+''''+') ';
                  ------------------------------------------------------------------
    sql.Text:=sqlstr+sqlwhere;
    open;
  end;
end;注:以前都是在SQL里用参数的,像下面这样
sql.Add('and decl_date >= :BeginDate and decl_date <= :EndDate');
Parameters.ParamValues['BeginDate']:=strToDate(DateToStr(BeginDateTimeP.Date)+' 00:00');
Parameters.ParamValues['EndDate']:=StrToDate(DateToStr(EndDateTimeP.Date)+' 23:59');
因为这是动态SQL用参数不行的

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/3369/3369102.xml?temp=.1375543
    #只是查询时对时间常量的引用下面列举三种查询方法
    1.parameter不需要#
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      with adoquery1 do
      begin
        Close;
        sql.clear;
        sql.add('select * from ddd where end_date<=:e_date');
        Parameters.ParamByName('e_date').Value:=FormatDateTime('yyyy-mm-dd hh:mm:ss',now());
        open;
      end;
    end;
    2.formatedatetime需要#
    procedure TForm1.Button3Click(Sender: TObject);
    begin
      with adoquery1 do
      begin
        Close;
        sql.clear;
        sql.add('select * from ddd where end_date<= '+'#'+FormatDateTime('yyyy-mm-dd',now())+'#');
        open;
      end;
    end;
    3.直接参数#
    procedure TForm1.Button4Click(Sender: TObject);
    begin
      with adoquery1 do
      begin
        Close;
        sql.clear;
        sql.add('SELECT * FROM ddd where end_date <= #2004-9-14#');
        open;
      end;
    end;
      

  2.   

    编译能通过就说明代码没问题,把程序执行产生的SQL语句用sql.savetofile('test.txt')存下来到access里执行一下看看参数不就是用来动态修改的吗?
      

  3.   

    Access中日期时间要用#2004-09-16 15:08:20#,而且你最好也不要随便转换类型,如果数据库里用的是DateTime 类型的在SQL 语句中就不要在转换成String 类型的了。Access 没有SQL 那么智能会自动识别类型。
      

  4.   

    二楼没有仔细看问题,答非所问,你说的那些方法,我都用过,但在这里不能用的,现在是动态生成的SQL,
       三楼的:在ACCESS里执行还是提示“用于函数参数的个数不对在表达式item_code not in(select item_code from prod_master) and decl_date >=date('2004-09-14') and decl_date<=date('2004-09-16')'中”
      怎么办啊,还有高手吗?
      

  5.   

    关键是这一句该怎么写啊?
    要是sql.add('')到是好写,用DELPHI的函数或是用ACCESS的函数都可以。
    sqlwhere:=sqlwhere+' and decl_date >= '+ 'date('+''''+datetostr(BeginDateTimeP.Date)+''''+') '+
      

  6.   

    接楼上,decl_date当然是字段啊
      

  7.   

    回复人: chengtwn(漠孤烟) ( ) 信誉:99  2004-09-16 15:24:00  得分: 0  
     
     
       关键是这一句该怎么写啊?
    要是sql.add('')到是好写,用DELPHI的函数或是用ACCESS的函数都可以。
    sqlwhere:=sqlwhere+' and decl_date >= '+ 'date('+''''+datetostr(BeginDateTimeP.Date)+''''+') '+---------------------------------该如下写
    sqlwhere:=sqlwhere+' and decl_date >= '+ '#'+datetostr(BeginDateTimeP.Date)+'#') '+
      

  8.   

    用FormatDateTime('"The meeting is on " dddd " at " hh:mm:ss AM/PM', Now )格式处理
      

  9.   

    要用 DateValue('2004-9-9') 这样
    也许还要用到 TimeValue
      

  10.   

    sql语句中在时间两边加上#号就对了.前边有人说了