选择插入替换列的局部变量

发布于 2024-10-10 11:25:28 字数 441 浏览 1 评论 0原文

我想将一个表中的记录插入到另一个表中,

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

但是我想插入存储在变量中的特定值,而不是来自表一的所有三个值。我认为它看起来像这样这样

declare @variable int
set @variable = 2

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

,表一中的每一行都会插入到表二中,唯一的区别是第一列中的每个值都是 2。

如果不使用游标,这可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过气美图社 2024-10-17 11:25:28

你这样做的方式是正确的:

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

The way you've done it is correct:

INSERT INTO table2 ([column1], [column2], [column3])
SELECT @variable, column2, column3
FROM table1
话少情深 2024-10-17 11:25:28

你所拥有的应该可以正常工作

declare @variable int 
set @variable = 2

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文