|
-
Aug 21st, 2006, 01:32 PM
#1
Thread Starter
Hyperactive Member
increment within update
how do you update a column in database and then increase the number by 1
like this
S.id Name
3 n
5 a
6 b
now i want to make an update statment that would make it become
S.id Name
1 n
2 a
3 b
i have
update tablename set columname = columname + 1;
but this only make it
S.id Name
4 n
6 a
7 b
plz help
-
Aug 21st, 2006, 01:38 PM
#2
Addicted Member
Re: increment within update
What you are describing sounds like expected behaviour but I'm not undestanding the relationshp between :
1 n
2 a
3 b
3 n
5 a
6 b
Also from your table description it would appear these are ID's which typically you wouldn't update manually.
-
Aug 21st, 2006, 01:45 PM
#3
Thread Starter
Hyperactive Member
Re: increment within update
assume that sid is not primary key but the other one is. so that should not get update automatically
-
Aug 21st, 2006, 02:23 PM
#4
Addicted Member
Re: increment within update
okay but how do you get from
3 to 1
5 to 2
6 to 3
I'm not seeing a pattern or relationship.
If you could clarify this it shouldn't be to hard.
-
Aug 21st, 2006, 02:25 PM
#5
Thread Starter
Hyperactive Member
Re: increment within update
those 3 6 5 number are just random number that i put in there. Then now i come back and say. ok i dont want those number i want it to be in an order so i want to change it to 1 2 3
-
Aug 21st, 2006, 02:35 PM
#6
Re: increment within update
This might work for you
Code:
Update TableName
Set Id = (Select Count(*) From TableName T2 Where T2.Id <= T1.Id )
From TableName T1
It assumes the original order of the Id column should be maintained and the Id column is unique, ie
3 -> 1
5 -> 2
6 -> 3
11 -> 6
8 -> 5
7 -> 4
Last edited by brucevde; Aug 21st, 2006 at 02:40 PM.
-
Aug 21st, 2006, 02:35 PM
#7
Addicted Member
Re: increment within update
okay well if there is no direct relationship between the current value and it's new value you won't be able to do it that way.
You are probably going to have to update individual updates unless there is some other relationship? even then it could be come complicated through use of sub queries etc.
What database are you using?
-
Aug 21st, 2006, 09:48 PM
#8
Thread Starter
Hyperactive Member
Re: increment within update
ms sql
but i took the advice from making the column automatic number ty
-
Aug 22nd, 2006, 10:33 AM
#9
Thread Starter
Hyperactive Member
Re: increment within update
sorry bruce i forgot to read your post. ty though.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|