【问题标题】:Use attributes from one query in the next one inside sp在 sp 内的下一个查询中使用来自一个查询的属性
【发布时间】:2021-02-14 16:00:17
【问题描述】:

我需要从一个 sp 中返回两个表。我需要第二个使用第一个结果中的属性。我可以这样做。

select UberId, ... from A where ...;
select * from B where UberId in (select UberId from A where ...);

但我想知道是否可以不运行括号内的子查询,而是直接使用第一个查询的输出。性能会更好吗?

【问题讨论】:

  • 不,您不能在后者中引用先前的结果集。但是,如果您希望这 2 个查询(第一个和后者)是相同的数据,您可能希望将该数据放入一个临时表中。

标签: sql sql-server tsql stored-procedures


【解决方案1】:

使用临时表。

select UberId, ... 
into #t
from A 
where ...;

select * from #t;

select * from B 
where UberId in (select UberId from #t);

【讨论】:

    猜你喜欢
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 2016-04-06
    • 1970-01-01
    • 2018-02-15
    相关资源
    最近更新 更多