select de pesquisa com vários edit preenchido
Olá, sou iniciante em delphi. Tenho um projeto que no campo pesquisa possui 3 edit, um 'nome', outro 'cod_pront_emergencia' e outro 'cod_prontuario_graduacao'. O usuário tem a opção de escolher se preenche os 3 campos ou apenas um. Gostaria de saber como faço a sql para trazer a busca, caso o usuário preencha ou não todos os edti de pesquisa.
Deise Souza
Curtidas 0
Respostas
Jones Granatyr
04/05/2017
Opa! Uma maneira é você fazer um SQL dinâmico de acordo com o que o usuário selecionar. Por exemplo:
if edtNome <> '' then
ASql := ' and nome = ' + edtNome
if edtNome <> '' then
ASql := ' and nome = ' + edtNome
GOSTEI 0
Deise Souza
04/05/2017
Eu tenho 4 edit para pesquisa na qual o usuáio pode escolher em qual irá pesquisar: editPaciente, editgrad, editpos, editemerg. O edit paciente busca pelo nome do paciente e os outros edits busca pelo numero do prontuario.
No sql da query digitei esse codigo:
select * from pacientes
where UPPER (nome_paciente) like UPPER (:NomePaciente)
and pront_grad like :ProntGrad
and pront_pos like :ProntPos
and pront_emerg like :ProntEmerg
Mas na hora da busca ele não traz nada. No codigo do botão pesquisar fiz o seguinte código: // Edit Nome
if EditPaciente.Text <> ' ' then
begin
QueryPacientes.ParamByName('NomePaciente').AsString
:= '%' + EditPaciente.Text + '%';
end
else
begin
QueryPacientes.ParamByName('NomePaciente').AsString := '%%';
end;
//Edit Pront Grad
if EditGrad.Text <> ' ' then
begin
QueryPacientes.ParamByName('ProntGrad').AsString := EditGrad.Text;
end
else
begin
QueryPacientes.ParamByName('ProntGrad').AsString := '%%';
end;
// Edit Pront Pos
if EditPos.Text <> ' ' then
begin
QueryPacientes.ParamByName('ProntPos').AsString := EditPos.Text;
end
else
begin
QueryPacientes.ParamByName('ProntPos').AsString := '%%';
end;
// Edit Pront Emerg
if EditEmerg.Text <> ' ' then
begin
QueryPacientes.ParamByName('ProntEmerg').AsString := EditEmerg.Text;
end
else
begin
QueryPacientes.ParamByName('ProntEmerg').AsString := '%%';
end;
Vc consegue me ajudar no que esta faltando?
No sql da query digitei esse codigo:
select * from pacientes
where UPPER (nome_paciente) like UPPER (:NomePaciente)
and pront_grad like :ProntGrad
and pront_pos like :ProntPos
and pront_emerg like :ProntEmerg
Mas na hora da busca ele não traz nada. No codigo do botão pesquisar fiz o seguinte código: // Edit Nome
if EditPaciente.Text <> ' ' then
begin
QueryPacientes.ParamByName('NomePaciente').AsString
:= '%' + EditPaciente.Text + '%';
end
else
begin
QueryPacientes.ParamByName('NomePaciente').AsString := '%%';
end;
//Edit Pront Grad
if EditGrad.Text <> ' ' then
begin
QueryPacientes.ParamByName('ProntGrad').AsString := EditGrad.Text;
end
else
begin
QueryPacientes.ParamByName('ProntGrad').AsString := '%%';
end;
// Edit Pront Pos
if EditPos.Text <> ' ' then
begin
QueryPacientes.ParamByName('ProntPos').AsString := EditPos.Text;
end
else
begin
QueryPacientes.ParamByName('ProntPos').AsString := '%%';
end;
// Edit Pront Emerg
if EditEmerg.Text <> ' ' then
begin
QueryPacientes.ParamByName('ProntEmerg').AsString := EditEmerg.Text;
end
else
begin
QueryPacientes.ParamByName('ProntEmerg').AsString := '%%';
end;
Vc consegue me ajudar no que esta faltando?
GOSTEI 0