Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data merge

Status
Not open for further replies.

dpu

IS-IT--Management
Joined
Jan 24, 2005
Messages
179
Location
US
I have 2 databases that I need to merge into one. Can anyone tell me what the easiest way to do this is on SQL?
 
Denis query assumes that you don't have any of the tables from DB2 in DB1 already. Assuming that is true and you have more than a handful of tables that you could easily move yourself the following will iterate all of your DB2 tables and dynamically build the command to move them (I used a print statement in my test instead of actually executing, but I think it will work as I use something similar for reindexing)(The [ ] brackets are to ensure that if you have any weird table names like inventory-tbl, they are just enclosed):

declare curTables cursor for
select sysobjects.name from sysobjects where sysobjects.xtype = 'U'
declare @TableName varchar(255)

open curTables

fetch next from curTables into @TableName

while @@fetch_status = 0
begin
declare @command varchar(1000)
set @command = 'select * into db1.dbo.[' + @TableName + '] from db2.dbo.[' + @TableName + ']'
EXECUTE sp_executesql @command
fetch next from curTables into @TableName
end

close curTables
deallocate curTables
 
You can use DTS to copy tables/data by checking all tables applicable.

Regards,
AA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top