[fpc-pascal] Re: How to insert a record and get the primary key with sqldb?

Michael Van Canneyt michael at freepascal.org
Sat Oct 22 17:42:01 CEST 2011



On Sat, 22 Oct 2011, Felipe Monteiro de Carvalho wrote:

> ugh, any ideas how I can obtain the next sequence value using sqldb
> and postgres then?
>
> I have never used sequence values before, and while I am googling I
> only found either answers which are specific for a particular
> framework (java, djambo, whatever) or people claiming that it should
> generate the index automatically! o.O Which clearly it is not doing...
> I couldn't yet get a generic answer using SQL supported by Postgres,
> or whatever thing which I could reuse in sqldb.

Just a use a TSQLQuery:

With TSQLQuery.Create(Self) do
   try
     Database:=MyDatabase;
     Transaction:=MyTransaction;
     // Replace sequencename with the correct name of the sequence
     SQL.Text:='SELECT nextval(''sequencename'')';
     Open;
     TheNewID:=(Fields[0] as TLargeIntField).AsLargeInt;
     close;
   finally
     free;
   end;

That's it.

Michael.



More information about the fpc-pascal mailing list