Always use with statement like ;WITH
then you’ll never get this error. The WITH command required a ;
between it and any previous command, by always using ;WITH
you’ll never have to remember to do this.
See WITH common_table_expression (Transact-SQL), from the section Guidelines for Creating and Using Common Table Expressions:
When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon.