RE: Sequences and Serials
Posted in 1999
Topics: Triggers, Constraints & Referential Integrity
What are you using to access teh database? Will >===== Original Message From troegers13@my-deja.com ===== >I am working with Informix for the first time. I understand the >basics of the Serial datatype. The problem that I am encountering is >that I need to get the value of the Serial field after the record has >been inserted, to use as a foreign key for records in other tables. I >have experience programming with Oracle and have used sequences in the >past which work very well for solving this problem. So my question is: >Is there a way to get the value of the key (Serial field) for the >record that was inserted? > > >Sent via Deja.com http://www.deja.com/ >Before you buy. ------------------------------------------------------------ This e-mail has been sent to you courtesy of OperaMail, as a free service from Opera Software, makers of the award-winning Web Browser, Opera. Visit us at http://www.opera.com/ or our portal at: http://www.myopera.com/ Your free e-mail account is waiting at: http://www.operamail.com/ ------------------------------------------------------------
I will be using ODBC. So far the only thing that I have found would be: SELECT DBINFO('sqlca.sqlerrd1') FROM table Any other ideas? Todd --------------------------------------------- In article <80smr8$crn$1@news.xmission.com>, William Rice <ricew@operamail.com> wrote: > > What are you using to access teh database? > > Will > >===== Original Message From troegers13@my-deja.com ===== > >I am working with Informix for the first time. I understand the > >basics of the Serial datatype. The problem that I am encountering is > >that I need to get the value of the Serial field after the record has > >been inserted, to use as a foreign key for records in other tables. I > >have experience programming with Oracle and have used sequences in the > >past which work very well for solving this problem. So my question is: > >Is there a way to get the value of the key (Serial field) for the > >record that was inserted? > > > > > >Sent via Deja.com http://www.deja.com/ > >Before you buy. > > ------------------------------------------------------------ > This e-mail has been sent to you courtesy of OperaMail, as a free service from > Opera Software, makers of the award-winning Web Browser, Opera. Visit us at > http://www.opera.com/ or our portal at: http://www.myopera.com/ Your free e-mail > account is waiting at: http://www.operamail.com/ > ------------------------------------------------------------ > > Sent via Deja.com http://www.deja.com/ Before you buy.
troegers13@my-deja.com wrote:
>
> I will be using ODBC. So far the only thing that I have found would be:
>
> SELECT DBINFO('sqlca.sqlerrd1') FROM table
>
> Any other ideas?
That is exactly what you need. In ESQL/C or 4GL you would access the
sqlca data structure directly and read the last inserted serial number
from the second field in the sqlca.sqlerrd array (index [1] in ESQL/C or
index [2] in 4GL) from there. The DBINFO option you note is available for
ODBC and other interfaces designed for less capable database servers like
Sybase and MS SQL Server that do not support this feature. ;-). So
immediately after the insert execute the following query:
SELECT dbinfo( 'sqlca.sqlerrd1' ) FROM systables where tabid = 1;
which will return the value to your program where you can bind it to a
host variable and use the value to insert the child rows. If you have
exactly one child row to insert into exactly one child table and do not
otherwise care what the serial number is you can use dbinfo directly in
the second INSERT statement and save a step thus:
INSERT INTO child
SELECT dbinfo( 'sqlca.sqlerrd1' ), val_1, val_2, val_3
FROM systables where tabid = 1;
Art S. Kagel
> Todd
> ---------------------------------------------
>
> In article <80smr8$crn$1@news.xmission.com>,
> William Rice <ricew@operamail.com> wrote:
> >
> > What are you using to access teh database?
> >
> > Will
> > >===== Original Message From troegers13@my-deja.com =====
> > >I am working with Informix for the first time. I understand the
> > >basics of the Serial datatype. The problem that I am encountering is
> > >that I need to get the value of the Serial field after the record has
> > >been inserted, to use as a foreign key for records in other tables.
> I
> > >have experience programming with Oracle and have used sequences in
> the
> > >past which work very well for solving this problem. So my question
> is:
> > >Is there a way to get the value of the key (Serial field) for the
> > >record that was inserted?
> > >
> > >
> > >Sent via Deja.com http://www.deja.com/
> > >Before you buy.
> >
> > ------------------------------------------------------------
> > This e-mail has been sent to you courtesy of OperaMail, as a free
> service from
> > Opera Software, makers of the award-winning Web Browser, Opera.
> Visit us at
> > http://www.opera.com/ or our portal at: http://www.myopera.com/ Your
> free e-mail
> > account is waiting at: http://www.operamail.com/
> > ------------------------------------------------------------
> >
> >
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.