I recently came across some code which generates an identity value for a key:
DECLARE @ID INT;
SELECT @ID = MAX(ID) + 1 FROM Person;
INSERT INTO [Person]
([ID], [FirstName], [LastName])
VALUES
(@ID, @FirstName, @LastName);
I brought this to the attention of a coworker who claimed that not using a built in auto incrementing field is a perfectly reasonable approach to database design and in some circles a best practice. The coworker's rational is that it gives you more control over the values and allows you to manipulate the data more easily.
I could only counter by pointing out that it just seems silly to do implement your own version of a feature that is supported by the server. And it is extremely error prone should someone decide to use a different method to insert a record.
Are there really any good reasons not to use an Identity column when all you need is an artificial key on a table?
SELECT MAX()
is often be very, very slow and gets slower as the table gets bigger. There's little to discuss once you have measurements.