AutoNumber Field?

  • Does anybody know how to set up an AutoNumber field in SQL Server, like in Access?

    I want a field that simply increments a number by 1 with each new record, and so creates a unique field.

    Thanks!

  • create table MyTable( MyID int identity( 1,1 ))

    Steve Jones

    steve@dkranch.net

  • Thanks! That was so easy.

  • You're welcome.

    Steve Jones

    steve@dkranch.net

  • One more question on this- I created a table with 2 fields, an ID field as you showed (called "Autonumber"), and a varchar field of length 40, called "deek". I then tried to run a data import into the deek field from a text file of 3 rows, where none of the data exceeds 40 characters, and none of it is null. I keep getting the error message:

    Error at destination for Row Number 3. Errors encountered so far in this task: 3.

    Insert error, column 1 ('Autonumber', DBTYPE_14), status 10: Integrity violation; attempt to insert NULL data or data which violates constraints.

    Unspecified error.

    Can I not insert several records at once into a table where there is a counter field like "Autonumber"?

  • are you trying to insert the identity field? Can you post the DDL for the talbe and the text file?

    Steve Jones

    steve@dkranch.net

  • The SQL code that created the table is:

    CREATE TABLE myID

    (

    Autonumber Int Identity(1,1),

    deek varchar(40)

    )

    The VBScript code for the import is: Function Main()

    DTSDestination("deek") = DTSSource("Col003")

    Main = DTSTransformStat_OK

    End Function

    The import takes rows 5-7, and the data in Colum003 is "01010709000010 " in each case.

  • Is Autonumber listed as one of your destination columns?

    K. Brian Kelley

    bkelley@sqlservercentral.com

    http://www.sqlservercentral.com/columnists/bkelley/

    K. Brian Kelley
    @kbriankelley

  • No, only "deek"

  • If you run rows 5-5 or 5-6, does it work?

    Steve Jones

    steve@dkranch.net

  • No, I tried that. I can't figure it out. It seems to think I'm trying to input into the autonumber field, although I'm not.

  • How are you running the data import?

    Steve Jones

    steve@dkranch.net

  • It's a Transform Data Task from a Text File to a SQL Server db. It works fine in my tables without identity fields.

  • Interesting. Haven't had issues and I do this type of import all the time, though usually from Excel.

    Are you positive there is no mapping from some field inthe text file to the identity field in the table?

    Steve Jones

    steve@dkranch.net

  • To eliminate variables for error, I created a table with only those two fields, and only defined the first column in the text file as well. So there's no way any other field could be mapped to anywhere.

Viewing 15 posts - 1 through 15 (of 22 total)

You must be logged in to reply to this topic. Login to reply