문제

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.

도움이 되었습니까?

해결책

Try this:

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

다른 팁

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top