Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

sql server - Implement Incrementing Counter in SQL

So I need to implement essentially an auto-increment column. I have a column in one table that has an int that will act as the counter, and there will be another table with a corresponding number of rows, each row having it's own counter. I thought this would be fairly simple to implement, but after doing some research I found a few forums where they mention there could be issues in situations like this if multiple transactions were running roughly at the same time, even by running the select,update, & insert in one transaction

After looking into though it seemed to me that if I used the OUTPUT clause to grab the new value the first table is getting updated with, I could avoid that issue entirely.

So this is the T-SQL I'm thinking of. Should this be okay for my situation?

BEGIN TRANSACTION;

DECLARE @tblCtrs table (Ctr int)
DECLARE @CtrVal INT

UPDATE Table1
SET Ctr = Ctr+1
OUTPUT inserted.Ctr INTO @CtrVals
WHERE id=<whatever id value>

SET @CtrVal = (SELECT TOP(1) Ctr FROM @tblCtrs)

INSERT INTO TABLE2 (Ctr, ....)
VALUES( @CtrVal, ....)

COMMIT;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand your question correctly, the concern is that concurrent transactions could cause unintended results with the counter value. You know how to manage the counter values just fine, it's just a matter of whether or not some other transaction could read/modify at same time. If that understanding is correct, that's what isolation levels are for. Depending upon how the data is accessed/stored (e.g. can there be multiple records with the same <whatever id value> or is that unique?) and application requirements (e.g. is it OK if <whatever id value> is not unique and there is insert of new record that is not updated due to timing) you should SET TRANSACTION ISOLATION LEVEL accordingly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...