I would like to update a number field in a table by incrementing the numbers by 1. Kind of like an auto number.
I have been trying to use an update statement with count + 1 but cannot get it to work. I would like it to keep going until it gets to the last entry so will need some type of loop. If possible, I would also like the numbering to start at the largest number in the table.
so you want to update all current numeric values with new ones starting from current max(value) + 1? Whats the purpose of such an update? or are you asking about auto numbering numeric field upon insertion? In which why is the field declared numeric and also why havent you declared it as identity column?
I baically would like an autonumber field that I calculate. I have populated the field with a place holder (1000001) and I would like to add 1 incrementally down (1000001, 1000002, etc). I haven't worked with SQL in a while so I may be going about this wrong. I hope this makes my question more clear.
I was hoping to find a way to update the field I already have in my table. I was thinking I could use some sort of loop with an update statement. Is there a way to do this?
Thanks for the sample scripts. I did try and execute one that looked appropriate for what I was trying to do. Even though I used the column name I was trying to update, it tried to fill in the primary key field which I do not want to change.
I found a solution though for anyone else who may want to do this:
declare <name of cursor> cursor for select <id field> from <table name>
open <name of cursor> fetch from <name of cursor>
declare @counter int set @counter = <1 or starting number>
while @@fetch_status = 0 begin update <table name> set <id field> = @counter set @counter = @counter + 1 fetch next from <name of cursor> end
Thanks for the sample scripts. I did try and execute one that looked appropriate for what I was trying to do. Even though I used the column name I was trying to update, it tried to fill in the primary key field which I do not want to change.
I found a solution though for anyone else who may want to do this:
declare <name of cursor> cursor for select <id field> from <table name>
open <name of cursor> fetch from <name of cursor>
declare @counter int set @counter = <1 or starting number>
while @@fetch_status = 0 begin update <table name> set <id field> = @counter set @counter = @counter + 1 fetch next from <name of cursor> end
Stacy
still cant understand purpose for this.You could have simply added an identity column as suggested by many of us to get this autoincremented value in your table.
Thanks for the sample scripts. I did try and execute one that looked appropriate for what I was trying to do. Even though I used the column name I was trying to update, it tried to fill in the primary key field which I do not want to change.
I found a solution though for anyone else who may want to do this:
declare <name of cursor> cursor for select <id field> from <table name>
open <name of cursor> fetch from <name of cursor>
declare @counter int set @counter = <1 or starting number>
while @@fetch_status = 0 begin update <table name> set <id field> = @counter set @counter = @counter + 1 fetch next from <name of cursor> end
Stacy
still cant understand purpose for this.You could have simply added an identity column as suggested by many of us to get this autoincremented value in your table.