Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 06:38 PM
pb648174
 
Posts: n/a
Default Getting list of recently added IDENTITY items

In a multi-user environment, I would like to get a list of Ids
generated, similar to:

declare @LastId int
select @LastId = Max(Id) From TableMania

INSERT INTO TableMania (ColumnA, ColumnB)
SELECT ColumnA, ColumnB From OtherTable Where ColumnC > 15

--get entries just added
SELECT * FROM TableMania WHERE Id > @LastId


The above works fine, except I'm assuming it will not work in a
multi-user environment. Is there any way to get the set of Ids that
were just added in the previous statement (similar to @@IDENTITY)
without doing all of this in a serializable transaction or making a
temp table of every single Id before the insert statement?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 06:39 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Getting list of recently added IDENTITY items

pb648174 (google@webpaul.net) writes:
> In a multi-user environment, I would like to get a list of Ids
> generated, similar to:
>
> declare @LastId int
> select @LastId = Max(Id) From TableMania
>
> INSERT INTO TableMania (ColumnA, ColumnB)
> SELECT ColumnA, ColumnB From OtherTable Where ColumnC > 15
>
> --get entries just added
> SELECT * FROM TableMania WHERE Id > @LastId
>
>
> The above works fine, except I'm assuming it will not work in a
> multi-user environment. Is there any way to get the set of Ids that
> were just added in the previous statement (similar to @@IDENTITY)
> without doing all of this in a serializable transaction or making a
> temp table of every single Id before the insert statement?


Actually, I don't know.

Say that you insert a couple of rows into a table with a column ident
that has the IDENTITY property. @@identity or scope_identity gives
you the highest value for ident for the lnserted rows. But is the
lowest value @@identity - @@rowcount + 1?

I have never seen any documentation that guarantees this to be true.
It is likely to be, but what if you insert 10000 rows, and in the
middle of this another users needs to insert a single row. Will he
steal a value?

One strategy would be to retrieve ident_current() before the insertion (or
MAX(ident), and then after the insertion check that the interval is equal
to @@rowcount, and bail out if it's not.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 06:39 PM
Doug
 
Posts: n/a
Default Re: Getting list of recently added IDENTITY items

sure.
get a timestamp, including milliseconds from the local workstation.
insert that timestamp into a column.
then you can group by timestamp.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 06:41 PM
pb648174
 
Posts: n/a
Default Re: Getting list of recently added IDENTITY items

I guess I will just go with a serializable transaction for now...

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 06:41 PM
David Portas
 
Posts: n/a
Default Re: Getting list of recently added IDENTITY items

pb648174 wrote:
> In a multi-user environment, I would like to get a list of Ids
> generated, similar to:
>
> declare @LastId int
> select @LastId = Max(Id) From TableMania
>
> INSERT INTO TableMania (ColumnA, ColumnB)
> SELECT ColumnA, ColumnB From OtherTable Where ColumnC > 15
>
> --get entries just added
> SELECT * FROM TableMania WHERE Id > @LastId
>
>
> The above works fine, except I'm assuming it will not work in a
> multi-user environment. Is there any way to get the set of Ids that
> were just added in the previous statement (similar to @@IDENTITY)
> without doing all of this in a serializable transaction or making a
> temp table of every single Id before the insert statement?


This is easy to solve provided you have an alternate key. IDENTITY
should not be the only key of a table and this is one example of why -
without an alternate key you have no entity integrity so you cannot
always guarantee reliable results from the data.

Try the following example. Notice that the reason this works is that
the INSERT list always must include the primary key when you are
inserting multiple rows (otherwise there is no key). The only potential
exception is where you assign a default value that forms part of the
key - for example DEFAULT CURRENT_TIMESTAMP. In that case you need to
retrieve the default value before you do the INSERT so that you can use
it in the SELECT.

CREATE TABLE tablemania (id INT IDENTITY PRIMARY KEY, a INT, b INT,
UNIQUE (a,b));
CREATE TABLE othertable (a INT, b INT, c INT, PRIMARY KEY (a,b,c));

INSERT INTO othertable (a,b,c)
SELECT 1,2,16 UNION ALL
SELECT 1,3,16 UNION ALL
SELECT 1,4,16 UNION ALL
SELECT 1,5,16 ;

DECLARE @t TABLE (a INT, b INT, PRIMARY KEY (a,b));

INSERT INTO @t (a, b)
SELECT a, b
FROM othertable
WHERE c > 15 ;

INSERT INTO tablemania (a, b)
SELECT a, b
FROM @t ;

SELECT T.id, T.a, T.b
FROM tablemania AS T
JOIN @t AS O
ON T.a = O.a
AND T.b = O.b ;

In SQL Server 2005 you have a more concise alternative. Use the OUTPUT
option:

INSERT INTO tablemania (a, b)
OUTPUT inserted.id, inserted.a, inserted.b
SELECT a, b
FROM othertable
WHERE c > 15 ;

Hope this helps.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 06:41 PM
David Portas
 
Posts: n/a
Default Re: Getting list of recently added IDENTITY items

Erland Sommarskog wrote:
> pb648174 (google@webpaul.net) writes:
> > In a multi-user environment, I would like to get a list of Ids
> > generated, similar to:
> >
> > declare @LastId int
> > select @LastId = Max(Id) From TableMania
> >
> > INSERT INTO TableMania (ColumnA, ColumnB)
> > SELECT ColumnA, ColumnB From OtherTable Where ColumnC > 15
> >
> > --get entries just added
> > SELECT * FROM TableMania WHERE Id > @LastId
> >
> >
> > The above works fine, except I'm assuming it will not work in a
> > multi-user environment. Is there any way to get the set of Ids that
> > were just added in the previous statement (similar to @@IDENTITY)
> > without doing all of this in a serializable transaction or making a
> > temp table of every single Id before the insert statement?

>
> Actually, I don't know.
>
> Say that you insert a couple of rows into a table with a column ident
> that has the IDENTITY property. @@identity or scope_identity gives
> you the highest value for ident for the lnserted rows. But is the
> lowest value @@identity - @@rowcount + 1?
>
> I have never seen any documentation that guarantees this to be true.
> It is likely to be, but what if you insert 10000 rows, and in the
> middle of this another users needs to insert a single row. Will he
> steal a value?
>


Tibor posted a repro that demonstrates the values are not always
contiguous. Also there is a related problem with IGNORE_DUP_KEY, which
causes gaps if rows are ignored.
http://groups.google.co.uk/group/mic...5cbb8f978decc9


> One strategy would be to retrieve ident_current() before the insertion (or
> MAX(ident), and then after the insertion check that the interval is equal
> to @@rowcount, and bail out if it's not.
>


See my solutions in this thread.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 06:41 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Getting list of recently added IDENTITY items

David Portas (REMOVE_BEFORE_REPLYING_dportas@acm.org) writes:
> Tibor posted a repro that demonstrates the values are not always
> contiguous. Also there is a related problem with IGNORE_DUP_KEY, which
> causes gaps if rows are ignored.
>

http://groups.google.co.uk/group/mic...ogramming/msg/
375cbb8f978decc9

Ah, that was interesting!

> This is easy to solve provided you have an alternate key. IDENTITY
> should not be the only key of a table and this is one example of why -
> without an alternate key you have no entity integrity so you cannot
> always guarantee reliable results from the data.


That is about as useful as saying "this is a good car, but you should
not drive it at night". If databases would only include data that
have natural keys, there wouldn't be much data in them.

What I didn't say in my first post, is that my take on this is usually
to not have IDENTITY on my surrogate key, but instead bounce data over
a temp table with IDENTITY, and then add that to a SELECT MAX(id)
from the target table. This comes with scaling problems obviously, but
that has not been an issue for me, luckily.

> In SQL Server 2005 you have a more concise alternative. Use the OUTPUT
> option:
>
> INSERT INTO tablemania (a, b)
> OUTPUT inserted.id, inserted.a, inserted.b
> SELECT a, b
> FROM othertable
> WHERE c > 15 ;


I will have to admit that I not really seen the point with the OUTPUT
clause for INSERT, but this an excellent use for it!

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-29-2008, 06:41 PM
David Portas
 
Posts: n/a
Default Re: Getting list of recently added IDENTITY items

Erland Sommarskog wrote:
> That is about as useful as saying "this is a good car, but you should
> not drive it at night". If databases would only include data that
> have natural keys, there wouldn't be much data in them.
>


You said "data" so you could be right. If you'd said "information"
you'd be wrong. Here's an analogy. If I have two copies of "Inside SQL
Server" on my bookshelf do I have more information than if I have one
copy of that book? Now if I write 1 on the cover of the first book and
2 on the cover of the second, do I have any more information? So should
I spend money and storage space on two books or one? The smart money is
invested in information not in data.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-29-2008, 06:41 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Getting list of recently added IDENTITY items

David Portas (REMOVE_BEFORE_REPLYING_dportas@acm.org) writes:
> You said "data" so you could be right. If you'd said "information"
> you'd be wrong. Here's an analogy. If I have two copies of "Inside SQL
> Server" on my bookshelf do I have more information than if I have one
> copy of that book? Now if I write 1 on the cover of the first book and
> 2 on the cover of the second, do I have any more information? So should
> I spend money and storage space on two books or one? The smart money is
> invested in information not in data.


Then again, you could have scribbled notes in one of the copies, and
the other could have Kalen's highly valuable autograph.

More importantly, there is data - or information - out there that
users want to - and need to - deal with, despite that we cannot define
a unique key for them. You already know the prime example too well:
customers.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-29-2008, 06:41 PM
David Portas
 
Posts: n/a
Default Re: Getting list of recently added IDENTITY items

Erland Sommarskog wrote:
> David Portas (REMOVE_BEFORE_REPLYING_dportas@acm.org) writes:
> > You said "data" so you could be right. If you'd said "information"
> > you'd be wrong. Here's an analogy. If I have two copies of "Inside SQL
> > Server" on my bookshelf do I have more information than if I have one
> > copy of that book? Now if I write 1 on the cover of the first book and
> > 2 on the cover of the second, do I have any more information? So should
> > I spend money and storage space on two books or one? The smart money is
> > invested in information not in data.

>
> Then again, you could have scribbled notes in one of the copies, and
> the other could have Kalen's highly valuable autograph.


In that case they wouldn't be duplicates any more. To complete the
analogy, you now have a natural key. (Alternatively you might want to
decompose the notes and the signature into separate tables)

> More importantly, there is data - or information - out there that
> users want to - and need to - deal with, despite that we cannot define
> a unique key for them. You already know the prime example too well:
> customers.


Users are concerned with information. Data (how the information is
represented) is primarily the concern of database professionals. As a
database designer you have a choice because the same information can
always be modelled with natural keys or without. You can argue that the
developer may lack the time, the resources or the authority to redesign
his database. He may even be unable to analyse his business problem
well enough to identify a suitable key with a high degree of
confidence. However, those constraints are not problems we can solve in
a newsgroup. They are project management problems rather than technical
ones.

The technical solution to the OP's problem is simple: elminate
redundancy. It is always possible to eliminate redundancy as a
consequence of the simple fact that duplicate rows cannot contain more
information than a single row. That applies equally whether the row
represents customers or books or anything else. I suggest we let the OP
to decide if he has the will or the resources to implement the solution
in his case.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:13 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532