Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get next available Id number, excluding 666

Author  Topic 

lindsaykesler
Starting Member

5 Posts

Posted - 2010-10-13 : 15:29:32
Hello, I have a script to get the next available Id number starting from a predetermined number (ex. 101) and I now need to exclude the number 666. Here is my script...

If (Select Min(ServingNum) From dbo.Student Where ServingNum >
(101 - 1)) = 101
Select IsNull(Min (aa.ServingNum + 1), 101) As Id
From dbo.Student As aa
Left Join dbo.Student As bb On (aa.ServingNum + 1) = bb.ServingNum
Where bb.ServingNum Is null And aa.ServingNum + 1 >= 101
Else
Select 101 As Id

I tried adding 'And (aa.ServingNum + 1) <> 666' but if Id's 666-670 and 680-700 are all available it would skip over the entire 666-670 section and return 680.

Thanks for any help!

Lindsay

X002548
Not Just a Number

15586 Posts

Posted - 2010-10-13 : 21:06:00
ummm...Lindsay...pretty convoluted wouldn't you say?

In Business terms...like you were writing a spec...tell us whatr you are trying to do...

Thanks



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx





Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2010-10-14 : 01:49:23
what's so bad about 666 anyway. it comes right after 665. if I had ticket 666 and you skipped me I'd be mad.


elsasoft.org
Go to Top of Page

lindsaykesler
Starting Member

5 Posts

Posted - 2010-10-14 : 09:23:37
Brett - I'm trying to "get the next available Id number starting from a predetermined number (ex. 101) and I now need to exclude the number 666." Sorry, I thought that explaination was pretty clear but I will break it down. I have a Student table and that table contains a ServingNum (int) column and that SertingNum is auto-generated. The user can select a starting number for the ServingNum, in my example the starting number is 101. So the first student created is assigned ServingNum 101, the second 102, the third 103 and so on... My issue is that when students are deleted we would like to reuse their ServingNum so we can't just increment from the greatest ServingNum used. My script was working great for this until our customers requested that we don't assign a student a ServingNum of 666. So now I'm am looking for any suggestions on how to alter my script, or a new one, so that the number 666 is never returned.

Thank you,
Lindsay
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-10-14 : 09:56:51
ahh..reasigning ID's....in my experience, that is a very bad idea

Didn't Jeff come up with something awhile ago?

So let me rephrase your req

I need to find the first available number gap and use that one

If no gaps exist, I want the max + 1

And at no point do I want to use 666

Question: What about 13? Or Prime numbers?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx





Go to Top of Page

lindsaykesler
Starting Member

5 Posts

Posted - 2010-10-14 : 10:30:53
I guess I misspoke, we are not really reassigning ID's, the StudentNum is not the primary Id, it is just a number assigned to a student. The Student table also has an "Id" field which is the primary id as well as an IdSeed field, which is an index, since they can choose what to set as the "Id".

Your rephrase sounds correct and so far we have not had any complaints about 13 or prime numbers.

Thanks,
Lindsay
Go to Top of Page
   

- Advertisement -