SQL statement to copy the table or table structure and content to the new table

Source: Internet
Author: User

SQL statement implementation, SQL Server 2000 and access copy the oldtable structure of the old table, or structure and content to the new table newtable

Both of them have been tried. The following code can be used in both databases (SQL Server 2000 and access.

-- Copy the new table structure
Select * into newtable from oldtable where 1 = 2

-- Copy the table structure and content to the new table
Select * into newtable from oldtable

If you only need to combine some fields in the old table or previous table or the summarized content into a field, you can

-- Copy some fields in the table structure to the new table.
Select title, type, price, notes OK into newtable2 from titles where 1 = 2

-- Copy the table structure and content to the new table. You can select fields.
Select title, type, price, pub_id-price OK into newtable2 from titles

You can also copy the table structure (you can select some fields as the new table) and insert the content.

-- Copy some fields in the table structure to the new table.
Select title Y1, type Y2, price Y3 into yongfa365 from titles where 1 = 2

-- Add data to the new table
Insert into yongfa365 (Y1, Y2, Y3) Select title, type, price from titles

In fact, we can see from the above example that no matter whether it is copying the table structure, content, or summary results, we can all go to a new table. Why? I think of it now, I can
(Select * from
Table) as a table, then there is an answer to this question: Since you can regard the selected result as a table, the table can be saved and directly saved as a real table, for later use.
You can remove the yellow font above and run the same code.

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.