Re: How do I obtain the last serial number inserted into a table?
Posted in 1998
It depends on the programming language you use. If it's Informix 4GL or ESQL/C you get the information automatically available in the sqlca-record (a global variable as you ask for). RTFM to find out about this. For most other languages you will have to use the dbinfo function either directly in SPL (Informix Stored Procedure Language) or in a select statement in other languages. Also an RTFM thing. There is no other way I know of. The select max(xx) way sure doesn't work in general. You would have to do a lock on the table in exclusive mode for this to work with multiple users. Something I wouldn't want to do in most cases. The Oracle way of doing it works slightly differently, but it achieves much of the same purpose. The only additional thing I can think of that you can use the named sequences of Oracle for is to insert unique values within groups of rows in a table where the groups are defined by values in another column. You would create a named sequence for each group value. For this purpose we create multiple tables with a single row with a serial type column and insert into one of these tables to optain the next value in a sequence. We also normaly delete all rows (or all but the last inserted) to avoide that these tables keep growing in size. The tables don't need to contain any rows as the next serial value is optained by the database server from another location than the data in the table. There are of course other ways to do a similar thing. The only point is to show simple ways of doing essentially the same as you do with Oracle. On Fri, 10 Apr 1998 19:13:03 GMT, "EKWAK" <ekwak@encoredev.com> wrote: >How can I properly obtain the last serial number that was inserted into a >table with a serial column bearing in mind that there will be concurrent >users inserting into this same table? Is the call to the dbinfo function the >only way other than doing a select max( xx) from the table? Is there a >global variable that can be used? I know in Oracle you can select >name_seq.nextval before hand and then use this number however you like. Nils Myklebust NM Data AS Norway E-mail: Nils.Myklebust@nmdata.com FAQ at: Primary with ODBC info: http://www.smooth1.demon.co.uk Official site http://www.iiug.org/techinfo/faq/faq_top.html