This has been troubling me for days, I have a table with auto-increment, what I want is that the value of the increment value will be inserted in the same row. The problem is that when concurrent users are accessing the query, it will not copy the correct value.
Below is my table:
CREATE TABLE tblTransactions
(
dctransno INT IDENTITY PRIMARY KEY,
vcDescription VARCHAR,
dcTransNoCopy INT NULL,
user VARCHAR
)
and my query:
BEGIN TRAN
INSERT INTO tblTransactions
(dctransno,
user)
VALUES (@vcDescription,
@user);
SET @identity = @@IDENTITY -- get the identity
UPDATE tblTransactions
SET dcTransNoCopy = @identity
WHERE dcTransNo = @identity
AND user = @user; -- insert
COMMIT TRAN
What I want to achieve is that I can have a grouped transaction like below example. The problem is that my above query doesn't take ownership. Assuming userA and userB are simultaneously using the system:
dcTransNo Description dcTransNoCopy User
1 Transaction1 1 userA
2 Transaction2 1 userA
3 Transaction3 1 userA
4 Transaction1 3 userB ---> not correct, ClonetransNo value for userB should be 4 not 3.
Please help, thanks in advance.