-1

将表变量插入到具有多列(ID、编号等)的 TempTable 中

表变量之一是:

DECLARE @@gvTempTable TABLE (Number bigint, ID int) 

insert into  @@gvTempTable (Number) values ('21212321332332');
insert into  @@gvTempTable (Number) values ('100000000000');
insert into  @@gvTempTable (ID) values ('1');
insert into  @@gvTempTable (ID) values ('12');

select * into ##GlobalTempTable from @@gvTempTable;
select * from ##GlobalTempTable;

我需要将 ID 和 Number 显示为没有 NULL 的一行

这将返回一种笛卡尔积:

[![Current vs Desired result set][1]][1]

[1]:http://i.stack.imgur.com/twal9.png

4

1 回答 1

0

这会给你想要的结果

select  Number = min(Number), ID = min(ID)
into    ##GlobalTempTable
from
(
    select *, rn = row_number() over (partition by case when Number IS NULL then 1 else 2 end order by ID) 
    from    @@gvTempTable
) d
group by rn
于 2016-04-01T04:56:41.753 回答