99

I have two SQL Servers (both 2005 version).

I want to migrate several tables from one to another.

I have tried:

  • On source server I have right clicked on the database, selected Tasks/Generate scripts. The problem is that under Table/View options there is no Script data option.

  • Then I used Script Table As/Create script to generate SQL files in order to create the tables on my destination server. But I still need all the data.

Then I tried using:

SELECT * 
INTO [destination server].[destination database].[dbo].[destination table] 
FROM [source server].[source database].[dbo].[source table]

But I get the error:

Object contains more than the maximum number of prefixes. Maximum is 2.

Can someone please point me to the right solution to my problem?

5
  • 1
    Can you display some of your generated statements please? Have you added the other servers as linked servers? Commented Jun 13, 2012 at 6:20
  • not as linked servers. That did not make sense, since both are mssql2005. I just created the connection. It must be linked server? Commented Jun 13, 2012 at 6:57
  • "Into" statement (that creates a table and insert into it) only supports local tables. You have to create the table first and then use "insert into [destination server].[destination database].[dbo].[destination table]". Commented Feb 5, 2016 at 16:31
  • The easy way is to take a backup (with data) & then restore in target database Commented Aug 4, 2016 at 13:33
  • I got error like server not available/not linked. so i used below commands and then it worked USE master; GO EXEC sp_addlinkedserver N'servername', N'SQL Server'; GO Commented Jul 27, 2018 at 8:07

12 Answers 12

85

Try this:

  1. create your table on the target server using your scripts from the Script Table As / Create Script step

  2. on the target server, you can then issue a T-SQL statement:

    INSERT INTO dbo.YourTableNameHere
       SELECT *
       FROM [SourceServer].[SourceDatabase].dbo.YourTableNameHere
    

This should work just fine.

Sign up to request clarification or add additional context in comments.

3 Comments

I like your proposal. I tried it and i get. Could not find server "" in sys.servers. Verify that the correct server name is specified... Maybe it is a problem sinte source server name is in "NAME\NAME2" format? But i think [] should fix that .... idea?
@no9: sorry - of course, for this to work, you must add a "linked server" between your two servers.
yeah. But imagine, i have problem creating linked server. The production server is set to allow remote connections yet I am still getting a timeout even if using impersonate or directly usr/pwd. Since the server managment is not in my domain there is not much i can do. But i managed to solve my problem a different way. Will post an anwser just for fun and accept yours since i like the solution.
85

Just to show yet another option (for SQL Server 2008 and above):

  1. right-click on Database -> select 'Tasks' -> select 'Generate Scripts'
  2. Select specific database objects you want to copy. Let's say one or more tables. Click Next
  3. Click Advanced and scroll down to 'Types of Data to script' and choose 'Schema and Data'. Click OK
  4. Choose where to save generated script and proceed by clicking Next

2 Comments

This option is not available in the Microsoft SQL Management Studio Version 9 (accompanying SQL Server 2005) which the OP is most likely referring to.
Good solution but it starts to be problematic with large tables.
21

If you don't have permission to link servers, here are the steps to import a table from one server to another using Sql Server Import/Export Wizard:

  • Right click on the source database you want to copy from.
  • Select Tasks - Export Data.
  • Select Sql Server Native Client in the data source.
  • Select your authentication type (Sql Server or Windows authentication).
  • Select the source database.
  • Next, choose the Destination: Sql Server Native Client
  • Type in your Server Name (the server you want to copy the table to).
  • Select your authentication type (Sql Server or Windows authentication).
  • Select the destination database.
  • Select Copy data.
  • Select your table from the list.
  • Hit Next, Select Run immediately, or optionally, you can also save the package to a file or Sql Server if you want to run it later.
  • Finish

2 Comments

turned out to be the easiest approach for me. Although, ssms works heavily/slow to open that wizard!
But primary and foreign key relationships are not getting copied to the new database.
12

There is script table option in Tasks/Generate scripts! I also missed it at beginning! But you can generate insert scripts there (very nice feature, but in very un-intuitive place).

When you get to step "Set Scripting Options" go to "Advanced" tab.

Steps described here (pictures can understand, but i do write in latvian there).

Comments

10

Try using the SQL Server Import and Export Wizard (under Tasks -> Export Data).

It offers to create the tables in the destination database. Whereas, as you've seen, the scripting wizard can only create the table structure.

5 Comments

There is no export data option on the database that i want to export tables from. Its just: Detach, Shrink, Backup, Restore, Generate scripts...
scripting wizzard can export data too
@user763539 - really? Last time I checked there was no option to generate a script that includes e.g. INSERT statements; certainly seems to be true for sql-server-2005. Do you have a link or reference?
The best solution for me so far!
@Damien_The_Unbeliever why do you insist on script generating? That was not the question...
7

If the tables are already created using the scripts, then there is another way to copy the data is by using BCP command to copy all the data from your source server to your destination server

To export the table data into a text file on source server:

bcp <database name>.<schema name>.<table name> OUT C:\FILE.TXT -c -t -T -S <server_name[ \instance_name]> -U <username> -P <Password> 

To import the table data from a text file on target server:

bcp <database name>.<schema name>.<table name> IN C:\FILE.TXT -c -t -T -S <server_name[ \instance_name]> -U <username> -P <Password>

4 Comments

I seen this before, but since im exporting from a production server i wasnt (still am not) sure if its safe :)
Your not modifying your table so its absolutely safe but remember to use IN or OUT statement properly . In to get the data from Text file to Table and OUT to get the data from Table to a text file ( or a csv file )
Use "-T" for trusted connection or "-U <username> -P <Password>" for SQL login.
Thanks! I was having a heck of a time trying to copy a table from one machine to another, where they couldn't talk to each other directly. Exporting to flat file with the import/export wizard worked fine, but importing the flat file with the import/export wizard was running into all kinds of wacky issues cause it was a flat file. This worked flawlessly.
3

For copying data from source to destination:

use <DestinationDatabase>
select * into <DestinationTable> from <SourceDataBase>.dbo.<SourceTable>

3 Comments

I have two different servers. If i had only one this would work.
Did you create a linked server? Not clear from your question. Then add the <SourceServer>'s name in front of <SourceDatabase> in the query above (executed on the <DestinationServer>).
cant create a linked server. See my reply to marc_s anwser
3

Just for the kicks.

Since I wasnt able to create linked server and since just connecting to production server was not enough to use INSERT INTO i did the following:

  • created a backup of production server database
  • restored the database on my test server
  • executed the insert into statements

Its a backdoor solution, but since i had problems it worked for me.

Since i have created empty tables using SCRIPT TABLE AS / CREATE in order to transfer all the keys and indexes I couldnt use SELECT INTO. SELECT INTO only works if the tables do not exist on the destination location but it does not copy keys and indexes, so you have to do that manualy. The downside of using INSERT INTO statement is that you have to manualy provide with all the column names, plus it might give you some problems if some foreign key constraints fail.

Thanks to all anwsers, there are some great solutions but i have decided to accept marc_s anwser.

Comments

2

You can't choose a source/destination server.

If the databases are on the same server you can do this:

If the columns of the table are equal (including order!) then you can do this:

INSERT INTO [destination database].[dbo].[destination table]
SELECT *
FROM [source database].[dbo].[source table]

If you want to do this once you can backup/restore the source database. If you need to do this more often I recommend you start a SSIS project where you define source database (there you can choose any connection on any server) and create a project where you move your data there. See more information here: http://msdn.microsoft.com/en-us/library/ms169917%28v=sql.105%29.aspx

6 Comments

Yes you can if you added them as linked servers msdn.microsoft.com/en-us/library/ms188279.aspx
Oh well, didn't know that myself. Thx for enlight me to that. In the case above I still recommend a SSIS project for this tasks.
SSIS is a bit of overload for a one off copy - but for regular and incremental loads I'd suggest it too!
since its SQL2005 express SSIS is not supported.
SQL 2005 Express? Standard and above has SSIS.
|
2

It can be done through "Import/Export Data..." in SQL Server Management Studio

Comments

1

This is somewhat a go around solution but it worked for me I hope it works for this problem for others as well:

You can run the select SQL query on the table that you want to export and save the result as .xls in you drive.

Now create the table you want to add data with all the columns and indexes. This can be easily done with the right click on the actual table and selecting Create To script option.

Now you can right click on the DB where you want to add you table and select the Tasks>Import .

Import Export wizard opens and select next.Select the Microsoft Excel as input Data source and then browse and select the .xls file you have saved earlier.

Now select the destination server and also the destination table we have created already.

Note:If there is any identity based field, in the destination table you might want to remove the identity property as this data will also be inserted . So if you had this one as Identity property only then it would error out the import process.

Now hit next and hit finish and it will show you how many records are being imported and return success if no errors occur.

1 Comment

The key thing for me here was being able to selectively choose the data to export
0

Yet another option if you have it available: c# .net. In particular, the Microsoft.SqlServer.Management.Smo namespace.

I use code similar to the following in a Script Component of one of my SSIS packages.

var tableToTransfer = "someTable";
var transferringTableSchema = "dbo";

var srvSource = new Server("sourceServer");
var dbSource = srvSource.Databases["sourceDB"];

var srvDestination = new Server("destinationServer"); 
var dbDestination = srvDestination.Databases["destinationDB"];

var xfr = 
    new Transfer(dbSource) {
        DestinationServer = srvDestination.Name,
        DestinationDatabase = dbDestination.Name,
        CopyAllObjects = false,
        DestinationLoginSecure = true,
        DropDestinationObjectsFirst = true,
        CopyData = true
    };

xfr.Options.ContinueScriptingOnError = false; 
xfr.Options.WithDependencies = false; 

xfr.ObjectList.Add(dbSource.Tables[tableToTransfer,transferringTableSchema]);
xfr.TransferData();

I think I had to explicitly search for and add the Microsoft.SqlServer.Smo library to the references. But outside of that, this has been working out for me.

Update: The namespace and libraries were more complicated than I remembered.

For libraries, add references to:

  • Microsoft.SqlServer.Smo.dll
  • Microsoft.SqlServer.SmoExtended.dll
  • Microsoft.SqlServer.ConnectionInfo.dll
  • Microsoft.SqlServer.Management.Sdk.Sfc.dll

For the Namespaces, add:

  • Microsoft.SqlServer.Management.Common
  • Microsoft.SqlServer.Management.Smo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.