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
Release: 2024-12-24 17:08:15
Original
822 people have browsed it

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 ....
)
Copy after login

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

;WITH SomeClause1 AS
(
  SELECT ....
)
, SomeClause2 AS
(
  SELECT ....
)
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template