declare @diff int
, @count int
select @diff = cash - @price
from users
where id = @uid
select @count = count
from shop
where item = @item
if (@count>0) and (@diff > 0)
begin
begin transaction
update users
set cash=cash-@price
where id=@uid
if exists (select top 1 from items where uid = @uid and item=@item)
update items
set count=count+1
where uid=@uid and item=@item;
else
insert into items
select @uid, @item, 1;
update shop
set count=count-1
where item=@item;
if @@error != 0
rollback
commit
end