在高性能的代码迭代通过表(Iterating Through Table in High-Perfo

2019-10-28 20:35发布

Supposing I have two temporary tables

CREATE TABLE TableA
(
    SomeValue    NVARCHAR(64)
)

CREATE TABLE TableB
(
    SomeValue    NVARHCAR(64)
)

and a final table

CREATE TABLE TableC
(
    SomeValue1    NVARCHAR(64),
    SomeValue2    NVARHCAR(64)
),

what is the best way to insert into TableC every possible combination of values from TableA and TableB in a high-perfomance fashion? I know cursors must be the least thing to think about, but will two WHILE loops do it fast enough?

Answer 1:

一个简单的笛卡儿积是一个CROSS JOIN( 维基百科 , MSDN )

INSERT TABLEC 
   (SomeValue1, SomeValue2)
SELECT
   TABLEA.SomeValue, TABLEB.SomeValue
FROM
   TABLEA CROSS JOIN TABLEB


文章来源: Iterating Through Table in High-Performance Code