Search code examples
sqlauto-incrementsap-ase

Auto increment PK on a column


I do not have the ID column defined to auto increase on insert. How do i write my insert statement to insert maxvalue +1 when i am inserting a new row every time.

Sybase is throwing an error if I try this. Is there any other way to solve this problem?

INSERT INTO TABLE(ID, FIRST_NAME,LAST_NAME) VALUES ( SELECT MAX(ID)+1 FROM TABLE,"JOHN","DOE")

ERROR: The name 'ID' is illegal in this context. Only constants, constant expressions, or variables allowed here. Column names are illegal.


Solution

  • Try this:

    INSERT INTO TABLE(ID, FIRST_NAME,LAST_NAME) 
    select MAX(ID)+1, 'JOHN','DOE' FROM TABLE