删除数据库服务器加入临时表

时间:2017-06-27 15:45:15

标签: sql sql-server

SELECT l.CKey,
       wl.LKey,
       l.AKey, 
       l.LKey
       FROM table1 l 
       INNER JOIN DatabaseServer.Table2 wl 

        ON l.CKey = wl.CKey 
        WHERE  LKey NOT IN (select LKey from DatabaseServer.Table3 wc 
        where wc.LKey = wl.LKey and wc.AKey = l.AKey)

我有上面的查询,我想删除临时表的跨数据库服务器连接。你能告诉我如何摆脱跨数据库服务器连接

1 个答案:

答案 0 :(得分:1)

select *
into temptable
from DatabaseServer.Table2
WHERE DatabaseServer.Table2.Ckey in (select Ckey from table1)


select *
into temptable2
from DatabaseServer.Table3
WHERE DatabaseServer.Table3.AKey not in (select AKey from table1)


SELECT l.CKey,
       wl.LKey,
       l.AKey, 
       l.LKey
       FROM table1 l 
       INNER JOIN temptable wl 

        ON l.CKey = wl.CKey 
        WHERE  LKey NOT IN (select LKey from temptable2 wc 
        where wc.LKey = wl.LKey and wc.AKey = l.AKey)

编辑:如果需要过滤临时表中的数据,则只需将WHERE添加到SELECT INTO查询中。问题是过滤器的复杂程度。

EDIT2:我添加了可能有助于创建临时表的过滤器。根据这将给您的数据量,您可能希望在运行查询本身之前在临时表上创建索引。