我有两个表p1和p2,下面有几列。
P1:
p_id P_revid item_type status rev_name
----- ------------- ---------------- ---------- --------------
123 A Part Production r1
123 B NonPart Released r2
234 A Part Production r4
567 A Part Snapshot r5
小二:
id revid item_type status rev_name
----- ------------- ---------------- ---------- --------------
123 B Part Released r1
123 C Part Production r12
234 A Part Production r3
345 A Part Production r5
567 A Part Production r5
以下是验证:-
在这里,P1中的p_id+p_revid应该与P2中的id+revid进行比较。
如果我们没有找到任何记录,请
id=p_id
并revid=p_revid
output_result
表和validaton_errror为"在目标中找不到">如果我们找到记录,请比较这些列并将错误输入为例如:-"Wrong_item_type"、">Wrong_release_status"。如 output_result表。
如果我们在 P2 中找到任何
revid
,而不是在 P1 中id
,则存储 ID, 从 output_result 中 revid,validation_eror
="在源代码中找不到">
output_result:-
id revid Validation_error
----- ------------- ------------------------
123 A Not found in target
123 B Wrong_item_type, wrong_rev_name
123 C Not_in_source
567 A Wrong_release_status.
您可以使用以下查询:
SELECT p1.p_id, p1.p_revid,
STUFF(
CASE
WHEN p2.id IS NULL THEN ',Not found in target'
ELSE IIF(p1.item_type <> p2.item_type, ',Wrong_item_type', '')
+
IIF(p1.status <> p2.status, ',Wrong_release_status', '')
+
IIF(p1.rev_name <> p2.rev_name, ',Wrong_rev_name', '')
END
,1, 1, '') AS Validation_error
FROM p1
LEFT JOIN P2
ON p1.p_id = p2.id AND p1.p_revid = p2.revid
UNION ALL
SELECT p2.id, p2.revid, 'Not_in_source'
FROM p2
WHERE NOT EXISTS
(
SELECT 1
FROM p1
WHERE p1.p_revid = p2.revid
)
输出:
p_id p_revid Validation_error
------------------------------------------------
123 A Not found in target
123 B Wrong_item_type,Wrong_rev_name
234 A Wrong_rev_name
567 A Wrong_release_status
123 C Not_in_source
在这里演示