使用 SELECT INTO 語法創建新表,但是新表不會有唯一鍵。
最基本的複製創建,無唯一鍵。
-- 將會創建一個完全相同欄位,資料的資料表 SELECT * INTO [Users2] FROM [Users]
加上流水號,有唯一鍵。
SELECT PkId = IDENTITY(INT, 1, 1), -- 建立 PK 流水號 [Users].Name, [Users].Age, [Users].CreateDate INTO [Users4] FROM [Users]
使用 Join 的來源,產生新表,有唯一鍵。
SELECT Id = IDENTITY(INT, 1, 1), -- 建立 PK 流水號 Name = tbl.Name, Age = tbl.Age, Country = tbl.Country, CreateDate = tbl.CreateDate INTO [Users3] FROM ( -- 使用 Join 的資料 SELECT u.Id AS ID, u.Name AS Name, u.Age AS Age, u.CreateDate AS CreateDate, c.Country AS Country FROM [Users] u INNER JOIN [Customers] c ON u.Id = c.CustomerId ) tbl