troubleshooting Question

Increment default value based on largest existing value

Avatar of brianmisty
brianmisty asked on
Microsoft SQL Server 2005
6 Comments1 Solution336 ViewsLast Modified:
I need to increment the default value of a field by 1 based on the largest number already in that column. I will need to be able to update this number at a later time, so obviously this is one reason why I cannot use the built-in auto-increment setting.  I am trying to determine the cleanest approach to do this, whether is be a trigger or function or some other idea.  I am a newb so your help would be appreciated.  

The table "Photos" is basically as follows...

PhotoID - primary key
PhotoName - varchar(20)
SortOrder - (int) - **this is the field that needs to increment. If the highest value is 2500, then the next insert needs to set this field to 2501.  Make sense?**

Using MS SQL Server 2005


Database Consultant
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros