Table copy statements insert into and select

Source: Internet
Author: User

In complex business logic, data in one table is often copied to another table;

SQL provides me with the most common method 2:

First, insert

Second, select

Here is an example of an instance:

 
----- Orders table ------------------ create table mdprices (ID int identity () not null primary key, goods int not null, area int not null, guideprice decimal ), createtime datetime default getdate () ----------------------------------- history tables --------------- create table md_historyprices (idint identity () not null primary key, goods int not null, area int not null, price decimal (18,4), createtime datetime) ---------- insert into -------- insert into md_historyprices (goods, area, price, createtime) select goods, area, guideprice, createtime from mdprices ---------- query -------------- select * From md_historyprices ----------------------------------

In the second case, select

Syntax select * into Table 1 from table 2

Table 1 cannot exist in our DB. Table 1 is created when the meaning of select into is executed, and does not need to be created in advance;

Select Identity (smallint, 1, 1) as ID, goods, area, guideprice, createtime into historyprice from mdprices

 

The Identity () function is used to edit the auto-increment number from 1 in the historyprice table. The seed value is 1;

Syntax: Identity (data_type, seed, increment) as column_nmae

Only use the SELECT statement with the into Table clause to insert the ID column into the new table, but not the identity attribute used with create table.

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.