从select到更新Oracle Sql

时间:2012-03-21 10:58:43

标签: sql oracle

我有这个(工作)选择陈述:

select * from memberships where MEMBERSHIP_TYPE = 'ZZZ' and inactive_date is null
 and name_id in (select name_id from name where history_yn = 'N')

但现在我想改变它,因此它将是一个更新声明,:

update name
set history_yn = 'Y'  
IN (select * from memberships where MEMBERSHIP_TYPE = 'ZZZ' and inactive_date is null)

但我收到ora-00933错误。你能告诉我这个吗?

2 个答案:

答案 0 :(得分:2)

这样的事情:

update name set history_yn = 'Y' 
where name_id IN (select name_id 
                  from memberships 
                  where MEMBERSHIP_TYPE = 'ZZZ' and inactive_date is null)
and history_yn = 'N'

答案 1 :(得分:1)

你需要在IN之前有一个WHERE子句。查询可以重写为

update name set history_yn = 'Y'
WHERE name_id
IN (select name_id from memberships where MEMBERSHIP_TYPE = 'ZZZ' and inactive_date is null)

更有效的方法可能是使用EXISTS而不是IN子句,如下所示

update name n set history_yn = 'Y'
WHERE EXISTS
(select name_id from memberships where MEMBERSHIP_TYPE = 'ZZZ' 
   and inactive_date is null
   and name_id = n.name_id)