[英]How do I only import unique records into a table from another table?
I am trying to update a table in SQL Server.我正在尝试更新 SQL 服务器中的表。 Table is called table1 with columns(code, desc).
表称为 table1 列(代码,描述)。 I want to update table1 with unique records from table2(code, desc).
我想用 table2(code, desc) 中的唯一记录更新 table1。 table2 is a copy of table1 but it contains records that are not present in table1.
table2 是 table1 的副本,但它包含 table1 中不存在的记录。
Sounds like you want an INSERT
听起来你想要一个
INSERT
Something like this should work:像这样的东西应该工作:
INSERT INTO table1 (code, desc)
SELECT t2.code, t2.desc
FROM table2 t2
LEFT JOIN table1 t1 on t2.code = t1.code and t1.desc = t2.desc
WHERE t1.code is null --Ignore records that already exist in table1
... Adjust join clause accordingly. ... 相应地调整连接子句。
To update table1.desc
with values from matching rows in table2
simply do:要使用
table2
中匹配行的值更新table1.desc
,只需执行以下操作:
update t1 set
t1.desc = t2.desc
from table1 t1
join table2 t2 on t2.code = t1.code;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.