【问题标题】:How to get rid of #temp tables and CTE in SQL Server如何摆脱 SQL Server 中的#temp 表和 CTE
【发布时间】:2013-04-24 09:21:41
【问题描述】:

因此,我问了一个关于如何摆脱#temp 表的问题。

How to get rid of #temp tables

也得到了解决.. 但我也不想使用 CTE。

任何想法,如何将所有这些查询合并为一个。

select {some columns} into #temp1 from {some tables} where {conditions1}
select {some other columns} into #temp2 from {some tables} where {conditions2}
select {some other columns} into #temp3 from {some tables} where {conditions3}

select {some columns from all #temp tables} from #temp1, #temp2,
#temp3 where {conditions}

谢谢

【问题讨论】:

  • 如果没有表格脚本、所需列、WHERE 和 JOIN 条件,我们真的无能为力
  • @gbn,完全同意......但我不能在这里发布我的查询。谢谢
  • 其实我已经解决了……但我也遇到了一些其他的异常。

标签: sql-server-2005 left-join common-table-expression


【解决方案1】:

我完全不清楚你想要实现什么,但如果你不想使用 CTE(无论出于什么奇怪的原因),派生表可能会做你想做的事:

select {some columns 
from ( 
  select {some columns} 
  from {some tables} 
  where {conditions1}
) as t1
  join ( 
    select {some other columns} 
    from {some tables} 
    where {conditions2}
  ) as t2 ON {join condition between t1 and t2}
  join (
    select {some other columns}  
    from {some tables} 
    where {conditions3}
  ) as t3 ON {join condition between t2 and t3}
where {conditions}

(虽然从技术上讲并没有真正的区别,但这只是写同一件事的不同方式)

【讨论】:

  • 你是对的......它只是另一种编写 CTE 内容的方式......但我想,我也不能使用它......我想为它编写单个查询。跨度>
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-09
  • 1970-01-01
  • 2010-10-14
相关资源
最近更新 更多