INSERT INTO vs SELECT INTO

Posted BI Information

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了INSERT INTO vs SELECT INTO相关的知识,希望对你有一定的参考价值。

What is the difference between using

SELECT ... INTO MyTable FROM...

and

INSERT INTO MyTable (...)

SELECT ... FROM ....

?

From BOL [ INSERTSELECT...INTO ], I know that using SELECT...INTO will create the insertion table on the default file group if it doesn‘t already exist, and that the logging for this statement depends on the recovery model of the database.

  1. Which statement is preferable?
  2. Are there other performance implications?
  3. What is a good use case for SELECT...INTO over INSERT INTO ...?

Edit: I already stated that I know that that SELECT INTO... creates a table where it doesn‘t exist. What I want to know is that SQL includes this statement for a reason, what is it? Is it doing something different behind the scenes for inserting rows, or is it just syntactic sugar on top of a CREATE TABLEand INSERT INTO.

 

  1. They do different things. Use INSERT when the table exists. Use SELECT INTO when it does not.
  2. Yes. INSERT with no table hints is normally logged. SELECT INTO is minimally logged assuming proper trace flags are set.
  3. In my experience SELECT INTO is most commonly used with intermediate data sets, like #temptables, or to copy out an entire table like for a backup. INSERT INTO is used when you insert into an existing table with a known structure.

以上是关于INSERT INTO vs SELECT INTO的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL insert into select 锁表问题

select into 与 insert into select

使用 insert into select 将源列添加到输出子句

INSERT INTO 表 SELECT 和 SELECT INTO 表之间的性能差异 [重复]

PostgreSQL SELECT INTO和INSERT INTO SELECT 两种表复制语句

mysql insert into select 语句为啥会造成死锁