Subject | Re: [firebird-support] How to enforce sort order? |
---|---|
Author | Martijn Tonies |
Post date | 2012-07-13T12:30:17Z |
Hi,
If you want unique numbers, use a GENERATOR:
create generator mygenerator;
insert into table(sort_order) values (gen_id(mygenerator, 1));
With regards,
Martijn Tonies
Upscene Productions
http://www.upscene.com
Download Database Workbench for Oracle, MS SQL Server, Sybase SQL
Anywhere, MySQL, InterBase, NexusDB and Firebird!
>I have the following stored procedure (simplified) for adding a new record:Tables are not locked.
>
> SELECT COUNT(*) FROM table INTO :number;
> INSERT INTO table(sort_order) VALUES(:number+1);
>
> If I call this stored procedure 1000 times asynchronously, some of the
> numbers on sort_order are duplicates.
>
> How do I enforce the sort_order so that there are no gaps and no
> duplicates?
>
> BTW, I thought the stored procedure was supposed to execute in a
> transaction which locks the table until it completes?
If you want unique numbers, use a GENERATOR:
create generator mygenerator;
insert into table(sort_order) values (gen_id(mygenerator, 1));
With regards,
Martijn Tonies
Upscene Productions
http://www.upscene.com
Download Database Workbench for Oracle, MS SQL Server, Sybase SQL
Anywhere, MySQL, InterBase, NexusDB and Firebird!