【问题标题】:T-SQL Insert Column from Other TableT-SQL 从其他表插入列
【发布时间】:2014-03-20 06:28:26
【问题描述】:

您好,我正在尝试将值从一个表列插入到另一个表。我正在使用 SQL Server 2008 R2。示例如下:

表 1

Declare @Table1 table (file varchar(15), type int, partiesid int)

表 2

Declare @Table2 table (file varchar(15), ....many other columns)

我想将表 2 中的文件以及其他一些静态值插入到表 1 中。

所以 select * from table1 最终会是这样的:

File           Type        PartiesID

GW100          1           555
GW101          1           555
GW103          1           555
GW104          1           555

其中 GW100、GW101 等来自 table2,而 1 和 555 对于每一行都是静态的。

我尝试插入 table1 (file,type,partiesid) 值(从 table2,1,555 中选择文件),但这不起作用。有没有办法为每个唯一文件插入一行,并将 1 和 555 的静态字段作为单独的列插入?

谢谢!

【问题讨论】:

标签: sql sql-server-2008 sql-server-2008-r2 sql-insert


【解决方案1】:
Insert into @table2
(
file,
type,
partiesid,
...(other columns for which you need to give static values)
)
select 
file,
type,
partiesid,
...(static values for columns)
from @table1

【讨论】:

    【解决方案2】:

    您可以尝试以下查询:

    USE dbName
    GO
    INSERT INTO table2 (column_name(s))
    SELECT column_name(s) FROM table1;
    GO
    

    【讨论】:

    • 不会@必须在表名之前吗?
    猜你喜欢
    • 2011-12-03
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多