这个简单的事务该怎样写

cyberkit 2008-06-23 11:08:36
执行一个出仓的操作该如何写?

平时我是这么写的,首先看看剩余数量

select shengyu into temp from table1 where mid=xxx;

如果大于指定数量那么执行出仓

if temp >= 100 then
update table1 set shengyu=shengyu-100 where mid=xxx;
commit;
end if;

但是如果这些语句并发执行的话,应该是有可能出错的,当剩余100时,两个事务同时去select,那么temp都大于等于100,于是都减去100,那剩余就小于0了,与事实不符。我的想法是,当我select时就应该将记录锁住,不让其他的事务去读该记录,只有当该事务完毕之后才释放锁。
但不知道怎么写啊,请各位指教。
...全文
75 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenqingyu 2008-06-23
  • 打赏
  • 举报
回复
试试

select shengyu into temp from table1 where mid=xxx for update; 


chensi05 2008-06-23
  • 打赏
  • 举报
回复
select * from table where *** for update
wffffc 2008-06-23
  • 打赏
  • 举报
回复
1
update前面加一个:
select * from table1 where mid=xxx for update;
2
或者
update table1 set shengyu=shengyu-100 where mid=xxx and shenyu=temp; 这样改一下也以
flg_inwind 2008-06-23
  • 打赏
  • 举报
回复
for update 即可

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧