[fpc-pascal] MSSQL and ftAutoInc: FPC 2.6.5 changes the behavior of 2.6.2 but AutoInc not works yet

Marcos Douglas md at delfire.net
Thu Apr 24 16:13:12 CEST 2014


Hi,

I'm using 2.6.5 (fixes_2_6) now but something broke between 2.6.2 and
2.6.5 using AutoInc fields.
AutoInc never worked well (at least for MSSQL) and because that I have
my own "Query" [1] that implements AutoInc values very well.

I override the ApplyRecUpdate method from TSQLQuery and update the
AutoInc field getting the value in DB using FLib.GetLastAutoIncValue
method that, for MSSQL, I use "select scope_identity() as id" (see
code below)

The problem is to use Field.SetData to put a value. If the field is
AutoInc it is ReadOnly too and I got a exception:
      Edit;
      Fields[I].SetData(@LastId);
      Post;

But in 2.6.2 version it worked.

I need to know the ID (identity column). So, if the behavior was
changed between 2.6.5 and 2.6.2, then the AutoInc fields needs to
works too, no?

Now, how I can get the ID using AutoInc, please?

=== BEGIN CODE ===
procedure TghSQLdbQuery.ApplyRecUpdate(UpdateKind: TUpdateKind);
var
  I: Integer;
  LastId: NativeInt;
  Fld: TField;
begin
  inherited;

  if UpdateKind <> ukInsert then
    Exit;

  for I := 0 to Fields.Count -1 do
  begin
    Fld := Fields.Fields[I];
    if Fld.IsNull and
       ((Fld.DataType = ftAutoInc) or (LowerCase(Fld.FieldName) =
'id') and (Fld is TNumericField)) then
    begin
      LastId := FLib.GetLastAutoIncValue;
      if LastId <= 0 then
        Exit;

      Edit;
      Fields[I].SetData(@LastId);
      Post;
      Exit;
    end;
  end;
end;
=== END CODE ===

[1] https://github.com/mdbs99/Greyhound

Regards,
Marcos Douglas



More information about the fpc-pascal mailing list