[英]SQL Server : compare values from two or more tables
內部查詢的結果有什么問題?
SELECT
ContractType,
LeaseType,
ContractNo,
ContractNumber
FROM
TableOne INNER JOIN
TableA ON
ContractNo = ContractNumber
WHERE
ContractNo = 101
您不需要輸出ContractNo和ContractNumber,因為它們必須始終匹配:
SELECT t1.ContractNo, t2.ContractNumber,
CASE
WHEN t1.ContractType != t2.LeaseType THEN 'ContractType'
WHEN t1.[Address] != t2.AddressMain THEN 'Address'
ELSE 'N/A' END as SystemA_Field,
CASE
WHEN t1.ContractType != t2.LeaseType THEN 'LeaseType'
WHEN t1.[Address] != t2.AddressMain THEN 'AddressMain'
ELSE 'N/A' END as SystemB_Field,
CASE
WHEN t1.ContractType != t2.LeaseType THEN CAST(ContractType as VARCHAR)
WHEN t1.[Address] != t2.AddressMain THEN CAST(Address as VARCHAR)
ELSE 'N/A' END as SystemA_Value,
CASE
WHEN t1.ContractType != t2.LeaseType THEN CAST(LeaseType as VARCHAR)
WHEN t1.[Address] != t2.AddressMain THEN CAST(AddressMain as VARCHAR)
ELSE 'N/A' END as SystemB_Value
FROM Table1 as t1
INNER JOIN Table2 as t2
ON t1.ContractNo = t2.ContractNumber
WHERE t1.ContractType != t2.LeaseType
OR t1.[Address] != t2.AddressMain
順便說一句,您是否看過Redgate的SQL比較? http://www.red-gate.com/products/sql-development/sql-comparison-sdk/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.