Simultaneous call from many users from an application, @intID_1 is generates duplicate value ( or same value ) for more than one user. This cause a Primary Key vialotion error. To avoid this I tried Locking and Isolation levels such as SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; WITH (HOLDLOCK) WITH (XLOCK) etc
at this stage I am getting a DeadLock Error!! when trying to call the procedure same time by many users. Can I block the second user till the Transaction Commit, some how? Note:- Due to some reason I really want to avoid the intID field as Auto Increment.
(comments are locked)
|
|
With regards only to your stated requirments, reversing your clustered PK contraint keys to (intLocationID, intID) would make insert much more efficient. That time savings may resolve a lot of your problem. You physical storage order would probably make more sense that way as well if typically deal with Sales by location. That could make any solution better as well - including Kristen's solution. Of course that may not be practical if you have a lot of un-stated requirements that rely on that the way it is...
(comments are locked)
|
|
Would INSERT ... SELECT work instead of INSERT ... VALUES
INSERT INTO tblSales
(
[intID],
[dtDate],
[fltAmount],
)
SELECT
[intID] =
(
SELECT Isnull( Max( intID ) + 1 , 1 )
FROM tblSales
WHERE intLocationID = @intLocationID_2
),
@dtDate_8,
@fltAmount_9
You'd have to come up with a way to return the ID assigned. If you also had an IDENTITY column you could get the Identity value assigned by the INSERT, and then select the intID from that record.
(comments are locked)
|
|
Reply to TG,
Reason for avoiding Identity column in intID is 1) ID is generated by a Pattern according to certain parameters such as Location, Zone, Dealer etc. 2) Need continues numbers from each location ( While RollBacking Idex is missing ) 3) Many Conditions we may need to reset those intID value of a location to specific Number etc These are few which i could recollect for the time being.
(comments are locked)
|


"Due to some reason I really want to avoid the intID field as Auto Increment." Can we ask why? Please post the structure, constraints, indexes, and what column(s) make up a clustered index for tblSales.
ID is contiguous per location, perhaps?