Faizi
Faizi

Reputation: 460

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)

Upvotes: 1

Views: 37

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269873

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;

Upvotes: 2

Related Questions