SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


generating and storing a counter


generating and storing a counter

Author
Message
Stef Teal
Stef Teal
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

Group: General Forum Members
Points: 194 Visits: 100
I need to sequentially number events created by users with an incremental counter. In the events table, I have a unique key on the combination of OrgId, TypeId, and OrgEventCounter. Are there any “best practices” on how best to handle the unlikely possibility that 2 users will try to save at the same time with the same OrgEventCounter? Here’s a stripped down version

DECLARE @OrgEventCounter int

SET @OrgEventCounter =
(SELECT MAX(OrgEventCounter)
FROM dbo.Event
WHERE OrganizationID = 2325 AND DocumentTypeOrgID = 1) + 1

INSERT dbo.Event
(OrganizationID, TypeID, OrgEventCounter)
VALUES (1234, 1, @OrgEventCounter)

I could trap for :

Msg 2627, Level 14, State 1, Line 9
Violation of UNIQUE KEY constraint 'IX_DocDataEvent'. Cannot insert duplicate key in object 'dbo.DocDataEvent'.

and loop until it works, but it seems there should be a more elegant way. Any insight is appreciated.
Bruce W Cassidy
Bruce W Cassidy
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1267 Visits: 1033
The way I would do this is to keep the counter in a separate table, and use an identity field on that table as the counter.

However, if you combine your two queries, and use a suffiently high isolation level, it should also do the trick:


--
-- if there is no trigger on your Event table, then use the following
-- logic to get the next counter
--
update Quantifi.dbo.DocDataEvent
set @OrgEventCounter = OrgEventCounter = OrgEventCounter + 1
where OrganizationID = 2325 and
DocumentTypeOrgID = 1

--
-- assuming you have a trigger on the Event table that updates the
-- counter in the Quantifi.dbo.DocDataEvent table
--
set transaction isolation level serializable
begin transaction;
insert into dbo.Event(
OrganizationID,
TypeID,
OrgEventCounter
)
select 1234,
1,
max(OrgEventCounter) + 1
from Quantifi.dbo.DocDataEvent
where OrganizationID = 2325 and
DocumentTypeOrgID = 1;
commit;



Florian Reischl
Florian Reischl
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3427 Visits: 3934
Hi

There are several possible practices.

If you only need an unique id you may use an IDENTITY INT column. If it does not matter if the id is an INT you can also use a TIMESTAMP or an UNIQUEIDENTIFYER.

I for my own do not like IDENTITY columns. Maybe you may add a ID-Table which maintains the IDs so you can cache a set of IDs in your client application and/or abstract the INSERT action into a procedure.

Greets
Flo


The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
J-F Bergeron
J-F Bergeron
SSC Eights!
SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)

Group: General Forum Members
Points: 993 Visits: 2707
florian.reischl (3/5/2009)
I for my own do not like IDENTITY columns.


Wow, that's some news there! How can you not like identity columns? I tell database developpers to put 1 on each and every table in the database. Only in really really rare scenarios we don't put an identity column.

What's your sight on this? It puts an easy uniqueness to the rows for transmitting to another server, it's a perfect field for the update statements, and so is it for the delete. What's so wrong with it?

Cheers,

J-F
Stef Teal
Stef Teal
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

Group: General Forum Members
Points: 194 Visits: 100
Sorry Bruce, for clarification, the tables in my 2 statements are actually the same table. I updated my original post.
Stef Teal
Stef Teal
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

Group: General Forum Members
Points: 194 Visits: 100
I do not have a trigger that updates the counter. I can add one if I decide that it's definitely the best option. I generally only use triggers when I have no other good solution.
noeld
noeld
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9486 Visits: 2048
Jean-François Bergeron (3/5/2009)
florian.reischl (3/5/2009)
I for my own do not like IDENTITY columns.


Wow, that's some news there! How can you not like identity columns? I tell database developpers to put 1 on each and every table in the database. Only in really really rare scenarios we don't put an identity column.

What's your sight on this? It puts an easy uniqueness to the rows for transmitting to another server, it's a perfect field for the update statements, and so is it for the delete. What's so wrong with it?


Wait until you start using replication ....


* Noel
Florian Reischl
Florian Reischl
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3427 Visits: 3934
Wow, that's some news there! How can you not like identity columns? I tell database developpers to put 1 on each and every table in the database. Only in really really rare scenarios we don't put an identity column.

What's your sight on this? It puts an easy uniqueness to the rows for transmitting to another server, it's a perfect field for the update statements, and so is it for the delete. What's so wrong with it?


Hi J-F

I don't like it because of some reasons:

Replication
Our DBAs say they don't like it becase of replicatiion problems. Don't ask me, I'm no DBA and do not know very much about replication. I don't really believe this... Hehe

Manipulation of many data
Our applications generate many data on client side before they become commited. We use an ID-Table which contains type (= table) specific ID-Ranges. So we can set new IDs to all new rows and then bulk insert them in one bulk.

The profit is that the client application can build all the data within the client application with their final IDs and without the @@IDENTITY select for further links to other objects (rows).

Resume
I think its just a subjective (maybe stupid) sight of me Wink .

Greets
Flo


The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
noeld
noeld
SSCrazy Eights
SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)SSCrazy Eights (9.5K reputation)

Group: General Forum Members
Points: 9486 Visits: 2048
florian.reischl (3/5/2009)
Wow, that's some news there! How can you not like identity columns? I tell database developpers to put 1 on each and every table in the database. Only in really really rare scenarios we don't put an identity column.

What's your sight on this? It puts an easy uniqueness to the rows for transmitting to another server, it's a perfect field for the update statements, and so is it for the delete. What's so wrong with it?


Hi J-F

I don't like it because of some reasons:

Replication
Our DBAs say they don't like it becase of replicatiion problems. Don't ask me, I'm no DBA and do not know very much about replication. I don't really believe this... Hehe

Manipulation of many data
Our applications generate many data on client side before they become commited. We use an ID-Table which contains type (= table) specific ID-Ranges. So we can set new IDs to all new rows and then bulk insert them in one bulk.

The profit is that the client application can build all the data within the client application with their final IDs and without the @@IDENTITY select for further links to other objects (rows).

Resume
I think its just a subjective (maybe stupid) sight of me Wink .

Greets
Flo


Hehe Your DBAs are RIGHT Hehe


* Noel
J-F Bergeron
J-F Bergeron
SSC Eights!
SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)SSC Eights! (993 reputation)

Group: General Forum Members
Points: 993 Visits: 2707
noeld (3/5/2009)
florian.reischl (3/5/2009)
Wow, that's some news there! How can you not like identity columns? I tell database developpers to put 1 on each and every table in the database. Only in really really rare scenarios we don't put an identity column.

What's your sight on this? It puts an easy uniqueness to the rows for transmitting to another server, it's a perfect field for the update statements, and so is it for the delete. What's so wrong with it?


Hi J-F

I don't like it because of some reasons:

Replication
Our DBAs say they don't like it becase of replicatiion problems. Don't ask me, I'm no DBA and do not know very much about replication. I don't really believe this... Hehe

Manipulation of many data
Our applications generate many data on client side before they become commited. We use an ID-Table which contains type (= table) specific ID-Ranges. So we can set new IDs to all new rows and then bulk insert them in one bulk.

The profit is that the client application can build all the data within the client application with their final IDs and without the @@IDENTITY select for further links to other objects (rows).

Resume
I think its just a subjective (maybe stupid) sight of me Wink .

Greets
Flo


Hehe Your DBAs are RIGHT Hehe


Wanna state why? From what I read, you can give servers different identity seeds (like separate to get 4 seeds).

First is even positive numbers
Second is even negative numbers
Third is not even positive numbers
Fourth is not even negative numbers.

What are the issues of Identity columns on a replication action?

Cheers,

J-F
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search