I've always used WITH
in my queries, however, I've never understood exactly why '; ' before the WITH
clause is required.
The error is very succinct, but gives an idea that WITH
requires the previous statement to be closed, but why?
;WITH
, it does not generate the error.
DECLARE @pID INT = 1
WITH CTE_TABELA AS
(SELECT * FROM TABELA)
SELECT * FROM CTE_TABELA CTE
WHERE CTE.ID = @pID
SQL error message:
Msg 319 , Level 15, State 1, Line 4
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement should be terminated with a semicolon.
English
Incorrect syntax near the 'WITH' keyword. If this statement is a common table expression, an xmlnamespaces clause, or a change tracking context clause, the previous statement should be terminated with a semicolon .
The same question applies also clauses:
- WITH XMLNAMESPACES
Home
- WITH CHANGE_TRACKING_CONTEXT
References
Using Expressions of common table
Using WITH WITH command on Sql Server