Ver Mensaje Individual
  #6  
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.

Es extraño... Y mas aún cuando en el código que comentaste (desactivaste) no existe referencia alguna al campo ID. Para salir de toda duda acabo de hacer una pequeña prueba en Firebird.

Cree la tabla:
Código SQL [-]
CREATE TABLE TBLREPRESENTANTE (
  ID INTEGER,
  DESCRIPCION VARCHAR(50),
  ACTIVO CHAR(1) CHECK(ACTIVO IN ('S','N')),
  ONLINE CHAR(1) CHECK(ONLINE IN ('S','N'))
)
Le cargué 500 registros de prueba:
Código Delphi [-]
procedure TForm1.FormCreate(Sender: TObject);
const
  SINO : array[0..1] of string = ('N','S');
var
  i: Integer;
begin
  Randomize;
  if not IBTransaction1.Active then
    IBTransaction1.StartTransaction;
  with IBQuery1 do
  begin
    Open;
    SQL.Clear;
    SQL.Add('INSERT INTO TBLREPRESENTANTE (ID,DESCRIPCION,ACTIVO,ONLINE)');
    SQL.Add('VALUES(:ID,:DESCRIPCION,:ACTIVO,:ONLINE)');
    Prepare;
    for i:= 1 to 500 do
    begin
      ParamByname('ID').AsInteger := i;
      ParamByname('DESCRIPCION').AsString := 'DESCRIPCION '+IntToStr(i);
      ParamByname('ACTIVO').AsString := SINO[Random(2)];
      ParamByname('ONLINE').AsString := SINO[Random(2)];
      ExecSQL;
    end;
  end;
  try
    IBTransaction1.Commit;
  except
    on E: Exception do
    begin
      IBTransaction1.Rollback;
      raise Exception.Create(E.Message);
    end;
  end;
end;
Y usando la función del mensaje #2:
Código Delphi [-]
function TForm1.GetActiveServiceStation: Integer;
var
  MaxRegs : Integer;
begin
  with IBQuery1 do
  begin
    // Obtener el número total de estaciones de servicio activas
    Close;
    SQL.Text := 'SELECT COUNT(*) AS MAX_REG FROM TBLREPRESENTANTE WHERE ACTIVO = ''S''';
    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 TBLREPRESENTANTE WHERE ACTIVO = ''S''';
    Open;
    //  Randomize;  // (innecesario por que ya fué llamado en FormCreate)
    Locate('ID', Random(MaxRegs), []);
    Result := FieldByName('ID').AsInteger;
    Close;
  end;
end;
No genera ningún tipo de error y funciona a la perfección.

¿ Segura que el error se produce en la función ?

Saludos
__________________
Daniel Didriksen

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