SQL用于连接和比较不同数据库中的表 [英] SQL to join and compare tables in a different database
问题描述
我在DB1的表中有一个select语句,并使用SQL Server 2014中的链接服务器连接到异地数据库DB2。
现在我有一些列具有相同的数据,例如,我在两个表中都有以下列:
- 文件名
- 位置
- ID
但是,表中的ID不一样但文件名是一样的。我将数据从DB2导入到DB1,但有些文件没有。所以我想比较这两个表,看看我没有从DB2复制过来将其复制到DB1。
我不知道如何使用链接服务器进行比较。最好的方法是什么?
提前谢谢
Hi,
I have a select statement in a table in DB1 and using a linker server in SQL Server 2014 to connect to offsite database DB2.
Now i have some columns which have the same data for example, i have the following columns in both tables:
- Filename
- Location
- ID
However, the ID's in the tables are not the same but the filenames are the same. I imported the data from DB2 to DB1 but some files did not. So i want to compare the two tables to see what i have not copied across from DB2 to be copied across to DB1.
I m not sure how to do a compare using the linked server. Whats the best way to go about this?
Thanks in advance
推荐答案
几种写作方式查询这样做。也许最简单的是使用except SQL运算符
Q1除了Q2将返回Q1中的所有行,但不包括Q2中的行。可以把它想象为Q1减去Q2。
因此查询
从DB1..table中选择Filename,除了从DB2选择Filename。表
应该给你想要的。
Several ways of writing a query to do this. Perhaps simplest is to use the except SQL operator
Q1 except Q2 will return all rows in Q1 but excluding those in Q2. Think of it as Q1 minus Q2.
Thus a query of
select Filename from DB1..table except select Filename from DB2..table
should give you want you want.
这篇关于SQL用于连接和比较不同数据库中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!