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.
Author |
Topic |
lpatters
Starting Member
3 Posts |
Posted - 2008-10-03 : 10:34:21
|
Hello, I have a table that contains columns Type_Business_Id and Type_Business. Whenever a new type of business is defined and needs to be added to the table I need to be able to search through the table and find any unused Ids and assign the unused Id to the new type. I know I need to do some kind of looping, but I can't seem to figure out exactly what I need to do. Any help would be appreciated.LPatters |
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-10-03 : 11:07:54
|
[code]create table #tmp ( Type_Business_Id int, Type_Business varchar(20))insert into #tmp ( Type_Business_Id, Type_Business )select 1, 'Test1' union allselect 3, 'Test3' union allselect 4, 'Test4' union allselect 5, 'Test5' union allselect 7, 'Test7' union allselect 8, 'Test8' union allselect 9, 'Test9'select Min(t1.Type_Business_Id+1) as Lowest_Available_Intfrom #tmp t1 left join #tmp t2 on t1.Type_Business_Id+1 = t2.Type_Business_Idwhere t2.Type_Business_Id is NULLselect t1.Type_Business_Id+1 as ALL_Available_Intsfrom #tmp t1 left join #tmp t2 on t1.Type_Business_Id+1 = t2.Type_Business_Idwhere t2.Type_Business_Id is NULL[/code]"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-03 : 11:17:50
|
quote: Originally posted by lpatters Hello, I have a table that contains columns Type_Business_Id and Type_Business. Whenever a new type of business is defined and needs to be added to the table I need to be able to search through the table and find any unused Ids and assign the unused Id to the new type. I know I need to do some kind of looping, but I can't seem to figure out exactly what I need to do. Any help would be appreciated.LPatters
why do you want to do this way? all you need to do is to declare Type_Business_Id as identity column so that it automatically gives you next id value and you dont have to worry too much for its generation. Also i wont personally be too much concerned about itermediate gaps caused by adhoc deletes of records. |
 |
|
lpatters
Starting Member
3 Posts |
Posted - 2008-10-03 : 14:04:18
|
quote: Originally posted by jhocutt
create table #tmp ( Type_Business_Id int, Type_Business varchar(20))insert into #tmp ( Type_Business_Id, Type_Business )select 1, 'Test1' union allselect 3, 'Test3' union allselect 4, 'Test4' union allselect 5, 'Test5' union allselect 7, 'Test7' union allselect 8, 'Test8' union allselect 9, 'Test9'select Min(t1.Type_Business_Id+1) as Lowest_Available_Intfrom #tmp t1 left join #tmp t2 on t1.Type_Business_Id+1 = t2.Type_Business_Idwhere t2.Type_Business_Id is NULLselect t1.Type_Business_Id+1 as ALL_Available_Intsfrom #tmp t1 left join #tmp t2 on t1.Type_Business_Id+1 = t2.Type_Business_Idwhere t2.Type_Business_Id is NULL "God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking
Thank you so much!LPatters |
 |
|
lpatters
Starting Member
3 Posts |
Posted - 2008-10-03 : 14:07:34
|
quote: Originally posted by visakh16
quote: Originally posted by lpatters Hello, I have a table that contains columns Type_Business_Id and Type_Business. Whenever a new type of business is defined and needs to be added to the table I need to be able to search through the table and find any unused Ids and assign the unused Id to the new type. I know I need to do some kind of looping, but I can't seem to figure out exactly what I need to do. Any help would be appreciated.LPatters
why do you want to do this way? all you need to do is to declare Type_Business_Id as identity column so that it automatically gives you next id value and you dont have to worry too much for its generation. Also i wont personally be too much concerned about itermediate gaps caused by adhoc deletes of records.
My senior DBA prefers to do it this way because we have a lot of unused ids. Instead of assigning new ids he wants to use up all the extras in the table.LPatters |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-03 : 14:15:44
|
but have you thought of overhead in finding out data that was added after a period or during a period using this approach? If ids were auto generated it was more easier to track as they always will be present over continuous range rather than scattered here & there |
 |
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2008-10-04 : 11:16:43
|
quote: Originally posted by lpattersMy senior DBA prefers to do it this way because we have a lot of unused ids. Instead of assigning new ids he wants to use up all the extras in the table.
He is a fool and not worthy of his job title. |
 |
|
|
|
|
|
|