Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Insert substituting local variable for a column

I want to Insert records from one table into another like

Insert into table2([column1], [column2], [column3])  
select column1, column2, column3  
from table1

however instead of all three values coming from table one I would like to insert a specific value stored in a variable. I Think it would look something like this

declare @variable int
set @variable = 2

Insert into table2([column1], [column2], [column3]) 
select @variable, column2, column3  
from table1

In this way every row from table one would be inserted into table two with the only difference being every value in column one would be 2.

Is this possible without using a cursor?

like image 201
Scottfivefour Avatar asked Dec 07 '25 20:12

Scottfivefour


1 Answers

The way you've done it is correct:

INSERT INTO table2 ([column1], [column2], [column3])
SELECT @variable, column2, column3
FROM table1
like image 77
Phil Hunt Avatar answered Dec 11 '25 14:12

Phil Hunt