SQL SELECT into statement

Source: Internet
Author: User
Tags one table

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.