0

我原来的更新语句在 Sybase 中仍然运行良好(见下文),但我们正在迁移到 Oracle 11g,我必须将此更新语句转换为 Oracle 更新。我尝试了这么多版本并不断出错,我想我还没有了解 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

4

3 回答 3

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, ' ') = ' ')
于 2013-03-27T23:31:26.627 回答
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)'
于 2013-03-27T18:33:59.077 回答
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)。

我认为这就是您所需要的,如果不是这种情况,请让我知道您为什么加入 valid 和 valid。我会调整我的更新。

于 2013-03-27T21:57:43.410 回答