SQL Server'select*into'与'insert into ..select*

Suj*_*ran 28 sql-server select-into

table1并且table2已经存在,这些查询之间是否有任何区别

query1: -

select * into table1 from table2 where 1=1
Run Code Online (Sandbox Code Playgroud)

query2: -

insert into table1 select * from table2
Run Code Online (Sandbox Code Playgroud)

aF.*_*aF. 43

select * into table1 from table2 where 1=1创建表并插入在其中表2的值.因此,如果已经创建了该表,那么该语句将产生错误.

insert into table1 select * from table2只有在插入表1表2的值.

  • 如果表存在,我不知道query-1会产生的错误.非常感谢你 ! (2认同)

Mar*_*ith 8

第一个(SELECT INTO)将创建并填充第二个(INSERT... SELECT)插入到现有表的新表.

在2008年之前的SQL Server版本中,第一个版本可以最少记录,第二个版本可以不记录,但这不再是真实的.


gbn*_*gbn 5

在下面的查询中,table1将被创建,如果已经存在,则会抛出错误

select * into table1 from table2 where 1=1
Run Code Online (Sandbox Code Playgroud)

在下面的查询中,运行命令之前该表table1 必须存在

insert into table1 select * from table2
Run Code Online (Sandbox Code Playgroud)


Cha*_*arl 5

select * into table1 from table2 where 1=1
Run Code Online (Sandbox Code Playgroud)

上面的查询要求表不存在.您无需指定列,因为从源表中检索所有列时都会创建这些列.

insert into table1 select * from table2 
Run Code Online (Sandbox Code Playgroud)

对于上面的查询,您需要一个EXISTING table1.两个表中的列也应该完全相同,否则您需要为两个表提供列列表.