SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Sort Automatic ID
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Exir
Posting Yak Master

151 Posts

Posted - 05/22/2013 :  04:58:36  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 05/22/2013 :  05:11:55  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 05/22/2013 :  05:12:58  Show Profile  Reply with Quote
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

Edited by - bandi on 05/22/2013 05:13:38
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 05/22/2013 :  05:20:12  Show Profile  Reply with Quote
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 - 05/22/2013 :  05:30:24  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 05/22/2013 :  05:34:38  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 05/22/2013 :  05:35:40  Show Profile  Reply with Quote
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 - 05/22/2013 :  06:46:47  Show Profile  Reply with Quote
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

Edited by - Exir on 05/22/2013 06:48:16
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 05/22/2013 :  06:55:30  Show Profile  Reply with Quote
--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

Edited by - bandi on 05/22/2013 06:56:02
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 05/22/2013 :  06:56:12  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 05/22/2013 :  06:57:44  Show Profile  Reply with Quote
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 - 05/22/2013 :  06:59:04  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 05/22/2013 :  07:02:24  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 05/22/2013 :  07:02:25  Show Profile  Reply with Quote
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 - 05/22/2013 :  07:06:45  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 05/22/2013 :  07:12:14  Show Profile  Reply with Quote
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 - 05/22/2013 :  07:34:01  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 05/23/2013 :  00:56:20  Show Profile  Reply with Quote
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 - 07/08/2013 :  03:46:05  Show Profile  Reply with Quote
unspammed
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000