以下是用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条以上的记录。其中此脚本将生成错误。 我想在运行此脚本之前创建验证。当我的验证生成多个记录时,我会停止运行此脚本。我怎么能这样做?
我尝试编写以下内容进行验证以捕获记录计数,但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'
答案 0 :(得分:0)
将原始MERGE
查询转换为从tbiblio
中选择:
MERGE INTO ... USING
- &gt; select from ... join
WHEN MATCHED THEN UPDATE SET ... WHERE
- &gt; 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'