select com mais valores
21/01/2010
0
Clei Martins
Posts
22/01/2010
Emerson Nascimento
var
sInstrucao, sCondicao: string;
begin
sInstrucao := 'select * from t_modelo';
sCondicao := '';
if trim(vmarca1) <> '' then
sCondicao := sCondicao + QuotedStr(vmarca1);
if trim(vmarca2) <> '' then
begin
if sCondicao <> '' then sCondicao := sCondicao + ', ';
sCondicao := sCondicao + QuotedStr(vmarca2);
end;
if trim(vmarca3) <> '' then
begin
if sCondicao <> '' then sCondicao := sCondicao + ', ';
sCondicao := sCondicao + QuotedStr(vmarca3);
end;
if sCondicao <> '' then
sInstrucao := sInstrucao + ' where marca in (' + sCondicao + ')';
[abra o dataset utilizando a instrucao contida na variável sInstrucao]
end;
se nenhuma variável tiver valor, tratá todo o conteúdo da tabela t_modelo, caso contrário trará somente os registros da tabela t_modelo que atendam aos valores da lista criada.
05/03/2010
Thiago Santana
05/03/2010
Wilson Junior
function SQL_Monta_OR(NomeCampo: string; stlDados: TStringList;
EhCampoString: Boolean = False): string;
var
x: integer;
strCampo: string;
begin
Result := '';
if Trim(NomeCampo) <> '' then
begin
for x := 0 to stlDados.Count - 1 do
begin
if Result <> '' then
Result := Result + ', '
;
strCampo := stlDados[x];
if EhCampoString then
strCampo := QuotedStr( strCampo )
;
Result := Result + strCampo;
end;
if Result <> '' then
Result := NomeCampo + ' IN (' + Result + ')'
;
end
;
end;
function RetornaSQL: string
var
strWhere: string;
stlWhere: TStringList;
begin
stlWhere := TStringList.Create;
stlWhere.Add( vmarca1 );
stlWhere.Add( vmarca2 );
stlWhere.Add( vmarca3 );
strWhere := SQL_Monta_OR( 'Marca', stlWhere, True );
Result := 'SELECT * FROM T_MODELO';
if strWhere <> '' then
Result := Result + ' WHERE ' + strWhere
;
end;
Espero ter colaborado.
Clique aqui para fazer login e interagir na Comunidade :)