Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi....

Friends , my query is

how to increment a column's value by 1.

For ex. Suppose a column ID has values 1,2,3,4
Now when I update this table then ID column should increment by 1.

Now ID will become 2,3,4,5..

Thanks.
Posted
Updated 15-Feb-12 2:01am
v2
Comments
Herman<T>.Instance 15-Feb-12 7:56am    
have some code to share?
why increment an ID value? ID 1 does not exist anymore and previous ID 2 has now value of 1?

1 solution

Make the column and identity column. Then you don't have to do any work.
 
Share this answer
 
Comments
Varinder Raii 15-Feb-12 8:15am    
i want this in UPDATE QUERY
ZurdoDev 15-Feb-12 8:20am    
Then do UPDATE table SET column = (SELECT MAX(column) + 1 FROM table) WHERE x=y but you'll want to do transactions and proper locking so you get the right value if 2 people run it at the same time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900