追加查询
SQL数据库中select into from和insert into select的区别
2015-01-20 11:19:07

SQL数据库中select into from和insert into select的区别

 

select * into target_table from source_table; insert into target_table(column1,column2) select column1,5 from source_table; 以上两句都是将源表source_table的记录插入到目标表target_table,但两句又有区别。
区别如下:
1. 第一句(select into from)要求目标表target_table不存在,因为在插入时会自动创建。
 
2.第二句(insert into select from)要求目标表target_table存在,由于目标表已经存在, 所以我们除了插入源表source_table的字段外,还可以插入常量,如例中的:5。
 
把一张旧表里的字段复制到另外一张新表中.可以这样写sql语句 
select * into 新表 from 旧表 where 新表.id=旧表.id