Iterating Through Table in High-Performance Code

↘锁芯ラ 提交于 2019-12-12 02:38:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!