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
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;
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