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
 Indentity Problem

Author  Topic 

JARON
Starting Member

9 Posts

Posted - 2010-01-06 : 07:17:20
Hello all,

I have a column within a table set to integer and identity, the problem goes with whenever i have to delete a row and add up a new one i want the same id number to be shown.
so there would be a gap between the ID numbers, i want them to be in consecutive order, any idea how would it be done?

ex:

ID Name
1 Edy
2 Gary
3 Evelyne
4 Daisy
5 Isac

if i deleted row 4 and 5 from the table then adding a new one will give me

ID Name
1 Edy
2 Gary
3 Evelyne
6 Anthony


i want anthony to have the id number 5.
Any clue?


Thanks in advance.

Ronald

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-06 : 07:18:37
thats how id values are generated. But didnt understand why you need to be concerned about gaps. You can always retrieve data in order you want by ordering on id value
Go to Top of Page

JARON
Starting Member

9 Posts

Posted - 2010-01-06 : 07:22:03
Not worried so much about gaps, but is there any way to have it all consecutive numbers.

Can it be done by c# coding?

Ronald
Go to Top of Page

JARON
Starting Member

9 Posts

Posted - 2010-01-06 : 07:23:26
The thing i am using c#. and in the datagridview i want all the records to have a consecutive id numbers
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-06 : 07:24:08
yup.its possible. but for that you need to manually retrieve maxid value from table and assign maxvalue+1 as next value for id field.
Go to Top of Page

JARON
Starting Member

9 Posts

Posted - 2010-01-06 : 07:27:20
Yeah i guess it is the case.
Thanks in advance
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-06 : 07:29:29
quote:
Originally posted by JARON

Yeah i guess it is the case.
Thanks in advance


but if you want consecutive numbers in grid cant you make use windowing functions like row_number() (i assume you use sql 2005 or above)
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-01-06 : 07:32:33
quote:
Originally posted by JARON

if i deleted row 4 and 5 from the table then adding a new one will give me

ID Name
1 Edy
2 Gary
3 Evelyne
6 Anthony

i want anthony to have the id number 5.


Sure you want Anthony to have id 5 and not 4


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

JARON
Starting Member

9 Posts

Posted - 2010-01-06 : 07:36:12
quote:
Originally posted by visakh16

quote:
Originally posted by JARON

Yeah i guess it is the case.
Thanks in advance


but if you want consecutive numbers in grid cant you make use windowing functions like row_number() (i assume you use sql 2005 or above)



Yeah i guess doing it with sql2005 would be a better choice. Because whithin the code it will have problems later on. so i guess having a good base within the database is better.
Can you help me with the function if possible?
Go to Top of Page

JARON
Starting Member

9 Posts

Posted - 2010-01-06 : 07:37:12
quote:
Originally posted by webfred

quote:
Originally posted by JARON

if i deleted row 4 and 5 from the table then adding a new one will give me

ID Name
1 Edy
2 Gary
3 Evelyne
6 Anthony

i want anthony to have the id number 5.


Sure you want Anthony to have id 5 and not 4


No, you're never too old to Yak'n'Roll if you're too young to die.




Yeah yeah sure it 4, sorry was in a hurry.
I wanted an answer heheh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-06 : 07:37:20
quote:
Originally posted by JARON

quote:
Originally posted by visakh16

quote:
Originally posted by JARON

Yeah i guess it is the case.
Thanks in advance


but if you want consecutive numbers in grid cant you make use windowing functions like row_number() (i assume you use sql 2005 or above)



Yeah i guess doing it with sql2005 would be a better choice. Because whithin the code it will have problems later on. so i guess having a good base within the database is better.
Can you help me with the function if possible?


yup just do like

SELECT ROW_NUMBER() OVER (ORDER BY ID) AS Row_No,ID,Name
FROM Yourtable
Go to Top of Page

JARON
Starting Member

9 Posts

Posted - 2010-01-06 : 07:52:08
quote:
Originally posted by visakh16

quote:
Originally posted by JARON

quote:
Originally posted by visakh16

quote:
Originally posted by JARON

Yeah i guess it is the case.
Thanks in advance


but if you want consecutive numbers in grid cant you make use windowing functions like row_number() (i assume you use sql 2005 or above)



Yeah i guess doing it with sql2005 would be a better choice. Because whithin the code it will have problems later on. so i guess having a good base within the database is better.
Can you help me with the function if possible?


yup just do like

SELECT ROW_NUMBER() OVER (ORDER BY ID) AS Row_No,ID,Name
FROM Yourtable





Oh thank you so much , i really appreciate it. I used it in a stored procedure and use it in my code and it works fine with me.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-01-06 : 07:59:03
But consider that the generated Row_No is only for display purpose and has no direct relation to the data in your table.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

JARON
Starting Member

9 Posts

Posted - 2010-01-06 : 08:38:21
quote:
Originally posted by webfred

But consider that the generated Row_No is only for display purpose and has no direct relation to the data in your table.


No, you're never too old to Yak'n'Roll if you're too young to die.




Yeah i am aware of that. thanks
Go to Top of Page
   

- Advertisement -