Results 1 to 9 of 9

Thread: increment within update

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    260

    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

  2. #2
    Addicted Member
    Join Date
    Mar 2006
    Location
    Manchester, England, UK
    Posts
    247

    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.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    260

    Re: increment within update

    assume that sid is not primary key but the other one is. so that should not get update automatically

  4. #4
    Addicted Member
    Join Date
    Mar 2006
    Location
    Manchester, England, UK
    Posts
    247

    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.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    260

    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

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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.

  7. #7
    Addicted Member
    Join Date
    Mar 2006
    Location
    Manchester, England, UK
    Posts
    247

    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?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    260

    Re: increment within update

    ms sql
    but i took the advice from making the column automatic number ty

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    260

    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
  •  



Click Here to Expand Forum to Full Width