Advertisement
Guest User

Untitled

a guest
Mar 10th, 2012
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.58 KB | None | 0 0
  1. USE TestDatabase
  2.  
  3. -- Create this table once
  4. -- create table ConcurrencyTest (id int primary key, value int)
  5. -- truncate table ConcurrencyTest
  6.  
  7. -- Run this in two windows
  8. SET xact_abort ON
  9. while 1=1
  10.     BEGIN
  11.    
  12.     DECLARE @newid INT
  13.    
  14.     SELECT  @newid = isnull(MAX(id),0) + 1
  15.     FROM    ConcurrencyTest
  16.  
  17.    
  18.     MERGE INTO ConcurrencyTest ct
  19.     USING (
  20.        SELECT @newid AS new_id
  21.     ) t ON ct.id = t.new_id
  22.     WHEN NOT matched THEN
  23.       INSERT (id) VALUES (t.new_id);
  24.  
  25.       END
  26.  
  27. -- You should get a primary key violation in one of the window
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement