2014-09-10 58 views
0

如何從兩個表(Patient和ContactDetails)將數據從DB1傳輸到DB2? 這兩個數據庫,都已經有這兩個數據表。我只是想將這兩個表中的數據從db1添加到db2。將數據從一個數據庫傳輸到另一個數據庫關於密鑰

我嘗試以下that

,但它沒有工作,因爲有一些行相同的按鍵並覆蓋是被禁止的。

還有其他方法可以做到嗎?或者我錯過了什麼?

患者和contactdetails關係是

patient inner join contactdetails 
(foreign_key)patient.contactdetailsid = (primary_key)contactdetails.id 
+0

你的主鍵是標識字段嗎? – 2014-09-10 13:58:03

+0

你只是想追加數據並保留現有數據,還是試圖替換整個表? – 2014-09-10 14:03:26

+0

這個副本的目的是什麼?是否保持2個數據庫同步?或者DB1是一個入門系統,DB2是母公司? – 2014-09-10 14:25:21

回答

0

環上的源contactdetails表,插入各行之一的時間在臨時表保存舊contactdetail id和匹配新contactdetail ID(here是一個例子sql循環)。
臨時表應該是這樣的:

create @temptableforcopy table (
oldcontactdetailsid [insertheretherightdatatype], 
newcontactdetailsid [insertheretherightdatatype] 
) 

從患者表中的數據複製連接到用於前面的步驟是這樣的臨時表:

insert into newdb.newschema.patient (contactdetailsid, field1, field2, ...) 
select TT.newcontactdetailsid, 
     old.field1, 
     old.field2, 
     ... 
from olddb.oldschema.patient old 
     join @temptableforcopy TT on TT.oldcontactdetailsid = old.contactdetailsid 

請注意,我的建議是隻是一個瘋狂的猜測:你沒有提供關於結構,密鑰,約束,沒有關於哪個密鑰阻止複製的信息,哪些錯誤消息,你已經丟棄的解決方案,你必須處理的數據量的信息...

相關問題