如何在sql中将数据从一个表导入到另一个表

时间:2022-09-30 23:10:34

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 ?

我有两个数据库,两个表都是数据库如何从数据库表导入数据到sql2008标准版中的另一个数据库表。我试过导出/导入功能,但运气不好。你能帮助我吗?

2 个解决方案

#1


4  

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.

还可以添加where子句。

#2


2  

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语句实际上有这样做的语法。如果您指定列名而不是选择“*”,就会容易得多:

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.

插入……SELECT FROM语法用于当您要插入的表(在我上面的示例中为“new_table”)已经存在时。正如其他人所说,选择……INTO语法用于当您想要创建新表作为命令的一部分时。

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.

您没有指定是否需要将新表作为命令的一部分创建,因此插入到…如果目标表已经存在,那么SELECT FROM应该没问题。

OR Check this link

或检查这个链接

i hope this answer useful for you.

我希望这个答案对你有用。

#1


4  

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.

还可以添加where子句。

#2


2  

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语句实际上有这样做的语法。如果您指定列名而不是选择“*”,就会容易得多:

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.

插入……SELECT FROM语法用于当您要插入的表(在我上面的示例中为“new_table”)已经存在时。正如其他人所说,选择……INTO语法用于当您想要创建新表作为命令的一部分时。

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.

您没有指定是否需要将新表作为命令的一部分创建,因此插入到…如果目标表已经存在,那么SELECT FROM应该没问题。

OR Check this link

或检查这个链接

i hope this answer useful for you.

我希望这个答案对你有用。