Home >Database >Mysql Tutorial >Why Does SQL Server Throw 'Incorrect Syntax near 'with'' and How Can I Fix It?

Why Does SQL Server Throw 'Incorrect Syntax near 'with'' and How Can I Fix It?

DDD
DDDOriginal
2024-12-24 17:08:15831browse

Why Does SQL Server Throw

Overcoming Error: "Incorrect Syntax near 'with'": Separating CTEs in SQL Server

When working with SQL Server 2005, you may encounter the error "Incorrect syntax near the keyword 'with'." This occurs when multiple WITH clauses are used in a single statement without proper separation.

Specifically, if your statement contains two or more WITH clauses like:

WITH SomeClause1 AS
(
  SELECT ....
)
WITH SomeClause2 AS
(
  SELECT ....
)

you will receive this error. To resolve it, you can use a comma to separate the CTEs:

;WITH SomeClause1 AS
(
  SELECT ....
)
, SomeClause2 AS
(
  SELECT ....
)

By adding a semicolon at the end of the first CTE and separating the subsequent CTEs with commas, you ensure that each CTE is properly terminated. This allows SQL Server to parse the statement correctly and execute it without errors.

The above is the detailed content of Why Does SQL Server Throw 'Incorrect Syntax near 'with'' and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn