年龄、姓名、职业是否构成主键(或至少是唯一键?):
如果是这样,您可以执行以下操作:
SELECT
t1.name, t1.age, t1.profession,
t1.column1 as t1column1, t2.column1 as t2column1,
t1.column2 as t1column2, t2.column2 as t2column2,
FROM
table1 t1, table2 t2
WHERE
(t1.name = t2.name and t1.age = t2.age and t1.profession = t2.profession) and
(
t1.column1<>t2.column1
OR t1.column2<>t2.column2
OR t1.column3<>t2.column3)
当然,这需要两个表中相同的唯一键。此外,我清楚地更改了查询结果以显示所有列,而不仅仅是更改的列。在单个 T-SQL 查询中识别像这样更改的那个是尴尬的(但可能)所以我的建议是像这样返回它,并根据您的用例让应用程序/表示层处理查找更改的列或只是扫描肉眼可见。
如果你真的想用 T-SQL 来做,你可以用 UNIONS 来做,比如:
SELECT
t1.name, t1.age, t1.profession,
'column1' as ColumnChanged,
t1.column1 as oldValue,
t2.column1 as newValue
FROM
table1 t1, table2 t2
WHERE
(t1.name = t2.name and t1.age = t2.age and t1.profession = t2.profession) and
t1.column1<>t2.column1
UNION ALL #better peformance than UNION, which would enforce uniqueness
SELECT
t1.name, t1.age, t1.profession,
'column2' as ColumnChanged,
t1.column2 as oldValue,
t2.column2 as newValue
FROM
table1 t1, table2 t2
WHERE
(t1.name = t2.name and t1.age = t2.age and t1.profession = t2.profession) and
t1.column2<>t2.column2
.......