Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2006-01-13 : 08:51:30
|
| MICHAEL writes "I have a table called REQUEST NUMBER, it has one field (req_num) which is numeric. I need a statement that will get me the number that is there then increment it by one and reinsert that number. ThanksMichael" |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-13 : 08:59:33
|
| Declare @id intSelect Isnull(Max(id),0)+1 from yourTableInsert into yourTable (columns) values (@id,....)MadhivananFailing to plan is Planning to fail |
 |
|
|
cshah1
Constraint Violating Yak Guru
347 Posts |
Posted - 2006-01-13 : 09:01:33
|
| DECLARE @newid numericSELECT @newid=MAX(req_num) FROM [REQUEST NUMBER] will give you maximum value and you can increment it by 1SET @newid = @newid +1 and then you can use your insert statement |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-01-13 : 09:06:09
|
| Give examples ofExisting dataNew or changed data |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-13 : 09:13:55
|
quote: Originally posted by cshah1 DECLARE @newid numericSELECT @newid=MAX(req_num) FROM [REQUEST NUMBER] will give you maximum value and you can increment it by 1SET @newid = @newid +1 and then you can use your insert statement
@newid will be null if there are no rows in the table MadhivananFailing to plan is Planning to fail |
 |
|
|
cshah1
Constraint Violating Yak Guru
347 Posts |
Posted - 2006-01-13 : 09:25:09
|
| You are correct and I realized seconds after your post..Namaste! |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2006-01-13 : 09:34:08
|
| Of course all of the above could introduce errors with consistency, or locking if you transactionalise them. You're really re-inventing the IDENTITY field wheel.-------Moo. :) |
 |
|
|
|
|
|