sql server - transfer data from one database to another regarding keys -
how can transfer rows 2 tables (patient , contactdetails) db1 db2? both dbs, have these 2 tables data. want add data these 2 tables db1 db2.
i tried following that
but didnt work, because there rows same keys , overwrite forbidden.
is there other way it? or missing something?
patient , contactdetails relationship is
patient inner join contactdetails (foreign_key)patient.contactdetailsid = (primary_key)contactdetails.id
loop on source contactdetails
table, insert each row 1 time saving in temp table old contactdetail id , matching new contactdetail id (here example of sql loop).
temp table should like:
create @temptableforcopy table ( oldcontactdetailsid [insertheretherightdatatype], newcontactdetailsid [insertheretherightdatatype] )
copy data patient table joined temp table used previous step this:
insert newdb.newschema.patient (contactdetailsid, field1, field2, ...) select tt.newcontactdetailsid, old.field1, old.field2, ... olddb.oldschema.patient old join @temptableforcopy tt on tt.oldcontactdetailsid = old.contactdetailsid
please note proposal wild guess: gave no information structure, keys, constraints, no detail key preventing copy error message, solution discarded, amount of data have deal with...
Comments
Post a Comment