Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sequence / Identity Project

Author  Topic 

bsilvertab
Starting Member

2 Posts

Posted - 2009-04-10 : 14:47:23
I am doing a project for school which requires me to increment a row called sequence (int) everytime the CustomerID field changes. Here is what I am talking about....

CustomerID char(10) StaffID char(6) Sequence int
X8X606LHH JACTAM 1
X8X606LHH KORSTA 2
X8X606LHH ROOKEN 3
X8X606LHH WAGGAR 4
X8X607DG LEVDIA 1
X8X607DG ROOKEN 2
X8X626EAB GUZKEL 1
X8X626EAB SWIGLO 2
X8X631ET MORKAR 1
X8X631ET TOWCON 2
X8X311MLS SCHDEN 1
X8X311MLS VANKAR 2
X8X323BMW KORSTA
X8X323BMW ROOKEN
X80307CRC GUZKEL
X80307CRC SWIGLO


This goes on for 350 rows. I think that the Identity command should be used to 'reseed' the sequence row, but I don't know how to make that happen everytime the customerID field changes. Any insight or help would be much appriciated.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-10 : 15:15:07
You can not use identity for this.

The table design is actually what we have in production right now for a particular system and it is causing deadlocks. I would highly recommend not using this approach (sequencer per customer) and instead just use an identity column (sequencer for the table).

To answer your question, you'll need to find the max value for that customer and increment it by 1. The deadlocking issue occurs due to the need to lock the row and other things wanting to query it. I'm not sure if your school project would require the locking mechanism in there, but it is required to guarantee the integrity of the data.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

bsilvertab
Starting Member

2 Posts

Posted - 2009-04-10 : 15:31:31
tkizer- I wouldn't do my table this way either, but that is what the assignment is requiring, so I am not worried about deadlocks. Do you have any examples of how to find a max value for the customer and increment it by 1? How do you find a max value of a char?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-10 : 15:38:05
INSERT INTO YourTable(CustomerID, ..., Sequence)
SELECT 'SomeValue', ..., (SELECT MAX(Sequence) + 1 FROM Table1 WHERE CustomerID = 'SomeValue')

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -