【问题标题】:Cross Join Values in SQL Server 2005SQL Server 2005 中的交叉联接值
【发布时间】:2014-08-22 12:52:32
【问题描述】:

我使用的是 SQL Server 2005,但出现错误:

关键字“VALUES”附近的语法不正确。

尝试运行此查询时:

  SELECT T.N 
  FROM Table
  CROSS JOIN (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9)) as T(N)
  WHERE 1 = 1

但在 SQL Server 2008 中却不行——在 2008 年效果很好。

我必须在 SQL Server 2005 中做什么才能使其正常工作?

【问题讨论】:

标签: sql sql-server sql-server-2005


【解决方案1】:

只需将selectunion all 一起使用:

SELECT T.N
FROM Table CROSS JOIN
     (select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 union all
      select 6 union all select 7 union all select 8 union all select 9
     ) as T(N)
WHERE 1=1;

或者,使用递归 CTE,这样您就不必输入值:

with t(n) as
      select 1 as n
      union all
      select n + 1
      from t
      where n < 9
     )
select t.n
from table1 cross join
     t
where 1 = 1;

【讨论】:

  • 是的,我想过,但是union all?认真的吗?
猜你喜欢
  • 2010-09-26
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 2012-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多