27

I have several hundred (currently 466, but ever growing) tables I have to copy from one server to another.

I have never had to do this before, so I'm not sure at all on how to approach it. All the tables are in the same format: Cart<Eight character customer number>

This is part of a larger project of which I am merging all these Cart<Number> tables to one Carts table, but that's a whole different question altogether.

Does anyone have a best-practice method I can use to copy all these tables over? The database names on both servers are the same, if that helps. And as I said earlier, I have the sa account so I can do whatever is necessary to get the data from A to B. Both servers are in the same server farm, as well.

21

You could use SQL Server Management Studio's "Export Data" task. Right click on the database in SSMS, select Tasks and then "Export Data". It will launch an interactive wizard that will allow to copy tables from one server to another, although you will have to recreate the indices yourself. The wizard creates a temporary SSIS package (although you can also opt to save it) and is relatively fast.

  • 4
    Can also do the same Export task via PowerShell if interested. Similar script to perform this action is in my answer here: dba.stackexchange.com/a/122149/507 – Shawn Melton Dec 1 '15 at 17:28
  • why doesn't the import/export copy the indices and dependencies? wouldn't it be too difficult to recreate the indices for 466 tables? – Vini Dec 3 '15 at 10:19
  • @Vini you can also use the Generate Scripts wizard, which does allow you to copy indexes (I don't know if it reliably generates a script in proper dependency order, though - I've always used RedGate SQL Compare for that). – Aaron Bertrand Dec 3 '15 at 15:01
  • @AaronBertrand : OK. I too had the same question. But when I imported datetime was also converted to smalldatetime – Vini Dec 4 '15 at 6:22
23

Here's a quick & dirty approach that only needs a linked server, in each direction, with sufficient privileges, collation compatibility, and data access enabled. You run this on the source linked server to generate the dynamic SQL that will be executed on the destination linked server.

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += N'SELECT * INTO [database].dbo.' + QUOTENAME(name)
  + N' FROM [source_linked_server].[database].dbo.' + QUOTENAME(name) + N';'
FROM sys.tables
WHERE name LIKE N'Cart[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]';

PRINT @sql; -- this will only print 8K, enough to spot check
--EXEC [destination_linked_server].master.sys.sp_executesql @sql;
19

If you want something that can be scripted to be easily re-runnable as you test this and make minor changes, check out my answer here:

Import data from one Database to another script

That answer describes using a SQLCLR stored procedure which makes use of the SqlBulkCopy class in .NET. Using that stored procedure can be done in a cursor that loops through the tables. This allows for easy editing of the process as well as accounting for new tables, or easily excluding one or more table via a WHERE condition in the cursor query.

19

You can use BCP OUT and BULK INSERT INTO the destination database. You can use this script

You can use SSIS (data Import/export) to do the data transfer as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.