Delphi - Passar código SQL para TFDQuery

28/05/2018

0

Bom dia,
consegui fazer um código para economizar digitação:

procedure SQL (SQL: String);

implementation

procedure TuFrmMain.SQl(SQL: String);
begin
DM.DataModule1.Query.Close;
DM.DataModule1.Query.SQL.Clear;

DM.DataModule1.Query.SQL.Add
(SQL);
DM.DataModule1.Query.Open();
end;

Mas eu queria passar também o nome do TFDQuery, pois em algumas eu tenho QuerySaida, QueryEntrada...
Aí queria chamar a procedure mais ou menos assim:
SQL('QueySaida','select distinct(CARREGAMENTO)as CARREGAMENTO from saida where posicao=''M''');
Tássio Gois

Tássio Gois

Responder

Post mais votado

28/05/2018

altere o procedimento de modo a receber o objeto como referência:
procedure TuFrmMain.SQL(var objQuery: TFDQuery, strSQL: String);
begin
   objQuery.Close;
   objQuery.SQL := strSQL;
   objQuery.Open();
end;


e, para utilizar, você passa o objeto como parâmetro:
SQL(DM.DataModule1.QuerySaida, 'select distinct(CARREGAMENTO) as CARREGAMENTO from saida where posicao=''M''');
SQL(DM.DataModule1.QueryEntrada, 'select distinct(CARREGAMENTO) as CARREGAMENTO from saida where posicao=''M''');

Emerson Nascimento

Emerson Nascimento
Responder

Mais Posts

28/05/2018

Raimundo Pereira

Bom dia.
Tássio, veja um exemplo:

Adicione em uses :
FireDAC.Stan.Intf, FireDAC.Stan.Option,
FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, Data.DB,
FireDAC.Comp.DataSet, FireDAC.Comp.Client, Vcl.StdCtrls;

type
Function RunQuery(MyQuery:TFDQuery; MySQL:String):Integer;

implementation

{$R *.dfm}

{ TForm1 }

function TForm1.RunQuery(MyQuery: TFDQuery; MySQL: String): Integer;
begin
    With MyQuery do begin
    close;
    sql.Clear;
    SQL.Text:=MySQL;
    try
    Open;
    FetchAll;
    Result:=RecordCount;
    except
    on E : Exception do
       ShowMessage(E.ClassName+'Mensagem de erro: '+E.Message);
    end;
        Result:=0;
    end;
end;


Chamando a função:

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(IntToStr(RunQuery(QueySaida,'select distinct(CARREGAMENTO)as CARREGAMENTO from saida where posicao=''M''')));
end;

Responder

28/05/2018

Tássio Gois

altere o procedimento de modo a receber o objeto como referência:
procedure TuFrmMain.SQL(var objQuery: TFDQuery, strSQL: String);
begin
   objQuery.Close;
   objQuery.SQL := strSQL;
   objQuery.Open();
end;


e, para utilizar, você passa o objeto como parâmetro:
SQL(DM.DataModule1.QuerySaida, 'select distinct(CARREGAMENTO) as CARREGAMENTO from saida where posicao=''M''');
SQL(DM.DataModule1.QueryEntrada, 'select distinct(CARREGAMENTO) as CARREGAMENTO from saida where posicao=''M''');




Boa tarde,
obrigado a vocês pela ajuda.

Com esse código dá erro:
[dcc32 Error] FrmMain.pas(180): E2010 Incompatible types: 'TStrings' and 'string'

Se fizer assim:
objQuery.SQL.ADD := StrSQL;

Dá o erro:
[dcc32 Error] FrmMain.pas(180): E2035 Not enough actual parameters
Responder

29/05/2018

Raimundo Pereira

Bom dia.
Repare que você está usando o mesmo select para ambas query

na tabela entrada
SQL(DM.DataModule1.QueryEntrada, 'select distinct(CARREGAMENTO) as CARREGAMENTO from saida where posicao=''M''');

na tabela saida
SQL(DM.DataModule1.QueryEntrada, 'select distinct(CARREGAMENTO) as CARREGAMENTO from saida where posicao=''M''');
Responder

29/05/2018

Emerson Nascimento

troque para
objQuery.SQL.Text := strSQL;
Responder

20/02/2019

Tássio Gois

Boa noite,
consegui desenrolar passando 1 parâmetro.

Gostaria de passar mais parâmetros e ter o resultado.

function TuFrmMain.ConsultaSQLcomParametros(var objQuery: TUniQuery; strSQL, Parametro: String; Valor: Variant; Field: String): String;
begin
if objQuery.Active = True then
objQuery.Close;

objQuery.SQL.Clear;
objQuery.SQL.Text := strSQL;
objQuery.ParamByName(Parametro).Value := Valor;
objQuery.Open;
Result := objQuery.FieldByName(Field).Value;

if objQuery.Active = True then
objQuery.Close;
end;

Estou chamado a função assim:
ConsultaSQLcomParametros(DataModule1.Query,'select razao from client where codcli=:pcliente','pcliente',11,'razao')

Gostaria de passar por exemplo 2, em quantos parâmetros eu quero e fazer a consulta.

Por exemplo
function TuFrmMain.ConsultaSQLcomParametros
(var objQuery: TUniQuery; strSQL; Field: String; QtdeParametros: Integer; Parametro: String; Valor: Variant;...): String;
ConsultaSQLcomParametros
(DataModule1.Query,'select razao from client where codcli=:pcliente and vendedor=:pvendedor', 2, 'pcliente',11,'razao','pvendedor','Fulano')
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

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

Aceitar