Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Conexión con bases de datos (https://www.clubdelphi.com/foros/forumdisplay.php?f=2)
-   -   Consultar si registro existe en campo de 2 tablas (https://www.clubdelphi.com/foros/showthread.php?t=83177)

jonydread 19-05-2013 22:27:49

Consultar si registro existe en campo de 2 tablas
 
como puedo unir campos para ver si existe campo en ambas tablas
por ejemplo en campo1 de tabla1 y campo1 de tabla2 si existe el numero 1
he probado de este modo pero no logro funcionamiento
Código Delphi [-]
  zquery1.SQL.Text := 'SELECT * FROM  AsignadosA inner JOIN AsignadosB on AsignadosA.Sierras=AsignadosB.Sierras where AsignadosA.Sierras = ' + QuotedStr(dblookupcombobox1.KeyValue);
  zquery1.Open;

saludos!

ecfisa 19-05-2013 22:55:08

Cita:

Empezado por jonydread (Mensaje 460736)
como puedo unir campos para ver si existe campo en ambas tablas
por ejemplo en campo1 de tabla1 y campo1 de tabla2 si existe el numero 1

Hola jonydread.

Si queres verificar si existen ocurrencias en ambas tablas podes hacer:
Código Delphi [-]
function Tu_Form.ExisteEnAmbas(aValue: Variant): Boolean;
begin
  with ZQuery1 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('SELECT COUNT(*) AS EXISTE');
    SQL.Add('FROM ASIGNADOSA T1, ASIGNADOSB T2');
    SQL.Add('WHERE T1.SIERRAS = :PVALUE AND T2.SIERRAS = :PVALUE');
    ParamByName('PVALUE').Value := aValue;
    Open;
    Result := FieldByName('EXISTE').AsInteger > 0;
    Close
  end
end;

(* Ejemplo de llamada *)
...
begin
    if ExisteEnAmbas(Valor_Condicion) then
    ShowMessage('Existe')
  else
    ShowMessage('No existe');
...

Si deseas mostrar todas las ocurrencias que coincidan con un valor:
Código Delphi [-]
...
  with ZQuery1 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('SELECT * '); // (O la selección de campos que desees)
    SQL.Add('FROM ASIGNADOSA T1, ASIGNADOSB T2');
    SQL.Add('WHERE T1.SIERRAS = :PVALUE AND T2.SIERRAS = :PVALUE');
    ParamByName('PVALUE').Value := Valor_Condicion;
    Open;
  end
...

Saludos :)

jonydread 19-05-2013 23:26:37

muchisimas gracias el codigo o no me funciono o no supe ocuparlo pero me has dado una exelente referencia o idea y lo hice así y por separado
Código Delphi [-]
function TMainform.ExisteEnAmbas(aValue: Variant): Boolean;
begin
  with ZQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Text := 'SELECT * FROM AsignadosA where Sierras = :PVALUE';
    ParamByName('PVALUE').Value := aValue;
    Open;
   if not zquery1.IsEmpty then
   begin
    MessageDlg('Sierra ya se encuentra asignada', mtWarning, [mbOk],0);
     Abort;
      end else
      with ZQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Text := 'SELECT * FROM AsignadosB where Sierras = :PVALUE';
    ParamByName('PVALUE').Value := aValue;
    Open;
   if not zquery1.IsEmpty then
   begin
    MessageDlg('Sierra ya se encuentra asignada', mtWarning, [mbOk],0);
     Abort;
      end;
  end;
end;
end;

y el llamado
Código Delphi [-]
ExisteEnAmbas(dblookupcombobox1.KeyValue);

Gracias y saludos!!!
v:-)v#:-)#


La franja horaria es GMT +2. Ahora son las 14:03:57.

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