The way SQL inserts data from one table into another table _mssql

Source: Internet
Author: User
Column names do not necessarily have to be the same, as long as you are listed in HH the list of columns to be inserted corresponds to list one by one of the selected columns in the Select from MM table, of course, the data types on both sides should be compatible.
Insert into HH (FIELDA,FIELDB,FIELDC) select Fieldx,fieldy,fieldz from mm
Copy Code code as follows:

---Update the data that does not exist in the metering point, transfer the information in the ledger to the metering point
Insert into metricpoints (METERID,METRICPOINTNAME,[DESCRIPTION],DEPARTMENTID,MEDIUMID)
(SELECT M.meterid,m.metricitems+m.installplace as M_metricpointname,m.metricitems,m.departmentid,m.mediumid
From Meters M WHERE does EXISTS (SELECT 1 from metricpoints where Metricpoints.meterid = M.meterid) and M.mediumid =2)

Fame: a,b, all tables
Copy Code code as follows:

--b table exists (same as two table structure)
INSERT INTO b select * from a

If the two tables are only partially (fields) identical, then
Copy Code code as follows:

Insert into B (col1,col2,col3,col4,...) Select Col1,col2,col3,col4,... from a where ...

Insert Table A into table B.
Copy Code code as follows:

--b table does not exist
SELECT * into B from a
//
Select (Field 1, Field 2, ...) into B from

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.