【问题标题】:SQL Server compare two tables rows with identical columns and return changed columnsSQL Server 比较具有相同列的两个表行并返回更改的列
【发布时间】:2012-11-17 08:35:56
【问题描述】:

我想比较两个具有相同列的表:

  • product - Id, Name, Description
  • Temp_Product - Id, Name, Description

现在用户完成的更新将保存到Temp_Product。当管理员将看到该产品的详细信息时,我需要显示用户所做的更改。我想将这两个表与查询进行比较,并返回从Product 更改为Temp_Product 的列。

请建议我更好的方法来做到这一点?

【问题讨论】:

标签: c# sql sql-server c#-4.0


【解决方案1】:
Select p.id,p.name as orgn,t.name as altn,p.descripion as orgd,t.description as altd
from product p
join tmp_product t
on t.id=p.id and (t.name<>p.name or t.description <> p.description)

【讨论】:

  • 我怎样才能做一个与此等效的 linq 查询?
【解决方案2】:

我想将两个表与一个查询进行比较并返回 已从 Product 更改为 Temp_Product

由于这两个表具有相同的结构,您可以为此使用EXCEPT 集合操作符:

SELECT * FROM Temp_Product
EXCEPT
SELECT * FROM Product;

SQL Fiddle Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多