RE: Getting Serial Value
Posted in 1997
On Mon, 4 Aug 1997, Tauren Mills wrote:
> Thanks for this info!
>
> Since this is for a web application, there is the potential that several people
> could hit the site at the same time and generate several new INSERTS into the
> customers table. How do I know that what sqlerrd[1] contains is the value of
> the serial that I inserted and not the most recent serial added? I do not have
> access to a sqlca structure in my development environment (I'm using Netscape
> Livewire -- server-side-javascript, which is kind of like Microsoft Active
> Server Pages), so I am doing it like this:
>
> query = "INSERT INTO customers (cust_id, name, email) VALUES (0, null, null)";
> database.beginTransaction();
> errno = database.execute(query);
The sqlca.sqlerrd[1] is available immediately after the insert and
contains the serial value for the record just inserted. Since all
Informix error numbers are negative (except of course SQLNOTFOUND==100
which is never returned for an INSERT) you could have a stored
procedure which inserts the dummy record and returns either the
sqlca.sqlcode if error or sqlca.sqlerrd[1] if no error. Then:
stored_proc_stmt = "EXECUTE PROCEDURE InsertDummyCust()";
database.beginTransaction();
errno = database.execute( stored_proc_stmt );
if (errno < 0) {
--- diagnostics
database.rollbackTransaction();
} else {
cursor.cust_id = errno;
customer_id = cursor.cust_id;
database.commitTransaction();
}
Where the procedure InsertDummyCust() looks somthing like:
create procedure InsertDummyCust()
returning int;define err, snum int;
on exception set err end exception with resume;
LET err = 0;
INSERT INTO customers (cust_id, name, email) VALUES (0, null, null);if err < 0 then
return err;
else
let snum=DBINFO( 'sqlca.sqlerrd1' );
return snum;
end if
end procedure;
Art S. Kagel, kagel@bloomberg.com