问题描述
我正在尝试将一个表的所有值插入到另一个表中.但是插入语句接受值,但我希望它接受来自 initial_Table 的 select *.这可能吗?
I am trying to insert all values of one table into another. But the insert statement accepts values, but i would like it to accept a select * from the initial_Table. Is this possible?
推荐答案
insert 语句实际上有这样做的语法.如果您指定列名而不是选择*"会容易得多:
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 ...
我最好澄清一下,因为出于某种原因,这篇文章得到了一些反对票.
I'd better clarify this because for some reason this post is getting a few down-votes.
INSERT INTO ... SELECT FROM 语法适用于您要插入的表(在我上面的示例中为new_table")已经存在的情况.正如其他人所说,SELECT ... INTO 语法适用于您想在命令中创建新表的情况.
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.
您没有指定是否需要在命令中创建新表,因此如果目标表已存在,则 INSERT INTO ... SELECT FROM 应该没问题.
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.
这篇关于将一个表的所有值插入到另一个表中的 SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!