不断收到错误 ')' 附近的错误语法。 (102)(SQLExecDirectW)

Keeps getting error Incorrect syntax near ')'. (102) (SQLExecDirectW)

刚开始学习 CTE,按照教程中的所有内容进行操作,但我总是出错

Incorrect syntax near ')'. (102) (SQLExecDirectW)

这是Users table:

这是我的代码:

WITH BannedCTE
AS
(SELECT Users_Id
FROM Users
WHERE Users.Banned = "Yes");

试试下面的查询。您应该将 CTE 与 select、DML 操作(插入、更新、删除)一起使用。对字符串使用单引号。

WITH BannedCTE AS (SELECT Users_Id FROM Users WHERE Users.Banned = 'Yes')
select * from BannedCTE ;

来自官方:

A CTE must be followed by a single SELECT, INSERT, UPDATE, or DELETE statement that references some or all the CTE columns. A CTE can also be specified in a CREATE VIEW statement as part of the defining SELECT statement of the view.

所以,试试这个:

;WITH BannedCTE AS 
(
    SELECT Users_Id 
    FROM Users
    WHERE Users.Banned = 'Yes'
)
SELECT * 
FROM BannedCTE ;