我有两个具有相同表结构但数据不同的独立数据库。我的数据库“a”有一个名为 articles 的表,其中包含 id、heading、detail 和 date 列。数据库“b”也有相同的表。
例如:
一个数据库有 10 行。
b 数据库有 12 行。
有没有办法从 a 中获取第 3 个选定行并将其与行中包含的数据一起传输到 b。
最佳答案
对该问题的一个合理解释是,您想将 a.dbo.articles
中尚不存在的行移动到 b.dbo.articles
:
insert into b.dbo.articles(id, heading, detail, date)
select id, heading, detail, date
from a.dbo.articles a
where not exists (select 1 from b.dbo.articles b where b.id = a.id);
注意事项:
- 假设这两个数据库在同一台服务器上。否则,您需要一个数据库链接。
- 假设
id
足以进行比较。如果没有,只需将正确的逻辑添加到where
子句即可。
关于sql - MS SQL 获取带有数据的表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33389125/