2

What is the problem with this command.

SELECT *  INTO database2.table2 FROM database1.table1;

It returns this error:

ERROR 1327 (42000): Undeclared variable: database2

The goal is to transfer data in table1 to database2.

Rigel1121
  • 2,022
  • 1
  • 17
  • 24

5 Answers5

6

select ... into is for storing values in variables. See the documenation for more details. What you need is an insert into .. select ..

insert into database2.table2 (select * from database1.table1)
Jens
  • 67,715
  • 15
  • 98
  • 113
3

Use: CREATE TABLE database2.table2 AS select * from database1.table1;

MySQL probably does not support SELECT ... INTO ... FROM syntax.

HGF
  • 389
  • 3
  • 15
1

You should use something like that :

INSERT INTO newDatabase.table1 (Column1, Column2)
SELECT column1, column2 FROM oldDatabase.table1;
Ufuk Ugur
  • 186
  • 1
  • 17
0

Use this query :

 insert into database2.table2 
  select * from database1.table1;
Sagar Joon
  • 1,387
  • 14
  • 23
0

If those two databases are not connected to each other, you should think about that first.

  1. Connect two databases

    Use Linked Server for SQL Server.
    To know about how to create a linked server, See this link.

  2. Then use the query:

    insert into database2.table2 
        select * from database1.table1;
    

    The syntax is:

    INSERT INTO ToTableName 
        SELECT */ColNames FROM FromTableName
    
Community
  • 1
  • 1
Raging Bull
  • 18,593
  • 13
  • 50
  • 55