Fórum sql_dinamica nao esta funcionando #245493

03/08/2004

0

O q está errado nesta query?
procedure TfrmConsulta.SpeedButton1Click(Sender: TObject);
var
tsql:string;
begin
if (length(trim(edIndice.Text)) = 0) and (length(trim(edDesc.Text)) = 0) then begin
showmessage(´Não foi selecioanda nenhuma opção para busca.´);
Status.Panels[0].Text := ´´;
SpeedButton2.Click;
end else
(*with Dm.Consulta do begin
tsql:=´select * from indice where´;
if (length(edIndice.Text) > 0) then //experimento o edIndice para saber se há algo
tsql:=tsql+´ posicao like ´+quotedstr(uppercase(edIndice.Text))+´¬´+´ and´;
if (length(edDesc.Text)) > 0 then //experimento o edDesc para saber se há algo
tsql:=tsql+´ descricao like ´+´¬´+quotedstr(uppercase(edDesc.Text))+´¬´+´ and´;
if Copy(tsql, length(tsql)-2,3) = ´and´ then
delete(tsql, length(tsql)-2,3);//removo os and não utilizados
if copy(tsql, length(tsql)-4,5) = ´where´ then //removo os wheres não utilizados
delete(tsql, length(tsql)-4,5);
Consulta.SQL.Text:=tsql;
Consulta.Active:=True; *)
end;
end;

Dá erro nos deletes: Too Many Parameters


Paulo

Paulo

Responder

Posts

03/08/2004

Macario

Ola programador, boa tarde.

Tente assim:

[color=darkred:79ff1e5681] if (length(trim(edIndice.Text)) = 0) and (length(trim(edDesc.Text)) = 0) then begin showmessage(´Não foi selecioanda nenhuma opção para busca.´); Status.Panels[0].Text := ´´; SpeedButton2.Click; end else begin with Dm.Consulta.SQL do begin Clear; Add(´select * from indice´); if (length(edIndice.Text) > 0) then begin Add(´where posicao like :Indice¬´; if (length(edDesc.Text)) > 0 then Add(´ and descricao like ¬:EdDesc¬´); end else if (length(edDesc.Text)) > 0 then Add(´where descricao like ¬:EdDesc¬´); Param.ParamByName(´indice´).asString := edindice.text; Param.ParamByName(´edDesc´).asString := edDesc.text; Open; end; end;[/color:79ff1e5681]


Sempre opte por usar parametros.
Qualquer coisa poste novamente


Responder

Gostei + 0

03/08/2004

Emerson Nascimento

pelo que eu percebi, o erro ocorria em função do lugar onde eram colocados os ¬. eles deveriam ser passados para a função QuotedStr().
veja se assim funciona:

var
    tsql:string;
begin
    if (length(trim(edIndice.Text)) = 0) and (length(trim(edDesc.Text)) = 0) then
    begin
        showmessage(´Não foi selecionada nenhuma opção para busca.´);
        Status.Panels[0].Text := ´´;
        SpeedButton2.Click;
    end
    else
    begin
        tsql:=´select * from indice where´;

        //experimento o edIndice para saber se há algo
        if (length(edIndice.Text) > 0) then
            tsql:=tsql+´ posicao like ´+quotedstr(uppercase(edIndice.Text)+´¬´)+´ and´;

        //experimento o edDesc para saber se há algo
        if (length(edDesc.Text)) > 0 then
            tsql:=tsql+´ descricao like ´+quotedstr(´¬´+uppercase(edDesc.Text)+´¬´)+´ and´;

        //removo o and não utilizado
        if Copy(tsql, length(tsql)-2,3) = ´and´ then
            delete(tsql, length(tsql)-2,3);

        //removo o where caso não seja utilizado
        if copy(tsql, length(tsql)-4,5) = ´where´ then
            delete(tsql, length(tsql)-4,5);

        Consulta.close;
        Consulta.SQL.Text:=tsql;
        Consulta.open;
    end;
end;


Responder

Gostei + 0

04/08/2004

Paulo

emerson, tentei como vc disse, mas não funcionou. Eu já fiz query dinamica e funcionou, só que os fontes não estão comigo. Tentei remover o where da variável tsql, mas mesmo assim não funcionou. Vou futucar mais um pouco e ver o q acontece. Eu preciso da query dinâmica, pq a consulta pode ter todos os edits preenchidos em alguns casos e em outros não. Por parâmetro, sou obrigado a informar todos os parâmetros. Eu dei um exemplo somente com dois edits, mas no total serão 12 edits, por isso a query dinâmica. Nem todos são obrigados a vir cheios.


Responder

Gostei + 0

04/08/2004

Macario

Seria mais facil vc tratar os parametros com if´s do que todo aquele codigo pra remover as clausulas where desnecessarias.


Responder

Gostei + 0

04/08/2004

Emerson Nascimento

eu testei assim e funcionou:

var
    tsql:string;
begin
    if (length(trim(edIndice.Text)) = 0) and (length(trim(edDesc.Text)) = 0) then
    begin
        showmessage(´Não foi selecionada nenhuma opção para busca.´);
        Status.Panels[0].Text := ´´;
        SpeedButton2.Click;
    end
    else
    begin
        tsql:=´´;

        //experimento o edIndice para saber se há algo
        if (length(edIndice.Text) > 0) then
            tsql:=tsql+´ and (posicao like ´+quotedstr(uppercase(edIndice.Text)+´¬´)+´)´;

        //experimento o edDesc para saber se há algo
        if (length(edDesc.Text)) > 0 then
            tsql:=tsql+´ and (descricao like ´+quotedstr(´¬´+uppercase(edDesc.Text)+´¬´)+´)´;

        //removo o and não utilizado
        delete(tsql, 1,4);

        if tsql <> EmptyStr then
            tsql:= ´select * from indice where´+tsql
        else
            tsql:= ´select * from indice´;

        Consulta.close;
        Consulta.SQL.Text:=tsql;
        Consulta.open;
    end;
end;


Responder

Gostei + 0

04/08/2004

Macario

O que voces acharam de errado na dica que eu passei?????

if (length(trim(edIndice.Text)) = 0) and (length(trim(edDesc.Text)) = 0) then begin showmessage(´Não foi selecioanda nenhuma opção para busca.´); Status.Panels[0].Text := ´´; SpeedButton2.Click; end else begin with Dm.Consulta.SQL do begin Clear; Add(´select * from indice´); if (length(edIndice.Text) > 0) then begin Add(´where posicao like :Indice¬´; if (length(edDesc.Text)) > 0 then begin Add(´ and descricao like ¬:EdDesc¬´); Param.ParamByName(´edDesc´).asString := edDesc.text; end; end else if (length(edDesc.Text)) > 0 then begin Add(´where descricao like ¬:EdDesc¬´); Param.ParamByName(´indice´).asString := edindice.text; end; Open; end; end; end;



Responder

Gostei + 0

04/08/2004

Emerson Nascimento

nada de errado, mas é que há 12 edits e fazer a verificação da forma como você indicou talvez seja um pouco complicado.


Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar