AUTO INCREMENTO

Delphi

18/05/2009

ESTOU COM A SEGUINTE DUVIDA.

COMO FAÇO PARA DEPOIS QUE DIGITAR O ZERO E DEPOIS ENTER GERAR UM INCREMENTO DESSE CODIGO.

GRATO
ANDRÉ


Andsantos

Andsantos

Curtidas 0

Respostas

.lg.

.lg.

18/05/2009

Todo banco de dados tem um tipo auto-incremento ( pelo menos os que eu uso tem ). Mas a desvantagem é que se você deleta um numero...
Ta lá a sequencia...:
1 2 3 4 5 6 . . .
E você vai e deleta um registro... O 3th registro, fica assim:
1 2 4 5 6 . . . 
O auto incremento não entende esse numero que esta faltando. Ele vai no último e acrescenta +1.

Eu fiz uma função que lhe permite procurar falhas no código ( como mostrei acima ).

Pra isso você precisa de um DataSet query pra trazer somente o código.
function GetNextCode: Integer;
var
  num1: Integer;
  num2: Integer;
begin
  num1 := 0;
  num2 := 0;

  with adqCodigo do begin
    adqCodigo.Close;
    Open;

    if not (IsEmpty) then begin
      First;
      num1 := adqCodigoCDTABELA.AsInteger;
      while not Eof do begin
        Next;
        if ((adqCodigoCDTABELA.AsInteger - num1) = 1) then begin
          num1 := adqCodigoCDTABELA.AsInteger;
        end else begin
          num2 := (num1 + 1);
          break;
        end;
      end;

      if (num2 = 0) then begin
        Last;
        num2 := adqCodigoCDTABELA.AsInteger;
      end;
      adqCodigoCDTABELA.Close;
    end else Result := 1;
  end;

  Result := num2;
end;

Talvez possa ter variáveis atoa, mas o resultado é o esperado. Dê uma estudana nele, se ainda lhe vier dúvidas, posta ae que damos um help.

Att,
.lg.


GOSTEI 0
Andsantos

Andsantos

18/05/2009

Obrigado pela dica.Me ajudou!!


GOSTEI 0
POSTAR