select for update的问题

hgamezoom 2012-06-29 06:44:00
我的语句如下,用select for update获取行锁用于后续操作。

declare lastid decimal;
begin

SELECT step_id into lastid FROM table where step_id=1 for update;
commit;
end;


正常情况没有问题,但如果where条件不满足,那么会抛no data found的异常。

如果改成下面这样,看似可以执行,实际上并没有锁定table表,所以也是不行的。
select nvl((SELECT step_id FROM Table where step_id=1),null) into lastid from dual for update;


不知道应该如何修改此语句?


...全文
209 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
小德 2012-06-29
  • 打赏
  • 举报
回复
declare 
lastid decimal;
begin
SELECT step_id into lastid FROM table where step_id=1 for update;
EXCEPTION WHEN No_Data_Found THEN
NULL;
commit;
end;

话说,where条件查不到资料的时候,你也要锁表吗?..
人生无悔 2012-06-29
  • 打赏
  • 举报
回复

declare lastid decimal;
row number:=0;
begin
select count(1) into row from mytable where step_id=1;
if row<>0 then
SELECT step_id into lastid FROM mytable where step_id=1 for update;
commit;
end if;
end;
/

17,078

社区成员

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

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