撰写查询尽量不要把程序写死,多用动态方式。以下是我程序片段:
procedure TClass1.ChangeSQL( const SearchValue: WideString);
var
AValueStringList,ByStringList:TStringList;
i,j,k:integer;
begin
AValueStringList:=TStringList.Create;
ByStringList:=TStringList.Create;
AValueStringList.Text:=SearchValue;
dtRP1501.Close;
//Reset SQL
for i:=dtRP1501.SQL.Count-1 Downto 0 do
if (pos('ORDER BY',UpperCase(dtRP1501.SQL))>0) or (pos('GROUP BY',UpperCase(dtRP1501.SQL))>0) then
begin
ByStringList.add(dtRP1501.SQL);
dtRP1501.SQL.Delete(i);
end;
for j:=dtRP1501.SQL.Count-1 Downto 0 do
if (Pos(' ',dtRP1501.SQL[j])<>0) and (Pos(' as ',dtRP1501.SQL[j])=0) then dtRP1501.SQL.Delete(j);
if (AValueStringList[0]<>'') and (AValueStringList[0]<>'*') then
//dtView4.SQL.Add(' AND QCMTDT10.RemeltDATE='+sqldate1(AValueStringList[0],2));
dtRP1501.SQL.Add(' AND QCMTDT10.RemeltDATE='+''''+AValueStringList[0]+'''');
if ByStringList.count > 0 then
for k:= ByStringList.count-1 downto 0 do
dtRP1501.SQL.Add(ByStringList[k]);
dtRP1501.Open;
end;
在从Client端呼叫此函数。