Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. ALTER DATABASE MyDatabase SET ALLOW_SNAPSHOT_ISOLATION ON;
  2.  
  3. IF object_id('dbo.TEST_InventoryActivity') IS NOT NULL
  4. DROP TABLE dbo.TEST_InventoryActivity
  5. GO
  6.  
  7. CREATE TABLE dbo.TEST_InventoryActivity
  8. ( activityID int not null primary key identity
  9. , itemID int not null
  10. , inOrOut char(1) not null
  11. , quantity int not null
  12. , modBy varchar(128) not null
  13. , modDate datetime not null default getdate()
  14. )
  15. go
  16.  
  17. INSERT INTO TEST_InventoryActivity(itemID, inOrOut, quantity, modBy)
  18. VALUES (1,'I',100, 'setups')
  19. ;
  20. --show all records
  21. SELECT i.*
  22. FROM TEST_InventoryActivity i
  23. ;
  24. --show inventory balances
  25. SELECT i.itemID, inventoryBalance = sum(i.quantity)
  26. FROM TEST_InventoryActivity i
  27. GROUP BY i.itemID
  28. ;
  29.  
  30. SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
  31.  
  32. BEGIN TRAN;
  33.  
  34. DECLARE @itemID int, @quantity int;
  35. set @itemID = 1;
  36. set @quantity = -75;
  37.  
  38. insert into TEST_InventoryActivity(itemID, inOrOut, quantity, modBy)
  39. values(@itemID,'O', @quantity, 'test 1') --use Item 1
  40. ;
  41.  
  42. IF EXISTS(
  43. SELECT i.itemID, sum(i.quantity)
  44. FROM TEST_InventoryActivity i
  45. WHERE i.itemID = @itemID
  46. GROUP BY i.itemID
  47. HAVING sum(i.quantity) < 0
  48. )
  49. BEGIN
  50. ROLLBACK;
  51. RAISERROR(N'Not enough remaining inventory', 16, 1);
  52. RETURN;
  53. END
  54. ;
  55.  
  56. SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
  57.  
  58. BEGIN TRAN;
  59.  
  60. DECLARE @itemID int, @quantity int;
  61. set @itemID = 1;
  62. set @quantity = -50;
  63.  
  64. insert into TEST_InventoryActivity(itemID, inOrOut, quantity, modBy)
  65. values(@itemID,'O', @quantity, 'test 2') --use Item 1
  66. ;
  67.  
  68. IF EXISTS(
  69. SELECT i.itemID, sum(i.quantity)
  70. FROM TEST_InventoryActivity i
  71. WHERE i.itemID = @itemID
  72. GROUP BY i.itemID
  73. HAVING sum(i.quantity) < 0
  74. )
  75. BEGIN
  76. ROLLBACK;
  77. RAISERROR(N'Not enough remaining inventory', 16, 1);
  78. RETURN;
  79. END
  80. ;
  81.  
  82. COMMIT;
  83.  
  84. COMMIT;
  85.  
  86. --show all records
  87. SELECT i.*
  88. FROM TEST_InventoryActivity i
  89. ;
  90. --show inventory balances
  91. SELECT i.itemID, inventoryBalance = sum(i.quantity)
  92. FROM TEST_InventoryActivity i
  93. GROUP BY i.itemID
  94.  
  95. IF EXISTS(SELECT i.itemID,
  96. sum(i.quantity)
  97. FROM TEST_InventoryActivity i With (READCOMMITTED)
  98. WHERE i.itemID = @itemID
  99. GROUP BY i.itemID
  100. HAVING sum(i.quantity) < 0)
  101. BEGIN
  102. ROLLBACK;
  103. RAISERROR(N'Not enough remaining inventory', 16, 1);
  104. RETURN;
  105. END
  106.  
  107. SELECT CASE transaction_isolation_level
  108. WHEN 0 THEN 'Unspecified'
  109. WHEN 1 THEN 'ReadUncommitted'
  110. WHEN 2 THEN 'ReadCommitted'
  111. WHEN 3 THEN 'Repeatable'
  112. WHEN 4 THEN 'Serializable'
  113. WHEN 5 THEN 'Snapshot' END AS TRANSACTION_ISOLATION_LEVEL
  114. FROM sys.dm_exec_sessions
  115. WHERE session_id = @@SPID
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement