Re: automatically calculating ID field on INSERT

From: Joe Celko <71062.1056_at_compuserve.com>
Date: 2000/04/18
Message-ID: <8dij02$gnu$1_at_nnrp1.deja.com>#1/1


>> I have a table with a numeric ID field representing the primary key.
How can I use standard SQL to insert a record so that always a new value is created for the field ID ? <<

INSERT INTO MyTable (id, a, b, c, ...)
VALUES ((SELECT MAX(id) FROM MyTable) + 1, new_a, new_b, new_c, ...);

--CELKO--
Joe Celko, SQL and Database Consultant

Sent via Deja.com http://www.deja.com/
Before you buy. Received on Tue Apr 18 2000 - 00:00:00 CEST

Original text of this message