Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move a table from one database to another database SQL Server

Tags:

sql-server

I have a database DB_1 which has an empty table T1 with 5 columns.

I want to move this table to another database DB_2 on the same SQL Server.

I have tried to use this command:

alter table DB_1.T1 rename DB_2.T1

but this showing error.

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'rename'.

Please help.

like image 634
Mudassir Hasan Avatar asked Sep 24 '12 04:09

Mudassir Hasan


2 Answers

If the databases are on same server then do it like this,

select * into DB_2.T1 from DB_1.[dbo].[T1]

if you have databases on different servers than you have to create a linked server.

On second thought you can generate "create tables scripts" and run them on second database

like image 150
Buzz Avatar answered Oct 30 '22 01:10

Buzz


In SQL Server Management Studio you have Import and Export Wizard :

  1. Right click on db name(DB_2)
  2. Tasks
  3. Import Data
  4. Choose data source (DB_1)
  5. Choose destination (DB_2)
  6. Choose copy data from one ore more tables
  7. Choose your table (T1)
  8. Finish
like image 32
Grisha Weintraub Avatar answered Oct 30 '22 00:10

Grisha Weintraub