Question

How to change a generator value with dbExpress framework? I want to change generator value directly with dbExpress, without writing a stored procedure in RDBMS side or etc. Please help me for doing that.

Was it helpful?

Solution

AFAIK DBExpress don't have any class specialized in dealing with sequences/generators.

You still can use a standard TSQLQuery to instruct the dbEngine to alter a generator value, like this:

procedure TMyDataModule.RestartMyGenerator;
begin
  Q := TSQLQuery.Create;
  try
    Q.SQLConnection := MyConnection;
    //compatible with firebird 1.x and 2.x
    //Q.SQL.Text := 'set generator mygenerator to 0';  
    //better alternative, but compatible only with firebird 2.x
    Q.SQL.Text := 'alter sequence mygenerator restart with 0';
    Q.ExecSQL;
  finally
    Q.Free;
  end;
end;

(untested code, written directly in this window)

OTHER TIPS

Function with param TableName returns the next Generator value

function TDM.GenID(TableName: string): Integer;
var
  Qry: TSQLQuery;
begin
  Qry := TSQLQuery.Create(Self);
  try
    Qry.SQLConnection := SQLConnection;
    Qry.SQL.Add('SELECT GEN_ID(GEN_' + TableName + '_ID' + ', 1) ' +
                'FROM RDB$DATABASE');
    Qry.Open;
    Result := Qry.Fields[0].AsInteger;
  finally
    Qry.Free;
  end;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top