with ADOQuery1 do begin close; sql.Clear; SQL.Add('select * from table where 1=1'); if trim(edit1.text) <> '' then //*********读取字符串进数组************* theStr := trim(edit1.Text); nStartPos := 1; nEndPos := Pos(',', theStr); i := 1; while nEndPos > 0 do begin a[i] := MidStr(theStr, nStartPos, nEndPos - nStartPos); theStr[nEndPos] := ' '; nStartPos := nEndPos; nEndPos := Pos(',', theStr); inc(i); end; //**************读取结束************* if i > 1 then begin sql.add(' and f1 in'); for j := 1 to i - 1 do begin sqlstr := sqlstr + '''' + a[j] + ''','; end; sqlstr := '(' + leftstr(sqlstr, length(sqlstr) - 1) + ')'; end; SQL.add(sqlstr); end;
edit1.text的内容也是动态生成的 如a,b,c,d,……
adoquery1.Close; adoquery1.SQL.clear; adoquery1.sql.Add('select * from AST_BillType where vchr_BillTypeID in ('+'''a'''+','+'''b'''+','+'''c'''+')'); adoquery1.Active :=true ;
select * from table where f1 in ('a','b','c') 是动态生成最后执行前的语句,我用showmessage(sql.text)查看过,动态和静态完全相同,但执行结束就是只能执行f1=a(a是in 后面的第一项)
begin
close;
sql.Clear;
SQL.Add('select * from table where 1=1'); if trim(edit1.text) <> '' then
//*********读取字符串进数组*************
theStr := trim(edit1.Text);
nStartPos := 1;
nEndPos := Pos(',', theStr);
i := 1;
while nEndPos > 0 do
begin
a[i] := MidStr(theStr, nStartPos, nEndPos - nStartPos);
theStr[nEndPos] := ' ';
nStartPos := nEndPos;
nEndPos := Pos(',', theStr);
inc(i);
end;
//**************读取结束************* if i > 1 then
begin
sql.add(' and f1 in');
for j := 1 to i - 1 do
begin
sqlstr := sqlstr + '''' + a[j] + ''',';
end;
sqlstr := '(' + leftstr(sqlstr, length(sqlstr) - 1) + ')';
end;
SQL.add(sqlstr);
end;
如a,b,c,d,……
adoquery1.SQL.clear;
adoquery1.sql.Add('select * from AST_BillType where vchr_BillTypeID in ('+'''a'''+','+'''b'''+','+'''c'''+')');
adoquery1.Active :=true ;
是动态生成最后执行前的语句,我用showmessage(sql.text)查看过,动态和静态完全相同,但执行结束就是只能执行f1=a(a是in 后面的第一项)
theStr := trim(edit1.Text);
nStartPos := 1;
nEndPos := Pos(',', theStr);
i:=1;
while nEndPos > 0 do
begin
a[i] := copy(theStr, 1, nEndPos - 1);
theStr:=copy(theStr,nEndPos+1,length(theStr)-nEndPos);
nEndPos := Pos(',', theStr);
inc(i);
end;
a[i]:=theStr;