比较两个表并获得它们之间的差异

时间:2014-08-25 10:30:51

标签: sql sql-server-2008

我有两个表:TableA和TableB。

我需要比较两个表中的两个特定列(AnsId& Content)并获得它们的区别。 如果没有发现差异,那么我应该返回'流程成功',否则返回'失败'

TableA

+--------+-----------+---------+-------------+
|   Id   |  RSCId    | AnsId   |  Content    |
+--------+-----------+---------+-------------+
|    1   |   12      |  1      | Test data.  |
|    2   |   12      |  0      | Sample Test.|
|    3   |   12      |  5      | Test data.  |
|    4   |   12      |  7      | Test Data.  |
|    5   |   12      |  46     | Test datas. |
+--------+-----------+---------+-------------+

表B

    +--------+-----------+---------+-------------+
    |   Id   |  RSCId    | AnsId   |  Content    |
    +--------+-----------+---------+-------------+
    |    1   |   35      |  2      | Test .      |
    |    2   |   35      |  0      | Sample Test.|
    |    3   |   35      |  5      | Test data.  |
    |    4   |   35      |  7      | Test Data.  |
    |    5   |   35      |  46     | Test datas. |
    +--------+-----------+---------+-------------+

请帮帮我。

1 个答案:

答案 0 :(得分:0)

select case when sum(case when a.ansid is null or b.ansid is null 
                          then 1
                     end) > 0 
            then 'mismatch'
            else 'ok'
       end as result
from tableA a
full outer join tableB b on a.ansid = b.ansid and a.content = b.content