gpt4 book ai didi

sql - SQL Server 2005 中的交叉联接值

转载 作者:行者123 更新时间:2023-12-01 09:56:34 25 4
gpt4 key购买 nike

我正在使用 SQL Server 2005,但出现错误:

Incorrect syntax near the keyword '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 中做什么才能使其正常工作?

最佳答案

只需使用 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;

关于sql - SQL Server 2005 中的交叉联接值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25438099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com