Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
tsql将列合并到一个表中_Tsql_Sql Server 2005 - Fatal编程技术网

tsql将列合并到一个表中

tsql将列合并到一个表中,tsql,sql-server-2005,Tsql,Sql Server 2005,如何将列合并到一个表中 declare @map_old table(ID int not null) insert into @map_old select 1 insert into @map_old select 2 declare @map_new table(ID int not null) insert into @map_new select 11 insert into @map_new select 22 declare @map(ID int not null, ID

如何将列合并到一个表中

declare @map_old table(ID int not null) insert into @map_old select 1 insert into @map_old select 2 declare @map_new table(ID int not null) insert into @map_new select 11 insert into @map_new select 22 declare @map(ID int not null, ID2 int not null) 声明@map_旧表(ID int不为空) 插入@map\u旧选择1 插入@map\u旧选择2 声明@map_新表(ID int非空) 插入@map_新建选择11 插入@map_新建选择22 声明@map(ID int不为null,ID2 int不为null) @map中的结果应为:

ID ID2 1 11 2 22 ID ID2 1 11 2 22
有什么建议吗?谢谢

您可以在如下表中使用identity:

declare @map_old table(iden int identity,ID int not null) 
insert into @map_old select 1
insert into @map_old select 2

declare @map_new table(iden int identity,ID int not null) 
insert into @map_new select 11
insert into @map_new select 22

declare @map table(ID int not null, ID2 int not null)

insert into @map
select t.ID, t2.ID from @map_old t join @map_new t2 on  t.iden = t2.iden

如果您确定此表的行数相同

那么可能是这样的:

;WITH CTE
AS
(
    SELECT
        ROW_NUMBER() OVER(ORDER BY ID) AS RowNbr,
        mapNew.ID
    FROM
        @map_new AS mapNew
),CTE2
AS
(
    SELECT
        ROW_NUMBER() OVER(ORDER BY ID) AS RowNbr,
        mapOld.ID
    FROM
        @map_old AS mapOld
)
INSERT INTO @map(ID,ID2)
SELECT
    CTE.ID,
    CTE2.ID
FROM
    CTE
    JOIN CTE2
        ON CTE.RowNbr=CTE2.RowNbr

SELECT * FROM @map

为什么
11
而不是
122
?您需要并排“粘贴”表吗?我需要为每一行创建一个与第二个表的关系。。。每个选项卡具有相同的行数。