开发者

Select Insert substituting local variable for a column

开发者 https://www.devze.com 2023-02-03 00:54 出处:网络
I want to Insert records from one table into another like Insert into table2([column1], [column2], [column3])

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 a开发者_开发技巧ll 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?


The way you've done it is correct:

INSERT INTO table2 ([column1], [column2], [column3])
SELECT @variable, column2, column3
FROM table1


what you have should work just fine

declare @variable int 
set @variable = 2

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

精彩评论

暂无评论...
验证码 换一张
取 消