Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Best Solution for Contiguous TrackingNumber

$
0
0

One way to generate contiguous TrackingNumber is using SELECT MAX(TrackingNumber) +1 computation.  This method (used before IDENTITY/SEQUENCE was invented) is processing intensive and can cause blocking or even deadlocks.

Is there a better way?  Thanks.

CREATE TABLE Tracking(
    ID BIGINT IDENTITY(1,1) PRIMARY KEY,
	TrackingID [uniqueidentifier] NOT NULL UNIQUE,
	TrackingNumber [bigint] NOT NULL UNIQUE,
	TrackingAsset [uniqueidentifier] NOT NULL UNIQUE,
	ModifiedDate datetime2 default (sysdatetime())
);

INSERT Tracking(TrackingID, TrackingNumber, TrackingAsset) VALUES (NEWID(), 1, NEWID());
GO

INSERT Tracking(TrackingID, TrackingNumber, TrackingAsset) 
VALUES (NEWID(), (SELECT MAX(TrackingNumber)+1 FROM Tracking), NEWID());
GO 9

SELECT * FROM Tracking ORDER BY TrackingNumber;
/*
ID	TrackingID	TrackingNumber	TrackingAsset	ModifiedDate
1	0C799924-A820-45C4-9F8D-495F501A2F7C	1	AD8744AD-B055-481B-84BC-1F20750B1337	2014-02-18 08:53:49.9136069
2	DC58CB6B-7BFC-42D6-A670-01CF6F88C887	2	264167F5-044E-48EF-9A67-26C2D62E0705	2014-02-18 08:55:11.9212975
3	3716F937-92C3-40DF-9369-9E4B8B646F62	3	B277F6E9-C5E2-4FEF-A648-8D6BF4D4077D	2014-02-18 08:55:11.9362984
4	B1BBB174-D9A3-45D5-8E1E-D3D9DC539C85	4	8832DC7A-AD22-4F50-80D0-619A29CAB341	2014-02-18 08:55:11.9382985
5	4150235D-565B-4EEC-8F3D-A029A74E020F	5	C48B5998-AEB5-444F-A783-309B6215277A	2014-02-18 08:55:11.9392985
6	0D690CBB-A1FF-412E-AF33-FF50F394BA57	6	F1F88C67-3105-4204-810D-0D3C3E9A7F3C	2014-02-18 08:55:11.9412986
7	1E02C4C3-0B52-44F6-804E-F063C558094F	7	71301EC3-7C88-476A-BDF2-FFD96F470452	2014-02-18 08:55:11.9422987
8	53561890-1DEA-4518-B78A-F238F76E77D9	8	1BBFDEA5-7233-41E6-9741-205998223D10	2014-02-18 08:55:11.9432988
9	19E27235-E1EC-4E13-92D4-16A7BD7C51F6	9	4D860003-4D6C-4C62-9302-5BE5E65A2ED8	2014-02-18 08:55:11.9442988
10	E0382932-2FC1-4408-B318-782C741ECA26	10	E4BE5CBB-25AE-4270-AE57-8ECB7E1B0F68	2014-02-18 08:55:11.9452989
*/




Kalman Toth Database & OLAP Architect SELECT Video Tutorials 4 Hours
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012







Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>