Insert Identity field

  • I'm trying to insert data into a table with an Identity field. I get an error message:

    [SQL Server Destination [162]] Error: An OLE DB error has occurred. Error code: 0x80040E2F. An OLE DB record is available. Source: "Microsoft SQL Native Client" Hresult: 0x80040E2F Description: "The statement has been terminated.". An OLE DB record is available. Source: "Microsoft SQL Native Client" Hresult: 0x80040E2F Description: "Cannot insert the value NULL into column 'DQS_MtReportsId', table 'MQDW.dbo.DQS_MtReports'; column does not allow nulls. INSERT fails.".

    Is there a way to allow SQL to auto-increment these values during the INSERT? Thanks.

  • Don't reference the Identity column in the insert statement and the DB will automatically assign the next value available. 

    Create table t1 (col1 int identity not null, col2 int)

    then the insert statment would be:

    insert into t1 (col2) values (10)

    and the DB would assign a value to col1 automatically.

    James.

     

  • thanks JLK. i was actually pointing to the wrong table. it's working.

Viewing 3 posts - 1 through 2 (of 2 total)

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