0

请告诉我如何从一个表中获取一个值并将该值插入另一个表中。

Table1     Table2
Col1       Col1  Col2 
10         16    Null 
4          17    Null
8
9

我想在 Table2 中插入值,如下所示。

    Table2
    column 1   column 2
    10          16
    10          17
    4           16
    4           17
    8           16
    8           17
    9           16
    9           17
4

2 回答 2

1
INSERT INTO table2 
SELECT t1.col1, 
       t2.col2 
FROM   (SELECT Row_number() 
                 OVER( 
                   ORDER BY (SELECT 0)) rno, 
               * 
        FROM   table1) t1 
       CROSS JOIN table2 t2 
ORDER  BY t1.rno  
Go
delete from table2 where col2 is null
于 2013-09-26T08:50:27.190 回答
0

您可以使用cross join

小提琴演示

insert into table2
select t1.col1 , t2.col1 col2
from table1 t1 cross join table2 t2;


--if you don't need null values
delete table2 where col2 is null;

--results
select * from table2;
于 2013-09-26T09:05:19.967 回答