The SELECT into statement copies the data from one table and then inserts the data into another new table.
SQL SELECT into syntax
We can copy all the columns into the new table:
SELECT * into
newtable[In
Externaldb] From
table1;
Or just copy the columns you want into the new table:
SELECT
column_name (s)Into
newtable[In
Externaldb] From
table1;
|
Tip: The new table will be created with the column name and type defined in the SELECT statement. You can use the AS clause to apply a new name. |
SQL SELECT into instance
To create a backup copy of the Customers:
SELECT * into WebsitesBackup2016 from Websites;
Use the IN clause to copy the table to another database:
SELECT * into WebsitesBackup2016 in ' Backup.mdb ' from Websites;
Copy only some columns into the new table:
SELECT name, url into WebsitesBackup2016 from Websites;
Copy only the Chinese site into the new table:
SELECT * into WebsitesBackup2016 from Websites WHERE country= ' CN ';
Copy data from multiple tables into a new table:
SELECT Websites.name, Access_log.count, access_log.date into WebsitesBackup2016 from Websites left joins Access_log on Webs ites.id=access_log.site_id;
Tips: The SELECT into statement can be used to create a new empty table from another schema. Just add a WHERE clause that urges the query to return no data:
SELECT * into
newtableFrom
table1WHERE 1=0;
SQL SELECT into statement