作为系统验证的一部分,我正在尝试比较两个查询之间的结果(下面的简化版本)
-- Reference query, writing the criteria myself as part of the verification process
SELECT p.Id, p.FullName
FROM Person p
WHERE -- some criteria
和
-- Data Query, simply reading what my system under tests thinks of the same criteria
SELECT p.Id, p.FullName
FROM Person p
RIGHT JOIN PersonsThatMatchCriteria pmc ON ...
我想确保它们返回相同的结果,如果不是,我想显示它们以供分析。
((REFERENCE QUERY)
EXCEPT
(DATA QUERY))
UNION
((DATA QUERY)
EXCEPT
(REFERENCE QUERY))
由于我的流程在工具和自动化方面很差,我主要是手动输入这些请求。我讨厌必须重新键入/复制粘贴两次查询,因为它容易出错。
所以问题很简单:是否有一种简单的语法(或简单的工具)可以让我编写类似的东西
(REFERENCE QUERY)
XOR
(DATA QUERY)
我发现了一个效果很好但仍然很重的hack。有没有更好的(写起来更简单)?
DECLARE @ShouldBeIncluded TABLE(Id INT); INSERT INTO @ShouldBeIncluded
SELECT p.Id, p.FullName
FROM Person p
WHERE ...
DECLARE @ActuallyIncluded TABLE(Id INT); INSERT INTO @ActuallyIncluded
SELECT p.Id, p.FullName
FROM Person p
RIGHT J
SELECT p.Id, p.FullName
FROM Person p
WHERE p.Id IN ( SELECT Id FROM @ActuallyIncluded
EXCEPT SELECT Id FROM @ShouldBeIncluded )
OR p.Id IN ( SELECT Id FROM @ShouldBeIncluded
EXCEPT SELECT Id FROM @ActuallyIncluded )