简体   繁体   中英

How to move certain column data from 1st table to 2nd table. And filling other columns of 2nd table with different data in a single query

I know about this query:

INSERT INTO table2 (column1, column2, column3)

SELECT column1, column2, column3,

FROM table1

WHERE condition; 

Let's say, I have at least 5 columns in table2. I want to get data of first three columns from table1 for table 2. I can do this with the above query. But I also want to fill other 2 columns of table2 with some data that is stored in variables. Is there any way to do so?
I want something like this:

INSERT INTO table2 (column1, column2, column3,column4,column5)

VALUES(

SELECT column1, column2, column3,

FROM table1

WHERE condition 

, @dataForColumn4, @dataForColumn5)

You just include them in the SELECT :

INSERT INTO table2 (column1, column2, column3, column4, column5)
    SELECT column1, column2, column3, @column4, @column5
    FROM table1
    WHERE condition;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM