且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL:在SELECT语句中使用INSERT

更新时间:2023-01-16 17:23:06

INSERT实际上将接受SELECT作为其输入.您可以只使用此命令(您将SELECT复制为子查询,尽管您可能可以重构它以返回那些没有子查询的不同GUID ...但是我需要查看整个怪物).

INSERT will actually accept a SELECT as its input. You can just use this command (your SELECT copied as a subquery, though you could probably refactor it to return those distinct GUIDs without a subquery...but I'd need to see the whole monster).

insert into (cs_ads_StoreLog)
select distinct
    a.LocationGUID
from
    (SELECT
        cs_ads_StoreLocations.LocationGUID, *Bunch of Selects AS Distance 
    FROM
        *Bunch of Inter Joins*
    WHERE
        *Bunch of conditions*
    ORDER BY 
        *You don't want to know*
    ) a

然后,您只需返回您拥有的SELECT(它将被缓存,因此您不必担心性能过高).

Then, you can just return the SELECT that you have (it'll be cached, so you don't need to worry about performance hits too much).