我用delphi自带的桌面数据工具(database desktop)建立了一个表,表中有一个日期时间型字段
tj_query.SQL.Clear ;
tj_query.Close;
sqlstr:='select sum(cj) as cjtj from cj where gh='''+copy(trim(ywh_cbox.text),0,3)+'''';
sqlstr:=sqlstr+' and rq>='''+datetostr(begin_date.date)+''''+' and rq<='''+datetostr(end_date.date)+'''';
tj_query.SQL.add(sqlstr);
tj_query.Open;
为什么老是报错?类型不匹配
错误在日期时间的比较上
我该怎么办?
我不想用过滤
tj_query.SQL.Clear ;
tj_query.Close;
sqlstr:='select sum(cj) as cjtj from cj where gh='''+copy(trim(ywh_cbox.text),0,3)+'''';
sqlstr:=sqlstr+' and rq>='''+datetostr(begin_date.date)+''''+' and rq<='''+datetostr(end_date.date)+'''';
tj_query.SQL.add(sqlstr);
tj_query.Open;
为什么老是报错?类型不匹配
错误在日期时间的比较上
我该怎么办?
我不想用过滤
tj_query.Close;
sqlstr:='select sum(cj) as cjtj from cj where gh='''+copy(trim(ywh_cbox.text),0,3)+'''';
sqlstr:=sqlstr+' and rq>='''+datetostr(begin_date.date)+''''+' and rq<='''+datetostr(end_date.date)+'''';
tj_query.SQL.add(sqlstr);
tj_query.Open;改一改看:tj_query.SQL.Clear ;
tj_query.Close;
sqlstr:='select sum(cj) as cjtj from cj where gh='''+copy(trim(ywh_cbox.text),0,3)+'''';
sqlstr:=sqlstr+' and rq>=:P_BeginDate and rq<=:P_EndDate';
tj_query.SQL.add(sqlstr);
tj_Query.Parameters.ParamByName('P_BeginDate').value := datetostr(begin_date.date);
tj_Query.Parameters.ParamByName('P_EndDate').value := datetostr(end_date.date)
tj_query.Open;
正确写法为
tj_query.SQL.Clear ;
tj_query.Close;
sqlstr:='select sum(cj) as cjtj from cj where gh='''+copy(trim(ywh_cbox.text),0,3)+'''';
sqlstr:=sqlstr+' and rq>=#'+datetostr(begin_date.date)+'#'+' and rq<=#'+datetostr(end_date.date)+'#';
tj_query.SQL.add(sqlstr);
tj_query.Open;
begin
SQL.Clear ;
Close;
sqlstr:='select sum(cj) as cjtj from cj where gh='''+copy(trim(ywh_cbox.text),0,3)+'''';
sqlstr:=sqlstr+' and rq>=:a and rq<=:b';
SQL.add(sqlstr);
Parameters.ParamByName('a').asdatetime :=FormatDatetime('yyyy-mm-dd',begin_date.date);
Parameters.ParamByName('b').asdatetime :=FormatDatetime('yyyy-mm-dd',end_date.date);
Open;
end;
说的是Access库中的用法。
建议用参数
总结方法:
跟踪一下
吧SQl语句付给一个字符串 然后到查询分析器去验证一下,看看什么错误!