拷貝的表的SQL語句 SELECT INTO 和 INSERT INTO SELECT的用法與區別

1、select   into  from

 語句形式爲:Insert into Table2(field1,field2,...) select value1,value2,... from Table1數據庫

 應用場景:經常使用於建立表的備份復件或者用於對記錄進行存檔spa

example1:code

SELECT * INTO  dbo.t_Category20190327 FROM dbo.t_Category WHERE ParentId=0

example2:blog

SELECT Persons.LastName,Orders.OrderNo
INTO Persons_Order_Backup
FROM Persons
INNER JOIN Orders
ON Persons.Id_P=Orders.Id_P

example3:ast

SELECT *
INTO Persons IN 'Backup.mdb'
FROM Persons

備註:IN 子句可用於向另外一個數據庫中拷貝表class

2、insert into from

語句形式爲:Insert into Table2(field1,field2,...) select value1,value2,... from Table1select

 應用場景:用於從一表格中取數據填充到另外一表格中。數據

example1:db

INSERT INTO dbo.t_Category( Name ,ParentId , Depth ,Sort  ) SELECT Name,PId,0,0 FROM dbo.t_Category20190327

 

3、區別:

select   into  from  要求填充的目標表不存在,由於在插入時會自動建立目標表。di

insert into from     要求目標表必須存在。

相關文章
相關標籤/搜索