今天遇到一个update更新的错误,用到了几个知识点拿出来备份下
出错语句如下
UPDATE request SET status='COMPLETE'
WHERE REQUEST_ID IN
(
select REQUEST_ID from repair A ,request B
where (status='TERMINATE' or nvl(terminate_flag,0)=1)
AND b.status='UNCOMPLETE'
AND A.TICKET_ID=B.TICKET_ID
)
首先分析下这句,更新一个表, 涉及两个表。两个表都是在不断更新变化的,所以要恢复,两个表都要以之前的一个固定时间为基准。
由于还涉及到条件中两个表的关联。于是我想到用with as 这个东西,简单说明下,with as就是创建临时表 然后再后面使用 一次创建多次使用。
语句如下
create table huif as select * from (with a as (select *
from request as of timestamp to_timestamp('2015/01/04 10:58:00', 'yyyy/mm/dd HH24:mi:ss')) ,
b as (select *
from repair as of timestamp to_timestamp('2015/01/04 10:58:00', 'yyyy/mm/dd HH24:mi:ss'))
select a.request_id,a.status from a
where a.request_id in
(select request_id
from b, a
where (status = 'TERMINATE' OR NVL(terminate_flag, 0) = 1)
and b.status = 'UNCOMPLETE'
AND A.TICKET_ID = B.TICKET_ID))
这里我为了方便 创建了个huif表
现在用huif去更新request表
update request
set status =
(select status
from huif
where request.request_id =
huif.request_id)
where exists (select 1
from huif
where request.request_id =
huif.request_id)
用一个表的信息去更新另一个表的数据还可以用如下的语句
merge into table request t1
using huif t2
on (t1.request_id=t2.request_id)
when matched
then update status=t2.status
阅读(1358) | 评论(0) | 转发(0) |