gpt4 book ai didi

连接 CTE 时 SQL Server 语法错误

转载 作者:行者123 更新时间:2023-12-02 09:33:11 25 4
gpt4 key购买 nike

我正在使用 SQL Server 2014,但下面的 SQL 查询遇到问题。似乎某个地方有语法错误,但我就是找不到它。

我正在从两个单独的 View (即 ReservationListARACCOUNT)连接两个选择查询。

USE MyDatabase

;WITH q1 AS (
select *
from ReservationList
WHERE [Market Segment Code] <> 'COMP' AND
NOT (Status] = 'CANCELED' OR [Status] = 'NOSHOW' OR [Status] = 'WAITLIST') AND
NOT [Departure Date] < '2014-07-01' AND
NOT [Market Segment Code] = 'COMP' AND
NOT [Arrival Date] = [Departure Date]
)a

q2 AS (
SELECT
[PMSConfirmationNumber],
[FOC ACCOUNT],
[AR ACCOUNT NAME]
FROM
ARACCOUNT
)b

Select
q1.*
from
q1
JOIN q2 ON q1.PmsConfirmatioNnumber = q2.foh_PmsConfirmationNumber

错误信息如下:

Msg 102, Level 15, State 1, Line 21
Incorrect syntax near 'a'.
Msg 102, Level 15, State 1, Line 29
Incorrect syntax near ')'.

我还需要从 ARACCOUNT 选择的字段出现在最终输出中。如何修改我的查询以包含我的第 2 季度选择?

最佳答案

两个 CTE 之间缺少逗号。另外,删除别名 - 定义 CTE 时不需要它们:

;WITH q1 AS (


select *

from ReservationList

WHERE [Market Segment Code] <> 'COMP'

AND NOT ([Status] = 'CANCELED' OR [Status] = 'NOSHOW' OR [Status] = 'WAITLIST')

AND NOT [Departure Date] < '2014-07-01'

AND NOT [Market Segment Code] = 'COMP'

AND NOT [Arrival Date] = [Departure Date]

), -- alias dropped and comma added here

q2 AS (

SELECT [PMSConfirmationNumber],[FOC ACCOUNT],[AR ACCOUNT NAME]

FROM ARACCOUNT

) -- alias dropped

Select q1.*, q2.* from q1
JOIN q2 ON q1.PmsConfirmatioNnumber = q2.foh_PmsConfirmationNumber

关于连接 CTE 时 SQL Server 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30659208/

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