我目前正在开发一个项目,其中我有一个生产Oracle数据库的副本,其中包含一些我想用一组测试值替换的生产值(假设我有比生产记录更多的生产记录而且我会有重复的。)
以下是我要做的一个示例:
任何建议都将不胜感激。
答案 0 :(得分:2)
一种方法是按行rowid
对行进行编号,然后使用模运算将原始数据与测试数据表进行匹配,如下所示:
merge into customer c
using (
with cte_customer as (
select rowid xrowid, mod(row_number() over (order by rowid)-1,(select count(*) from test_data))+1 rownumber
from customer
order by rowid
), cte_testdata as (
select row_number() over (order by rowid) rownumber, first_name, last_name, email
from test_data
order by rowid
)
select c.xrowid, t.last_name, t.first_name, t.email
from cte_customer c
left outer join cte_testdata t on (t.rownumber = c.rownumber)
order by c.xrowid
) u
on (c.rowid = u.xrowid)
when matched then update set
c.last_name = u.last_name,
c.first_name = u.first_name,
c.email = u.email