SQL SELECT INTO Statement
What is the SELECT INTO statement used for?
SELECT INTO copies data from one table into a brand new table.
This operation creates a new table in the default filegroup.
The SQL SELECT INTO syntax
The general syntax is
SELECT column-names INTO new-table-name FROM table-name WHERE condition
The new table will have column names as specified in the query.
SUPPLIER |
---|
Id |
CompanyName |
ContactName |
City |
Country |
Phone |
Fax |
SQL SELECT INTO Example
Problem: Copy all suppliers from
the USA to a new SupplierUSA table.
the USA to a new SupplierUSA table.
SELECT * INTO SupplierUSA FROM Supplier WHERE Country = 'USA'
Result: 4 rows inserted
Here are the records in the newly created table SupplierUSA:
Id | CompanyName | ContactName | City | Country | Phone | Fax |
---|---|---|---|---|---|---|
2 | New Orleans Cajun Delights | Shelley Burke | New Orleans | USA | (100) 555-4822 | NULL |
3 | Grandma Kelly's Homestead | Regina Murphy | Ann Arbor | USA | (313) 555-5735 | (313) 555-3349 |
16 | Bigfoot Breweries | Cheryl Saylor | Bend | USA | (100) 555-4822 | NULL |
19 | New England Seafood Cannery | Robb Merchant | Boston | USA | (617) 555-3267 | (617) 555-3389 |