Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.

Error Message:
Msg 319, Level 15, State 1, Line 3
Incorrect syntax near the keyword ‘with’. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.

Severity level:
15.

Description:
This error message appears when you try to use a common table expression or an xmlnamespace clause, but the previous statement has not been terminated by a semicolon.

Consequences:
The SQL statement cannot be parsed and further execution is stopped.

Resolution:
Error of the Severity level 16 are generated by the user and can be fixed by the SQL Server user. The statement can not be run this way. You either must terminate the previous statement with a semicolon or begin the common table expression with a semicolon.

Versions:
This error message was introduced with SQL Server 2005.

Example(s):
SELECT * FROM (SELECT 1 UNION ALL SELECT 2) x (c1)

WITH myCte
AS
(SELECT 1)

SELECT * FROM myCte

Remarks:
In the above example we try to use the common table expression myCte. Because the previous statement has not been terminated with a semicolon, the error is raised.

It is considered best practise to terminate all statements with a semicolon.

]]>

Leave a comment

Your email address will not be published.