I am trying to update a column in sql server table which is used to generate ID's: Table structue is something like this
Name varchar(50), LastID long
In last ID we keep Last ID we have generated for something say StudentID. So data will look like this
Name :Student, LastID : 50001
This last ID is incremeting by 1 to generate a unique Id for (primary key).
Now there is a situation that where we have to reset this LastID to 0 when it reacheachs to Max (say 100000). So I am using
Update Sequence
set LastID = Case when LastID + @range >= @max then 0 else LastID end
where Name ='StudentID'
Range is parameter we pass to our store procedure generaly it's 100 in my case. So before seting the LastID I check whether it's exceiding Max for student ID i.e. 1,00,000.
What bothers me in above update query I am again updating LastID to it's current value if it's not excedding the range. Looking for a better alternative. What I don't want to do is:
Using select and keeping LastID in a variable or something like this.
I just want to do in a single update if it's possible.
It's a sql server 2008 databse so I can't use inbuilt sequence feature of sql server 2012.
If more inforamtion is required please let me know.