拷贝的表的SQL语句 SELECT INTO 和 INSERT INTO SELECT的用法与区别
一、select into from
语句形式为:Insert into Table2(field1,field2,…) select value1,value2,… from Table1
应用场景:常用于创建表的备份复件或者用于对记录进行存档
example1:
SELECT * INTO dbo.t_Category20190327 FROM dbo.t_Category WHERE ParentId=0
example2:
SELECT Persons.LastName,Orders.OrderNo INTO Persons_Order_Backup FROM Persons INNER JOIN Orders ON Persons.Id_P=Orders.Id_P
example3:
SELECT * INTO Persons IN \'Backup.mdb\' FROM Persons
备注:IN 子句可用于向另一个数据库中拷贝表
二、insert into from
语句形式为:Insert into Table2(field1,field2,…) select value1,value2,… from Table1
应用场景:用于从一表格中取数据填充到另一表格中。
example1:
INSERT INTO dbo.t_Category( Name ,ParentId , Depth ,Sort ) SELECT Name,PId,0,0 FROM dbo.t_Category20190327
三、区别:
select into from 要求填充的目标表不存在,因为在插入时会自动创建目标表。
insert into from 要求目标表必须存在。