Board index » delphi » multi user and auto increment fields

multi user and auto increment fields

Hello everybody,

I need to get the ID-value from a newly created row in a multi user
environment.
I can find two ways of doing it, not sure if they are ok, and which one is
preferable:
(all inside a stored procedure)

insert into orders(custname) values('ballon');
orderid=gen_id(genorderid,0);
insert into orderdetail(orderid,artid) values(:orderid,'1234');

or

fix the trigger so it wont run if id-value is sent:
CREATE TRIGGER "GETORDERIDGEN" FOR "ORDERS"
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ORDERID IS NULL) THEN NEW.ORDERID= GEN_ID(ORDERIDGEN, 1);
END
and then
orderid=gen_id(genorderid,1);
insert into orders(orderid,custname) values(:orderid,'ballon');
insert into orderdetail(orderid,artid) values(:orderid,'1234');

is there any difference between the both?

thanks
/t eriksson

 

Re:multi user and auto increment fields


Yes, there's a difference. The latter is multi-user safe, the first isn't.

--
Martijn Tonies
InterBase Workbench - the developer tool for InterBase and Firebird
http://www.interbaseworkbench.com

Upscene Productions
http://www.upscene.com

"This is an object-oriented system.
If we change anything, the users object."

Quote
"Tjelvar Eriksson" <tjemal...@hotmail.com> wrote in message

news:3b573b03_2@dnews...
Quote
> Hello everybody,

> I need to get the ID-value from a newly created row in a multi user
> environment.
> I can find two ways of doing it, not sure if they are ok, and which one is
> preferable:
> (all inside a stored procedure)

> insert into orders(custname) values('ballon');
> orderid=gen_id(genorderid,0);
> insert into orderdetail(orderid,artid) values(:orderid,'1234');

> or

> fix the trigger so it wont run if id-value is sent:
> CREATE TRIGGER "GETORDERIDGEN" FOR "ORDERS"
> ACTIVE BEFORE INSERT POSITION 0
> AS
> BEGIN
>   IF (NEW.ORDERID IS NULL) THEN NEW.ORDERID= GEN_ID(ORDERIDGEN, 1);
> END
> and then
> orderid=gen_id(genorderid,1);
> insert into orders(orderid,custname) values(:orderid,'ballon');
> insert into orderdetail(orderid,artid) values(:orderid,'1234');

> is there any difference between the both?

> thanks
> /t eriksson

Re:multi user and auto increment fields


Yes. The second method will work reliably in a multi-user environment while
the first will not. With the first method it is possible that another user
can increment the generator before you call GEN_ID(GENORDERID, 0).

--
Bill

Other Threads