var
str:array [0..7] of string;
sSql:string;
i:integer;
begin
for i:=0 to 7 do
str[i]:='';
sSql:='select * from table where 1=1 '
if edit1.text <>'' then
str[0]:= ' and field1 link '''+edit1.text +'%''';
if edit2.text<>'' then
str[1]:=' and filed2 ='''+edit2.text+'''';
if cbTime.check then
str[2]:=' and filed3 between '''+ datetostr(tpk1.date)+'' and ' +datetostr(tpk2.date); ..... for i:=0 to 7 do
ssql:=sql+str[i];end;
有什么搞不掂再讨论
str:array [0..7] of string;
sSql:string;
i:integer;
begin
for i:=0 to 7 do
str[i]:='';
sSql:='select * from table where 1=1 '
if edit1.text <>'' then
str[0]:= ' and field1 link '''+edit1.text +'%''';
if edit2.text<>'' then
str[1]:=' and filed2 ='''+edit2.text+'''';
if cbTime.check then
str[2]:=' and filed3 between '''+ datetostr(tpk1.date)+'' and ' +datetostr(tpk2.date); ..... for i:=0 to 7 do
ssql:=sql+str[i];end;
有什么搞不掂再讨论
yingwei(樱薇)
cobi(小新国际) 三位说得太粗略,不知能否再详尽一些,注意看一下Tquery组件的sql builder里类似功能,那是非常灵活的,希望有哪位高手能写出通用代码让大家共享,敝下在此先行谢过!!!
如果都是and 'select * from .. where 1=1'
如果都是or 'select * from .. where 0=1'
连接子句时比较方便
sSql:='select * from table '
if edit1.text <>'' then
if cb1.check then
str[0]:= ' and field1 link '''+edit1.text +'%''';
else
str[0]:= ' or field1 link '''+edit1.text +'%''';
if edit2.text<>'' then
if cb2.check then
str[1]:=' and filed2 ='''+edit2.text+'''';
else
str[1]:=' or filed2 ='''+edit2.text+'''';
if cbTime.check then
if cb3.check then
str[2]:=' and filed3 between '''+ datetostr(tpk1.date)+'' and ' +datetostr(tpk2.date);
else
str[2]:=' or filed3 between '''+ datetostr(tpk1.date)+'' and ' +datetostr(tpk2.date);
...
// 找到第一句非空子条件
for i:=0 to 7 do
if str[i]<>'' then
begin
iFirst:=i;
//str 保证and 和or 前只有一个空个;or 至少有两个空格
str[ifrist]:=copy(str[ifirst],5,length(str[ifirst]);
str[ifrist]:=' '+str[ifrist];
sSql:=sSql+' where '+str[ifirst]; break;
end; for i:=ifrist+1 to 7 do
sSql:=ssql+str[i]