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 |
|
jgandara
Starting Member
18 Posts |
Posted - 2006-06-12 : 12:50:48
|
| I have some tables with numeric non-incremental keys. What is the best way to add a new record?I get the MAX number +1 , assign it to a variable and then do the insert. |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-06-12 : 12:56:42
|
yes if you're inserting just one row at the time.why don't you use an Identity column?Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
jgandara
Starting Member
18 Posts |
Posted - 2006-06-12 : 13:16:14
|
| It's a web app, so we can have concurrent users. I want to see if I can do it with just a SQL statement and avoid possible key violations |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-06-12 : 14:04:07
|
well then yes to your 1st question. Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2006-06-12 : 17:19:51
|
quote: Originally posted by spirit1 well then yes to your 1st question.
1st question?Is there a 2nd?rockmoose |
 |
|
|
DustinMichaels
Constraint Violating Yak Guru
464 Posts |
Posted - 2006-06-12 : 17:41:09
|
| You can use an identity key even if you have concurrent users to your system.The approach your using Max + 1 has a greater chance to result in key violations. If two ppl are getting the Max + 1 value at the same time before one of them is able to insert their record then you should get a key violation. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-06-13 : 04:17:46
|
well as i understood it he wants to increment only one value - how many users are on the site.i could be wrong... Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2006-06-13 : 04:28:17
|
Always use identity where possible.Max + 1 gets really ugly and definately does cause key violations.I just inheritted a system using the max + 1 approach - giving me nightmares - I have changed it to identity and is working like a charm.Duane. |
 |
|
|
|
|
|