4
I’ve always used the WITH
in my consultations, however, I never understood exactly why is required the ';' before the clause WITH
.
The Error is very succinct, but it gives an idea that the WITH
requires the previous instruction to be terminated, but why?
Query that creates the problem
Change to ;WITH
, does not generate error.
DECLARE @pID INT = 1
WITH CTE_TABELA AS
(SELECT * FROM TABELA)
SELECT * FROM CTE_TABELA CTE
WHERE CTE.ID = @pID
Error message in SQL:
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 must be terminated with a semicolon.
Português
Incorrect syntax near the 'WITH' keyword. If this statement is a common table expression, xmlnamespaces clause or change tracking context clause, the previous instruction shall be closed with a semicolon.
The same question applies also to clauses:
- WITH XMLNAMESPACES
- WITH CHANGE_TRACKING_CONTEXT
References
Using common table expressions
Using WITH AS command in Sql Server
The problem is that word
WITH
can be used ashint
also and this confuses the compiler.– Sorack
@Wow Sorack! That makes total sense! This complements Maniero’s response and justifies what he said about the compiler. Thank you!
– Ismael