Reputation: 25058
So many times seen with
and, so many times SQL Server ask that with has ;
before it
How does ;with ...
work??
;with coords(...) as (
SELECT * ...
)
Why must have ;
before it?
Upvotes: 7
Views: 13804
Reputation: 57093
It is best practise to terminate every SQL statement with a semicolon. The SQL Server docs (for example here) suggest doing so will be mandated in a future version to there's really no excuse for not getting into the habit now.
To answer the question: you see ;WITH...
on Stackoverflow because EITHER the person answering is a sloppy coder OR the person answering assumes the person asking the question is a sloppy coder (and they'll claim it is the latter when it is the former :) The definition of "sloppy coder" here is someone who only uses a semicolon when they are forced to do so.
Upvotes: 3
Reputation: 700830
The semicolon is used in SQL to end a query. Putting it before a query like that is just to make sure that the database understands that any previous query has ended.
Originally it was required after each query as they were entered line by line, so the database had to know when to run the query. When the entire query is sent in a single string, you only need semicolons in the case where the SQL syntax is not enough to determine where a query ends. As the with
keyword has different uses a semicolon is sometimes needed before it to make sure that it's not part of the previous query.
Upvotes: 14
Reputation: 3972
The use of WITH is for common table expressions (CTEs). They were trying to force the CTE to be defined as the first statement (i.e. cannot be linked with other parts of the query hence the ;)
Upvotes: 1
Reputation: 432667
Using WITH for CTEs requires the previous statement to be terminated with ;
. Using it at the start like this guarantees correct syntax
So does MERGE in SQL Server 2008
See this SO question: Incorrect syntax near the keyword 'with'...previous statement must be terminated with a semicolon
Upvotes: 5