[英]T-SQL Insert Column from Other Table
Hello I am trying to insert values from one table column to another table. 您好,我正在尝试将值从一个表列插入到另一个表。 I am using SQL Server 2008 R2
. 我正在使用SQL Server 2008 R2
。 Here is example: 这是示例:
Table 1 表格1
Declare @Table1 table (file varchar(15), type int, partiesid int)
Table 2 表2
Declare @Table2 table (file varchar(15), ....many other columns)
I would like to insert file from Table 2 into Table 1 as well as some other static values. 我想将表2中的文件以及其他一些静态值插入到表1中。
So select * from table1 would end up looking like this: 因此,从table1中选择*最终看起来像这样:
File Type PartiesID
GW100 1 555
GW101 1 555
GW103 1 555
GW104 1 555
where the GW100, GW101, etc come from table2 and the 1 and 555 are static for every row. 其中GW100,GW101等来自表2,并且每行1和555都是静态的。
I tried insert into table1 (file,type,partiesid) values (select file from table2,1,555) but that doesn't work. 我试图插入到table1(文件,类型,partiesid)值(从table2,1,555中选择文件)中,但是那不起作用。 Is there a way where it will just insert a row for each unique file and also insert the static fields of 1 and 555 as separate columns? 有没有一种方法可以为每个唯一文件插入一行,并将静态字段1和555插入为单独的列?
Thanks! 谢谢!
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
You can try the below query: 您可以尝试以下查询:
USE dbName
GO
INSERT INTO table2 (column_name(s))
SELECT column_name(s) FROM table1;
GO
This link gives you the format(s): 该链接为您提供格式:
http://www.w3schools.com/sql/sql_insert_into_select.asp http://www.w3schools.com/sql/sql_insert_into_select.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.