Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import data from one table to another table in sql

I have two database there is two table both databse how to import data from database table to another database table in sql2008 standard edition. I have tried to export/import function but no luck . would you Please help me ?

like image 492
Sajith Avatar asked Sep 05 '13 08:09

Sajith


People also ask

How do I import data from a table to another table?

INSERT INTO new_table (Foo, Bar, Fizz, Buzz) SELECT Foo, Bar, Fizz, Buzz FROM initial_table -- optionally WHERE ... The INSERT INTO ... SELECT FROM syntax is for when the table you're inserting into ("new_table" in my example above) already exists. As others have said, the SELECT ...

How do I Export data from one table to another table in SQL Server?

Launch SQL Server Management Studio. Select and right-click on the Source Database, go to Tasks > Export Data. Import/Export Wizard will be opened and click on Next to proceed. Enter the data source, server name and select the authentication method and the source database.


2 Answers

I think the code below will work for your case:

INSERT INTO table1 (column1,column2)
SELECT oldcolumn1, oldcolumn2
FROM table2 

Optionally you could add a where clause.

like image 197
Christos Avatar answered Sep 30 '22 07:09

Christos


Use this code and check below links

The insert statement actually has a syntax for doing just that. It's a lot easier if you specify the column names rather than selecting "*" though:

INSERT INTO new_table (Foo, Bar, Fizz, Buzz)
SELECT Foo, Bar, Fizz, Buzz
FROM initial_table
-- optionally WHERE ...

The INSERT INTO ... SELECT FROM syntax is for when the table you're inserting into ("new_table" in my example above) already exists. As others have said, the SELECT ... INTO syntax is for when you want to create the new table as part of the command.

You didn't specify whether the new table needs to be created as part of the command, so INSERT INTO ... SELECT FROM should be fine if your destination table already exists.

OR Check this link

i hope this answer useful for you.

like image 24
Darshan Kunjadiya Avatar answered Sep 30 '22 06:09

Darshan Kunjadiya