AutoNumber in SQL Server?

Results 1 to 2 of 2

Thread: AutoNumber in SQL Server?

  1. #1
    Join Date
    Dec 1969

    Default AutoNumber in SQL Server?

    In Access I always used to have the first field in the table as &#039ID&#039 and have it set as &#039AutoNumber&#039, so when ever a new record it was automatically assigned a number incremented from the previous record. However in SQL Server/Design view I cant seem to do this. Any ideas how?

  2. #2
    Join Date
    Dec 1969

    Default RE: AutoNumber in SQL Server?

    It is called an Identity Column. You must specify the seed and the increment of the values. (1,1) will start at one and increase in intervals of 1.<BR><BR>You can run this example script in SQL query tool that creates the table:<BR><BR>/* Example Script */<BR>Create Table Customer<BR>(<BR>ID int identity(1,1),<BR>CustName varchar(50) not null,<BR>CustAccNo varchar(10) not null<BR>)<BR>GO<BR>/* End of example script */<BR><BR>You can insert a record by executing the following<BR>/* Example Insert */<BR>INSERT Customer(CustName, CustAccNo) VALUES (&#039Matthew&#039,&#039M0001&#039)<BR>/* End of example insert */<BR><BR>Now to get the ID value of the record you just entered, simply execute the query:<BR><BR>/* Example of getting last new ID value entered */<BR>Select @@IDENTITY<BR>/* End of Example Query */<BR><BR><BR>Cheers<BR>Matthew Deeks<BR><BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts