1

下面是用 Oracle 11g 编写的脚本:

 MERGE INTO tblbio t
      USING (SELECT e.id, tblduplicate.cpid, e.bdt,e.LN, e.FN               
          FROM tblduplicate, entities where
          trim(e.id) = trim(tblduplicate.id)) source
      ON (t.cpid = source.cpid and trim(t.bdt) = trim(source.bdt))
WHEN MATCHED
THEN
UPDATE SET t.id = source.id, t.stat = '4'
WHERE  t.cmp = 'HHCC'
  AND t.thn = '2013'
  AND trim(lower(source.LN)) = trim(lower(t.LN))
  AND trim(lower(source.FN)) = trim(lower(t.FN))
  AND nvl(trim(t.bdt), ' ') <> ' '
  AND t.bdt <> '00000000'
  AND nvl(trim(source.bdt), ' ') <> ' '
  and source.bdt <> '00000000'
  AND t.stat <> '4'

由于我的数据完整性问题,此脚本偶尔会生成超过 1 条记录。此脚本将在其中生成错误。我想在运行此脚本之前创建验证。当我的验证生成超过 1 条记录时,我会停止运行此脚本。我怎样才能做到这一点?

我尝试编写以下内容以进行验证以捕获记录数,但 Oracle 只是不喜欢它。

 select* from tblbio t <-----
      USING (SELECT e.id, tblduplicate.cpid, e.bdt,e.LN, e.FN               
          FROM tblduplicate, entities where
          trim(e.id) = trim(tblduplicate.id)) source
      ON (t.cpid = source.cpid and trim(t.bdt) = trim(source.bdt))
WHEN MATCHED
THEN
select * from tblbio t <-----
WHERE  t.cmp = 'HHCC'
  AND t.thn = '2013'
  AND trim(lower(source.LN)) = trim(lower(t.LN))
  AND trim(lower(source.FN)) = trim(lower(t.FN))
  AND nvl(trim(t.bdt), ' ') <> ' '
  AND t.bdt <> '00000000'
  AND nvl(trim(source.bdt), ' ') <> ' '
  and source.bdt <> '00000000'
  AND t.stat <> '4'
4

1 回答 1

0

将原始MERGE查询转换为 select from tbiblio

  • MERGE INTO ... USING-->select from ... join
  • WHEN MATCHED THEN UPDATE SET ... WHERE-->WHERE

最终查询:

select * from tblbio t 
      JOIN (SELECT e.id, tblduplicate.cpid, e.bdt,e.LN, e.FN               
          FROM tblduplicate, entities where
          trim(e.id) = trim(tblduplicate.id)) source
      ON (t.cpid = source.cpid and trim(t.bdt) = trim(source.bdt))
WHERE  t.cmp = 'HHCC'
  AND t.thn = '2013'
  AND trim(lower(source.LN)) = trim(lower(t.LN))
  AND trim(lower(source.FN)) = trim(lower(t.FN))
  AND nvl(trim(t.bdt), ' ') <> ' '
  AND t.bdt <> '00000000'
  AND nvl(trim(source.bdt), ' ') <> ' '
  and source.bdt <> '00000000'
  AND t.stat <> '4'
于 2013-08-24T04:32:36.140 回答