![](/img/trans.png)
[英]Copy data from one table and save it into another table in different database on different SQL Server
[英]Copy data from table and insert in different SQL Server database
我有两个数据库
databaseone
databasetwo
并且我在数据库中有一个类似的表,该表的名称为tableemployeedetails
。
在我的databaseone
,表tableemployeedetails
有500列。
在databasetwo
,表tableemployeedetails
有10列。
我不能使用insert into select query
因为我想将数据插入到其他数据库中。
在我的情况下,最好的方法是什么?
我只想在两个数据库中合并tableemployeedetails
尝试这个,
insert into databasetwo..tableemployeedetails
SELECT * FROM databaseone..tableemployeedetails A
WHERE NOT EXISTS (SELECT 1 FROM databasetwo..tableemployeedetails B
WHERE A.COLUMN=B.COLUMN
)
如果两个数据库都有不同的记录,则需要两个如下的插入语句。 如果它们相同,则除了下面的插入内容外,您还需要首选哪些数据库记录是最新的,然后编写更新。
insert into databasetwo..tableemployeedetails
SELECT * FROM databaseone..tableemployeedetails d1
left join databasetwo..tableemployeedetails d2 on A.PKKEY=B.PKKEY
where d2.PKKEY is null
insert into databaseone..tableemployeedetails
SELECT * FROM databasetwo..tableemployeedetails d2
left join databaseone..tableemployeedetails d1 on A.PKKEY=B.PKKEY
where d1.PKKEY is null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.