问题
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?
回答1:
A simple Cartesian product which is a CROSS JOIN (Wikipedia, MSDN)
INSERT TABLEC
(SomeValue1, SomeValue2)
SELECT
TABLEA.SomeValue, TABLEB.SomeValue
FROM
TABLEA CROSS JOIN TABLEB
来源:https://stackoverflow.com/questions/6290865/iterating-through-table-in-high-performance-code