Oracle 11g中的正确更新语句是什么

时间:2013-03-27 18:20:15

标签: oracle oracle11g

我的原始更新语句在Sybase中仍然运行良好(见下文),但我们正在迁移到Oracle 11g,我必须将此更新语句转换为Oracle Update。 我尝试了这么多版本并且不断出错,我想我还没有弄清楚Oracle Update语句是如何工作的。有人可以帮忙吗?

我原来的好的更新声明(在Sybase中):

UPDATE Valid
    SET A.status = 'X',
        A.reason = 'Missing'
  FROM Valid A, Valid B
  WHERE A.id_number = B.id_number
  AND A.session_id = 69
  AND A.userid = 'BS'
  AND A.status = 'A' 
  AND isnull(B.street1, ' ') = ' '

以下是我的Oracle版本,无论我做什么都无法正常工作:

MERGE
  INTO um_valid Target
  USING (select * from um_valid) SOURCE
  ON (t1.id_number = t2.id_number)                
  WHEN MATCHED THEN
  UPDATE
   SET status = 'X',
       reason = 'Missing (street 1)'
  WHERE Target.session_id = 69
  AND Target.userid = 'BS'
  AND Target.status = 'A' 
  AND NVL(SOURCE.street1, ' ') = ' ') 

我得到的错误: ORA-00933: SQL command not properly ended

3 个答案:

答案 0 :(得分:0)

最后的WHERE子句不合适。也许你需要这个:

MERGE
   INTO um_valid Target
   USING (
      select distinct id_number 
      from um_valid 
      where street1 is null
   ) SOURCE
   ON (
      Target.id_number = SOURCE.id_number
      AND Target.session_id = 69
      AND Target.userid = 'BS'
      AND Target.status = 'A' 
   )                
   WHEN MATCHED THEN UPDATE SET 
      status = 'X',
      reason = 'Missing (street 1)'

答案 1 :(得分:0)

您可以使用普通的更新语句。合并适用于批量操作。 如果您需要在存在时更新某些内容或在不存在时进行插入,则可以使用合并语句。 因此,对于您的示例,请尝试使用以下内容:

UPDATE Valid
SET status = 'X',
    reason = 'Missing'
WHERE session_id = 69
AND userid = 'BS'
AND status = 'A' 
AND street1 is null

上述语句将更新表“Valid”中的所有行,其中session_id等于69,userid等于BS,status等于A,street1为空(null)。

我认为这就是你所需要的,如果不是这样,请告诉我为什么你加入有效的有效。我会调整我的更新。

答案 2 :(得分:0)

应该这样做:

UPDATE Valid a
    SET status = 'X',
        reason = 'Missing'
  WHERE A.session_id = 69
  AND A.userid = 'BS'
  AND A.status = 'A' 
  AND exists (select null
              from   valid b
              where  a.id_number = b.id_number
                 AND coalesce(B.street1, ' ') = ' ')