开发者

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 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜