// Comparing data of tables in different databases
use role accountadmin;
create or replace database exampledb1;
use database exampledb1;
create or replace table employee
( id int,
FirstName varchar(20),
LastName varchar(20),
email varchar(50)
);
insert into employee values
(1,'Sarah', 'Janes','sarah.janes@hotmail.com'),
(2,'Richard', 'Hays','richard.hays@gmail.com'),
(3,'Maria', 'Khan','maria.khan@wow.com');
create or replace database exampledb2;
use database exampledb2;
create or replace table employee
( id int,
FirstName varchar(20),
LastName varchar(20),
email varchar(50)
);
insert into employee values
(1,'Sarah', 'Janes','sarah.janes@hotmail.com'),
(2,'Richard1', 'Hays','richard.hays@gmail.com'),
(3,'Maria', 'Khan','maria.khan@wow.com');
// Check the variance by comparing hash of each row
// You should have correct access to both databases, scheam and underlying tables
with qadata as
(
select id,hash(FirstName||LastName||email) as r1, FirstName, LastName,Email
from exampledb2.public.employee as r1
),
proddata as
(
select id,hash(t.FirstName||t.LastName||t.email) as r1, FirstName, LastName,Email
from exampledb1.public.employee as t
)
select prod.id,
case when prod.r1 = qa.r1 then 'Matched' else 'Variance' end as CheckStatus,
prod.FirstName,
prod.LastName,
prod.Email,
qa.FirstName,
qa.LastName,
qa.Email
from qadata qa
inner join proddata prod on qa.id=prod.id;
1条答案
按热度按时间cmssoen21#
根据您的需求,有许多方法可以进行验证。假设您想验证两个不同数据库中employee表的列数据,您可以创建一个sql脚本并运行它来验证是否存在任何差异。下面是一个示例,您可以进一步扩展它,或者在它的基础上构建您的用例。
下面是输出列checkstatus,它将标识行数据是否匹配,或者是否存在如第2行所示的差异。比较结果