Copy Tables from One Database to Another in SQL Server

Copy tables from one database to another in SQL Server

On SQL Server? and on the same database server? Use three part naming.

INSERT INTO bar..tblFoobar( *fieldlist* )
SELECT *fieldlist* FROM foo..tblFoobar

This just moves the data. If you want to move the table definition (and other attributes such as permissions and indexes), you'll have to do something else.

Copy table from one database to another

Create a linked server to the source server. The easiest way is to right click "Linked Servers" in Management Studio; it's under Management -> Server Objects.

Then you can copy the table using a 4-part name, server.database.schema.table:

select  *
into DbName.dbo.NewTable
from LinkedServer.DbName.dbo.OldTable

This will both create the new table with the same structure as the original one and copy the data over.

SQL Server Copying tables from one database to another

Assuming that you have two databases, for example A and B:

  • If target table not exists, the following script will create (I do not recommend this way):

    SELECT table_A.FIELD_1, table_A.FIELD_2,......, table_A.FIELD_N 
    INTO COPY_TABLE_HERE
    FROM A.dbo.table_from_A table_A
  • If target table exists, then:

     INSERT INTO TABLE_TARGET 
    SELECT table_A.FIELD_1, table_A.FIELD_2,......, table_A.FIELD_N
    FROM A.dbo.table_from_A table_A

Note: if you want learn and practice this, you can use previous scripts, but if you want copy the complete structure and data from database to another, you should use, "Backup and restore Database" or, "Generate Script Database with data" and run this into another database.

SQL Server - Create a copy of a database table and place it in the same database?

Use SELECT ... INTO:

SELECT *
INTO ABC_1
FROM ABC;

This will create a new table ABC_1 that has the same column structure as ABC and contains the same data. Constraints (e.g. keys, default values), however, are -not- copied.

You can run this query multiple times with a different table name each time.


If you don't need to copy the data, only to create a new empty table with the same column structure, add a WHERE clause with a falsy expression:

SELECT *
INTO ABC_1
FROM ABC
WHERE 1 <> 1;

copy tables with data to another database in SQL Server 2008

You can use the same way to copy the tables within one database, the SELECT INTO but use a fully qualified tables names database.schema.object_name instead like so:

USE TheOtherDB;

SELECT *
INTO NewTable
FROM TheFirstDB.Schemaname.OldTable

This will create a new table Newtable in the database TheOtherDB from the table OldTable whih belongs to the databaseTheFirstDB

How to create a copy of database tables into another computer

Here is what I did to make a copy of all the tables along with the data present in my database of "computer A" into the database of "computer B".

  1. Microsoft SQL Server Management Studio > Connect.
  2. Right Click on The database which has the tables > Tasks > Generate Scripts.
  3. Select either full database Specific table > Next
  4. Advanced > Scroll down to "Types of Data to Script" and select "Schema and Data". > OK
  5. Select location to save Script
  6. Next > Next > Finish.

Now copy This Script File into "computer B"

  1. Double click will open it into Microsoft SQL Server Management Studio
  2. Click "Execute" button from top toolbar.

Done, Just Refresh the database and all your tables with the data are now copied.

Copy tables from one database to another using Alteryx

You can do this with a batch macro where each batch will bring in a different table. The Dynamic Input tool will not work because the tables are of different schemas.

The workflow will have a Control Parameter (set to the table name) and updating an Input Data tool. This will then go to an Output Tool that has the "Take File/Table Name from field" option selected.

Export table data from one SQL Server to another

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.



Related Topics



Leave a reply



Submit