Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   C++ Builder (https://www.clubdelphi.com/foros/forumdisplay.php?f=13)
-   -   Encontrar un parámetro (https://www.clubdelphi.com/foros/showthread.php?t=95039)

Angel.Matilla 29-01-2021 11:42:42

Encontrar un parámetro
 
Tengo un TIBQuery que se monta de forma dinámica en función de las opciones que elige el usuario y dependiendo de ellas puede o no llevar un determinado parámetro. Queda una cosa así:
Código:

fMEnu->Query->SQL->Text = "SELECT[...]";
fMenu->Query->SQL->Add("WHERE TRUNC(B.Estadistica / 100) = :Estadistica");
if (this->Tag != 1)
    fMenu->Query->SQL->Add("AND A.CodPrv = :PrvIns");

He estado mirando en la ayuda de Builder las funciones FindParam y GetParamList pero no acabo de entender como funcionan; ¿podríais indicarme como usar una de ellas (o ambas) para saber si ese parámetro PrvIns está presente en query?

movorack 29-01-2021 13:38:05

Hola,

FindParam busca el parámetro dentro de la consulta y devuelve el objeto. En caso de no existir, en Delphi devuelve un nil

Código Delphi [-]
  AQuery.SQL.Text := 'SELECT * FROM TABLA WHERE CAMPO1 = :CAMPO1';

  if CheckBox1.Checked then
    AQuery.SQL.Add('  AND CAMPO2 = :CAMPO2';

  AQuery.Params.ParamsByName('CAMPO1').Value := Valor1;

  if Assigned(AQuery.Params.FindParam('CAMPO2')) then
    AQuery.Params.ParamsByName('CAMPO2').Value := Valor2;

GetParamList, Permite hacer una búsqueda similar pero con mas de un parámetro. En este caso llena un TList.

Código Delphi [-]
var
  i: integer;
  ListaP: TList < TParam >;
begin
  AQuery.SQL.Text := 'SELECT * FROM TABLA WHERE CAMPO1 = :CAMPO1';

  if CheckBox1.Checked then
    AQuery.SQL.Add('  AND CAMPO2 = :CAMPO2';

  if CheckBox2.Checked then
    AQuery.SQL.Add('  AND CAMPO3 = :CAMPO3';

  if CheckBox3.Checked then
    AQuery.SQL.Add('  AND CAMPO4 = :CAMPO4';

  AQuery.Params.ParamsByName('CAMPO1').Value := Valor1;
  
  ListaP := TList < TParam >.Create;
  try
    AQuery.Params.GetParamList(ListaP, ['CAMPO2;CAMPO3;CAMPO4']);
    
    for i := 0 to ListaP.Count - 1 do
    begin
      //Aquí puedo interactuar con los parámetros presentes en la lista
    end;
  finally
    ListaP.free;
  end;    
end;

Angel.Matilla 29-01-2021 18:21:29

Gracias por la ayuda.


La franja horaria es GMT +2. Ahora son las 20:47:47.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi