Author |
Topic |
Exir
Posting Yak Master
151 Posts |
Posted - 2013-05-22 : 04:58:36
|
HiI have a table in sql which its ID fills automatically by SQL identitythe problem is when i delete a row , the IDs will be missed upfor 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 |
|
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 |
|
|
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-... |
|
|
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() functionYou can reclaim identity gaps but question is whether its really worth doing it for your casehttp://weblogs.sqlteam.com/peterl/archive/2009/03/10/How-to-efficiently-reuse-gaps-in-identity-column.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://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 ? |
|
|
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 MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
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 MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
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 wrongUPDATE [project1db].[dbo].[Sell] SET [IDobj] =(ROW_NUMBER() over (ORDER BY ID)) where ID=0GO |
|
|
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 cteSET IDobj= RN;GOSELECT * FROM [project1db].[dbo].[Sell]--Chandu |
|
|
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 wrongUPDATE [project1db].[dbo].[Sell] SET [IDobj] =(ROW_NUMBER() over (ORDER BY ID)) where ID=0GO
if ID is 0 on what sequence you want ID values to be numbered?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
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 cteSET IDobj= RN;GOSELECT * FROM [project1db].[dbo].[Sell]--Chandu
No need of cte you can do it inlineHowever as per OP all ID have 0 value so not sure whether thats field on which you should be sorting on!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
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 |
|
|
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 |
|
|
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 MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
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 0Is it possible ? |
|
|
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 0Is it possible ?
yep it isBut in that case records will be chosen on random ordered and then numbered 1-2-3-4 etc UPDATE tSET IDobj = RnFROM (SELECT IDobj,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS Rn FROM [project1db].[dbo].[Sell] WHERE ID = 0 )t ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
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 |
|
|
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 MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
lilyyang
Starting Member
3 Posts |
Posted - 2013-07-08 : 03:46:05
|
unspammed |
|
|
|