One statement is used to copy multiple data records between SQL Server tables.

Source: Internet
Author: User
Table 1
ID Int   4   0
R_name Nvarchar   20   1
R_class Nvarchar   20   1
R_p Int   4   1
R_p1 Int   4   1
R_p2 Money   8   1
R_p3 Money   8   1
R_type Int   4   1
R_color Varchar   7   1
R_remark Nvarchar   200   1
R_max Int   4   1

 

Table 2
ID Int   4   0
R_id Int   4   1
X_id Int   4   1
X_p Money   8   1
X_p1 Money   8   1
X_p2 Money   8   1
X_p3 Money   8   1

 

Table 1 Data
Id R_p r_p1 r_p2 r_p3
1 1 12 23 34
2 45 56 57 58
3 76 89 92 99
......
100 356 234 231 213

 

 

Table 2 no data

 

Objective: To copy ID, R_p, r_p1, r_p2, r_p3, r_p4 from table 1 to r_id, x_p, x_p1, x_p2, x_p3 of table 2, and x_id is fixed. It is convenient to initialize some values from a table.

Insert   Into Table 2 (x_id, r_id, x_p, x_p1, x_p2, x_p3) Select   1 , ID, R_p, r_p1, r_p2, r_p3 From Table 1

 

After copying:

Table 2
Id x_id r_id R_p r_p1 r_p2 r_p3
1 1 1 1 12 23 34
2 1 2 45 56 57 58
3 1 3 76 89 92 99  

 


......
100 1 100 356 234 231 213

 

The essence of this statement cannot be described in languages due to poor writing.Code.

Related Article

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.