I am trying to achieve something like the following sql statements in a transaction. Can anyone advice how I can accomplish this? most importantly, there shouldnt be any dirty read or write. Thanks!
UPDATE items SET assigned = 1
WHERE
SELECT TOP 1 @item_no = item_no
FROM items
WHERE item_code = @item_code
AND store_id = @store_id
UPDATE parts SET issued = 1
WHERE
SELECT TOP 1 @part_no = part_no
FROM parts
WHERE part_id = part_id
INSERT INTO issued_hardware (@item_no, @part_no, DateTime.now, @username);
As other have mentioned you use a transaction and its isolation state to control Dirty Reads. I recommend against this since it means you can read an uncommitted transaction that may fail which can cause race conditions that are difficult to trace down.
Also since you’re doing more that one data modification you should probably explicitly roll back your transaction. Finally your SQL needed to fixed. Taking advantage of the output clause is probably a good idea.