Ver Mensaje Individual
  #2  
Antiguo 21-08-2013
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Reputación: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola steelha.

Supongamos que tu tabla se llama ESTACIONES y tiene dos campos ID y ACTIVA, identificador y estado de la estación respectivamente, también que el estado:
  • ACTIVA = 0 si esta fuera de servicio.
  • ACTIVA = 1 si esta en servicio.

Código Delphi [-]
...
function TForm1.GetActiveServiceStation: Integer;
var
  MaxRegs : Integer;
begin
  with un_Query do
  begin
    // Obtener el número total de estaciones de servicio activas
    Close;
    SQL.Text := 'SELECT COUNT(*) AS MAX_REG FROM ESTACIONES WHERE ACTIVA = 1';
    Open;
    if isEmpty then
      raise Exception.Create('No hay estaciones de servicio activas');
    MaxRegs :=  FieldByName('MAX_REG').AsInteger;

    // Seleccionar una estación de servicio activa al azar
    Close;
    SQL.Text := 'SELECT ID FROM ESTACIONES WHERE ACTIVA = 1';
    Open;
    Randomize;
    Locate('ID', Random(MaxRegs), []);
    Result := FieldByName('ID').AsInteger;
    Close;
  end;
end;

Llamada ejemplo:
Código Delphi [-]
procedure TForm1.btnGenerarClick(Sender: TObject);
begin
  ListBox1.Items.Add(IntToStr(GetActiveServiceStation));
end;

Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita