SQL SELECT INTO
With SQL, you can copy information from one table to another.
The SELECT INTO statement copies data from one table and then inserts the data into a new table.
SQL SELECT INTO statement
The SELECT INTO statement copies data from one table and then inserts the data into another new table.
SQL SELECT INTO syntax
We can copy all columns and insert them into a new table:
//SELECT *
//INTO newtable [IN externaldb]
//FROM table1;
//INTO newtable [IN externaldb]
//FROM table1;
Or just copy the desired columns and insert them into the new table:
SELECT column_name(s)
INTO newtable [IN externaldb]
FROM table1;
INTO newtable [IN externaldb]
FROM table1;
## Tips: The new table will use the SELECT statement Create the column names and types defined in . You can use the AS clause to apply the new name. |
##SQL SELECT INTO Example
Create a backup copy of Customers:
SELECT *INTO WebsitesBackup2016
SELECT *FROM Websites;
Please use the IN clause to copy the table to another database: INTO WebsitesBackup2016 IN 'Backup.mdb'
SELECT name,
urlFROM Websites;
Copy only some columns and insert them into the new table:INTO WebsitesBackup2016
SELECT *FROM Websites;
Copy only Chinese websites and insert them into the new table: INTO WebsitesBackup2016
SELECT Websites.name, access_log.count, access_log.dateFROM Websites
WHERE country='CN';
Copy data from multiple tables and insert it into a new table: WHERE country='CN';
INTO WebsitesBackup2016
Tip:FROM Websites
LEFT JOIN access_log
ON Websites.id=access_log.site_id;
LEFT JOIN access_log
ON Websites.id=access_log.site_id;
The SELECT INTO statement can be used to create a new empty table through another schema. Just add a WHERE clause that causes the query to return no data:
SELECT *INTO
newtable
FROM table1
WHERE 1=0;
FROM table1
WHERE 1=0;