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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Sort Automatic ID

Author  Topic 

Exir
Posting Yak Master

151 Posts

Posted - 2013-05-22 : 04:58:36
Hi
I have a table in sql which its ID fills automatically by SQL identity
the problem is when i delete a row , the IDs will be missed up
for example IDs will be like this 1-2-5-6-9-...
How can i sort the IDs after deleting and when i add new row the id start from the last available ID in the table ?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-22 : 05:11:55
no need to actually do this. You can always retrieve this in same sequence by means of ORDER BY IDcol
In case you want them to be continuosly numbered you can use ROW_NUMBER() function
You can reclaim identity gaps but question is whether its really worth doing it for your case

http://weblogs.sqlteam.com/peterl/archive/2009/03/10/How-to-efficiently-reuse-gaps-in-identity-column.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-22 : 05:12:58
Whenever you delete data(record) there can be gaps between identity values...
But what is the problem with sorting... You can use ORDER BY clause for sorting records


--
Chandu
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2013-05-22 : 05:20:12
quote:
Originally posted by bandi

Whenever you delete data(record) there can be gaps between identity values...
But what is the problem with sorting... You can use ORDER BY clause for sorting records


--
Chandu



I think i couldnt Express my meant, i mean "filling those gaps" by "order" them.
because when user view or print the informations, it is not good that row number be like 1-2-5-6-9-...

Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2013-05-22 : 05:30:24
quote:
Originally posted by visakh16

no need to actually do this. You can always retrieve this in same sequence by means of ORDER BY IDcol
In case you want them to be continuosly numbered you can use ROW_NUMBER() function
You can reclaim identity gaps but question is whether its really worth doing it for your case

http://weblogs.sqlteam.com/peterl/archive/2009/03/10/How-to-efficiently-reuse-gaps-in-identity-column.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs




Thnak you.
Can i replace my main IDs(which is automatically fills by sql ) with ROW_NUMBER() Numbers when some rows delete to keep the sequence ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-22 : 05:34:38
You can. But again for what?
Its not worth doing it unless you reach a stage where your identity values in a table are getting exhausted.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-22 : 05:35:40
for displaying to user as sequence you dont need to do the change in table. you just need to add the row_number based logic in the select which retrives records from the table

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2013-05-22 : 06:46:47
I changed my method and turn off the automatic numbering.
now please tell me how can i set number for the rows which their IDs are 0?
I wrote this but it is wrong


UPDATE [project1db].[dbo].[Sell]
SET [IDobj] =(ROW_NUMBER() over (ORDER BY ID))
where ID=0

GO
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-22 : 06:55:30
--try this
;With cte AS(SELECT IDobj, ROW_NUMBER() over (ORDER BY ID) RN FROM [project1db].[dbo].[Sell])
UPDATE cte
SET IDobj= RN;
GO
SELECT * FROM [project1db].[dbo].[Sell]

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-22 : 06:56:12
quote:
Originally posted by Exir

I changed my method and turn off the automatic numbering.
now please tell me how can i set number for the rows which their IDs are 0?
I wrote this but it is wrong


UPDATE [project1db].[dbo].[Sell]
SET [IDobj] =(ROW_NUMBER() over (ORDER BY ID))
where ID=0

GO


if ID is 0 on what sequence you want ID values to be numbered?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-22 : 06:57:44
quote:
Originally posted by bandi

--try this
;With cte AS(SELECT IDobj, ROW_NUMBER() over (ORDER BY ID) RN FROM [project1db].[dbo].[Sell])
UPDATE cte
SET IDobj= RN;
GO
SELECT * FROM [project1db].[dbo].[Sell]

--
Chandu


No need of cte you can do it inline
However as per OP all ID have 0 value so not sure whether thats field on which you should be sorting on!

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2013-05-22 : 06:59:04
yes you are true, how can i write it to set 1-2-3-4 for IDobj of the rows which its ID=0 ?
sorry I havent use ROW_NUMBER() yet and i dont know how to write it
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-22 : 07:02:24
Is there any unique column (such as modified_date/ datetime columns) in that table?

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-22 : 07:02:25
quote:
Originally posted by Exir

yes you are true, how can i write it to set 1-2-3-4 for IDobj of the rows which its ID=0 ?
sorry I havent use ROW_NUMBER() yet and i dont know how to write it


Thats what you should answer
You should determine based on your business rules a column or set of columns based on which you want records to be ordered and numbered.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2013-05-22 : 07:06:45
I dont want to order them base on any column , i just want to set number 1-2-3-4 in IDobj for the rows where their ID are 0
Is it possible ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-22 : 07:12:14
quote:
Originally posted by Exir

I dont want to order them base on any column , i just want to set number 1-2-3-4 in IDobj for the rows where their ID are 0
Is it possible ?


yep it is
But in that case records will be chosen on random ordered and then numbered 1-2-3-4 etc


UPDATE t
SET IDobj = Rn
FROM (SELECT IDobj,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS Rn
FROM [project1db].[dbo].[Sell]
WHERE ID = 0
)t


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2013-05-22 : 07:34:01
thank you verry much visakh , you are perfect, you always solve all problems
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-23 : 00:56:20
welcome
glad that i could be of help

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

lilyyang
Starting Member

3 Posts

Posted - 2013-07-08 : 03:46:05
unspammed
Go to Top of Page
   

- Advertisement -