Autonumbering & Identity ColumnsBy Bill Graziano on 25 June 2000 | Tags: Identity John writes "I'd like to be able to automatically number each row as it comes into the table. Is possible to do this? How do I know what the value of the row I just inserted is?" Well John, What you can do is set up an identity column in SQL Server. This will autonumber the rows. In Enterprise Manager, start by setting a column with a datatype of Note that if you delete all the rows in a table and start adding rows, the identity column will pick up where it left off. You can reset it but that's a topic for another day. The script to create a table looks like this: CREATE TABLE [dbo].[Items] ( [ItemID] [int] IDENTITY (1, 1) NOT NULL , [ItemName] [char] (50) NOT NULL ) You can use the @@IDENTITY function to return the value you just inserted. -graz
|
- Advertisement - |