不要这样写sql,
如果你的查询条件里面有“ ’”,可能会报错
如果很多地方要用,自己写个过程吧
这个我经常用的!
procedure AddWhere(var strWhere:string;const KeyField,KeyValue:string;IsString:Boolean=True;Oper:string=' = ');
var
strExpress,strKeyVal,strOper:string;
p:Integer;
begin
if KeyValue='' then
Exit;
if IsString then
begin
p:=Pos('*',KeyValue);
if p<>0 then
begin
strKeyVal:=KeyValue;
strKeyVal[p]:='%';
strOper:=' like ';
end
else
begin
strKeyVal:=KeyValue;
strOper:=Oper;
end;
strKeyVal:=''''+StringReplace(strKeyVal,'''','''''',[rfReplaceAll])+'''';
end
else
begin
strKeyVal:=KeyValue;
strOper:=Oper;
end;
strExpress:=KeyField+strOper+strKeyVal;
if strWhere='' then
strWhere:=strExpress
else
strWhere:=strWhere+' and '+strExpress;
end;
如果你的查询条件里面有“ ’”,可能会报错
如果很多地方要用,自己写个过程吧
这个我经常用的!
procedure AddWhere(var strWhere:string;const KeyField,KeyValue:string;IsString:Boolean=True;Oper:string=' = ');
var
strExpress,strKeyVal,strOper:string;
p:Integer;
begin
if KeyValue='' then
Exit;
if IsString then
begin
p:=Pos('*',KeyValue);
if p<>0 then
begin
strKeyVal:=KeyValue;
strKeyVal[p]:='%';
strOper:=' like ';
end
else
begin
strKeyVal:=KeyValue;
strOper:=Oper;
end;
strKeyVal:=''''+StringReplace(strKeyVal,'''','''''',[rfReplaceAll])+'''';
end
else
begin
strKeyVal:=KeyValue;
strOper:=Oper;
end;
strExpress:=KeyField+strOper+strKeyVal;
if strWhere='' then
strWhere:=strExpress
else
strWhere:=strWhere+' and '+strExpress;
end;
NameStr:String;∥设置字符串中间变量
Begin
NameStr:=′王′;
Query1SQLClear
Query1SQLAdd(′Select*FromCustomdbwhere(Name1>=N1)and(Name2<=N2)′);
Query1params[0]AsString:=NameStr+chr(0);
Query1params[1]AsString:=NameStr+chr($ff);
Query1Open;
End;
好象这样也能搞定,不过你要查询的字段必须为字符型的,而且要是
关键字段(但如果用access2000好象能对任何字符字段模糊查询!)
query1.Close;
query1.sql.Clear;
query1.sql.Add('select * from "e:\01\tsk.db" as tsk where 1=1 ')
if Trim(edit1.text) <> '' then
begin
Query1.SQL.Add(' or (tsk.ssh=:var) ');
ParamByName(var).AsString := Edit1.Text;
end;
if Trim(Edit2.Text) <> '' then
Query1.SQL.Add(' or (tsk.sm like '''+'%'+Edit2.text+'%'') ');
if Trim(Edit3.Text) <> '' then
Query1.SQL.Add(' or (tsk.gjz like '''+'%'+edit3.text+'%'') ');
query1.prepare;
query1.Open;