下面的代码意思实现动态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用参数不行的
数据库用的是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.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;
三楼的:在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')'中”
怎么办啊,还有高手吗?
要是sql.add('')到是好写,用DELPHI的函数或是用ACCESS的函数都可以。
sqlwhere:=sqlwhere+' and decl_date >= '+ 'date('+''''+datetostr(BeginDateTimeP.Date)+''''+') '+
关键是这一句该怎么写啊?
要是sql.add('')到是好写,用DELPHI的函数或是用ACCESS的函数都可以。
sqlwhere:=sqlwhere+' and decl_date >= '+ 'date('+''''+datetostr(BeginDateTimeP.Date)+''''+') '+---------------------------------该如下写
sqlwhere:=sqlwhere+' and decl_date >= '+ '#'+datetostr(BeginDateTimeP.Date)+'#') '+
也许还要用到 TimeValue