How to Insert Properly

  • When I run the following statement, I receive the error message: Cannot insert the value NULL into column 'PK'"

    Insert Into ComTrace (Div,Stag)

    Select Distinct Div,Stag

    From RefVal;

    Understand why but not sure how to resolve it. Any suggestions?

  • Probably you have NULLs in RefVal.Div. Maybe you could add

    WHERE RefVal.Div is not NULL

    Edit: I was mistakenly thinking 'Div' was a primary key.

  • I do not have NULL value. The PK value is set to auto increment and since I am not passing the PK value, its returning the error. The question is when insert is taking place, I'd like PK value to generate automatically instead of having to hard code it.

  • Looks ok to me.

    Double check it's set to auto increment, and not just set to not allow nulls.

  • I made a mistake. It appears the Identity Specification is set to no. The PK column, however, does not allow NULL. Is there a way to generate PK value automatically when those two values (Div column and Stag Column) are being inserted?

  • Obvious way is to set identity=yes. Otherwise I think you would have to write code to maintain the values. Maybe there's another way ?

Viewing 6 posts - 1 through 5 (of 5 total)

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