简体   繁体   中英

Copying Data from One Database to Another SQL code

I am looking for help; Situation is that I have 2 databases which should be identical in structure with the exception that the NEW prod dbs has ident columns, PK and fk contraints defined. I have to consolidate 4 slightly different dbs in one 'TempDB' then copy the data into the 'NewProddb'.

I have managed a script lto turn off FK checks, and turn on FK checks, so the insert needs to look something like this;

[Script to disable all FK Constraints (nocheck)]

SET IDENTITY_INSERT [NewProdDB].[dbo].[Event] ON; 
INSERT INTO [dbo].[Event] 
(EventID, Name, StartDate, EndDate, PartnerRegStartDate, PartnerRegEndDate, HouseholdRegStartDate, HouseholdRegEndDate, ChannelId, HasTeam, MaxteamMembers) 

(Select 
EventID, Name, StartDate, EndDate, PartnerRegStartDate, PartnerRegEndDate, HouseholdRegStartDate, HouseholdRegEndDate, ChannelId, HasTeam, MaxteamMembers
From TempDB.dbo.Event);

SET IDENTITY_INSERT [NewProdDb].dbo.[Event] OFF; 

[Script to enable all FK Constraints (check)]

What I want to do is to script this in one go rather than writing a script for ALL the tables separately. ALl Data in All Columns is to be copied.

I hope this is clear.

Thanks in Advance.

I will try to answer in terms of Oracle db, you can find equivalents in MSSQL.

There are system tables which store tables & columns information. So your script can do the following

  1. Pass source schema & target schema
  2. Load tables & corresponding columns with a join between ALL_TABLES & ALL_TAB_COLUMNS for the source schema. Loop the results & generate your insert scripts. You can write it to sql file & then execute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM