Question

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.

Was it helpful?

Solution

Try this:

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

OTHER TIPS

Try

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

your code try to add with max id. so max(id) is exist then dublicate error will occured. But this method is not advised because when two user wants to add record then it may be give same max(ID).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top